Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions diesel_derives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ homepage = "https://diesel.rs"
repository = "https://github.com/diesel-rs/diesel/tree/master/diesel_derives"

[dependencies]
syn = { version = "0.13.0", features = ["full", "fold"] }
quote = "0.5.0"
syn = { version = "0.14", features = ["full", "fold"] }
quote = "0.6"
clippy = { optional = true, version = "=0.0.195" }
proc-macro2 = "0.3.0"
proc-macro2 = "0.4"

[dev-dependencies]
cfg-if = "0.1.0"
Expand Down
15 changes: 7 additions & 8 deletions diesel_derives/src/as_changeset.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use quote;
use proc_macro2::{self, Span};
use syn;
use proc_macro2::Span;

use diagnostic_shim::*;
use field::*;
use meta::*;
use model::*;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let treat_none_as_null = MetaItem::with_name(&item.attrs, "changeset_options")
.map(|meta| {
meta.warn_if_other_options(&["treat_none_as_null"]);
Expand All @@ -18,7 +17,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
.unwrap_or(Ok(false))?;
let model = Model::from_item(&item)?;
let struct_name = &model.name;
let table_name = model.table_name();
let table_name = &model.table_name();

let (_, ty_generics, where_clause) = item.generics.split_for_impl();
let mut impl_generics = item.generics.clone();
Expand Down Expand Up @@ -91,9 +90,9 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {

fn field_changeset_ty(
field: &Field,
table_name: syn::Ident,
table_name: &syn::Ident,
treat_none_as_null: bool,
lifetime: Option<quote::Tokens>,
lifetime: Option<proc_macro2::TokenStream>,
) -> syn::Type {
let column_name = field.column_name();
if !treat_none_as_null && is_option_ty(&field.ty) {
Expand All @@ -107,9 +106,9 @@ fn field_changeset_ty(

fn field_changeset_expr(
field: &Field,
table_name: syn::Ident,
table_name: &syn::Ident,
treat_none_as_null: bool,
lifetime: Option<quote::Tokens>,
lifetime: Option<proc_macro2::TokenStream>,
) -> syn::Expr {
let field_access = field.name.access();
let column_name = field.column_name();
Expand Down
11 changes: 5 additions & 6 deletions diesel_derives/src/as_expression.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use quote;
use syn;

use meta::*;
use proc_macro2::{self, Ident, Span};
use syn;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let dummy_mod = format!(
"_impl_as_expression_for_{}",
item.ident.as_ref().to_lowercase()
item.ident.to_string().to_lowercase()
);
let flags =
MetaItem::with_name(&item.attrs, "diesel").unwrap_or_else(|| MetaItem::empty("diesel"));
Expand Down Expand Up @@ -106,7 +105,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {

if any_sql_types {
Ok(wrap_in_dummy_mod(
dummy_mod.into(),
Ident::new(&dummy_mod, Span::call_site()),
quote! {
use self::diesel::expression::AsExpression;
use self::diesel::expression::bound::Bound;
Expand Down
19 changes: 9 additions & 10 deletions diesel_derives/src/associations.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use quote;
use syn;

use diagnostic_shim::*;
use meta::*;
use model::*;
use proc_macro2;
use syn;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let model = Model::from_item(&item)?;
let tokens = MetaItem::all_with_name(&item.attrs, "belongs_to")
.into_iter()
Expand All @@ -30,15 +29,15 @@ fn derive_belongs_to(
model: &Model,
generics: &syn::Generics,
meta: MetaItem,
) -> Result<quote::Tokens, Diagnostic> {
) -> Result<proc_macro2::TokenStream, Diagnostic> {
let AssociationOptions {
parent_struct,
foreign_key,
} = AssociationOptions::from_meta(meta)?;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let foreign_key_field = model.find_column(foreign_key)?;
let struct_name = model.name;
let foreign_key_field = model.find_column(&foreign_key)?;
let struct_name = &model.name;
let foreign_key_access = foreign_key_field.name.access();
let foreign_key_ty = inner_of_option_ty(&foreign_key_field.ty);
let table_name = model.table_name();
Expand Down Expand Up @@ -86,7 +85,7 @@ impl AssociationOptions {
let foreign_key = meta.nested_item("foreign_key")
.ok()
.map(|i| i.ident_value())
.unwrap_or_else(|| Ok(infer_foreign_key(parent_struct)))?;
.unwrap_or_else(|| Ok(infer_foreign_key(&parent_struct)))?;

let unrecognized_options = meta.nested()?.skip(1).filter(|n| n.name() != "foreign_key");
for ignored in unrecognized_options {
Expand All @@ -103,7 +102,7 @@ impl AssociationOptions {
}
}

fn infer_foreign_key(name: syn::Ident) -> syn::Ident {
let snake_case = camel_to_snake(name.as_ref());
fn infer_foreign_key(name: &syn::Ident) -> syn::Ident {
let snake_case = camel_to_snake(&name.to_string());
syn::Ident::new(&format!("{}_id", snake_case), name.span())
}
9 changes: 4 additions & 5 deletions diesel_derives/src/diesel_numeric_ops.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use quote;
use proc_macro2::{self, Ident, Span};
use syn;

use util::*;

pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
let struct_name = item.ident;
pub fn derive(mut item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let struct_name = &item.ident;

{
let where_clause = item.generics
Expand All @@ -21,7 +20,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
let dummy_name = format!("_impl_diesel_numeric_ops_for_{}", item.ident);

Ok(wrap_in_dummy_mod(
dummy_name.to_lowercase().into(),
Ident::new(&dummy_name.to_lowercase(), Span::call_site()),
quote! {
use self::diesel::expression::{ops, Expression, AsExpression};
use self::diesel::sql_types::ops::{Add, Sub, Mul, Div};
Expand Down
19 changes: 10 additions & 9 deletions diesel_derives/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::Span;
use proc_macro2::{self, Ident, Span};
use quote;
use syn::spanned::Spanned;
use syn;
use syn::spanned::Spanned;

use meta::*;
use util::*;
Expand All @@ -19,7 +19,7 @@ impl Field {
pub fn from_struct_field(field: &syn::Field, index: usize) -> Self {
let column_name_from_attribute =
MetaItem::with_name(&field.attrs, "column_name").map(|m| m.expect_ident_value());
let name = match field.ident {
let name = match field.ident.clone() {
Some(mut x) => {
// https://github.com/rust-lang/rust/issues/47983#issuecomment-362817105
let span = x.span();
Expand Down Expand Up @@ -50,15 +50,16 @@ impl Field {

pub fn column_name(&self) -> syn::Ident {
self.column_name_from_attribute
.clone()
.unwrap_or_else(|| match self.name {
FieldName::Named(x) => x,
FieldName::Named(ref x) => x.clone(),
_ => {
self.span
.error(
"All fields of tuple structs must be annotated with `#[column_name]`",
)
.emit();
"unknown_column".into()
Ident::new("unknown_column", Span::call_site())
}
})
}
Expand All @@ -81,7 +82,7 @@ impl FieldName {
parse_quote!(#tokens)
}

pub fn access(&self) -> quote::Tokens {
pub fn access(&self) -> proc_macro2::TokenStream {
let span = self.span();
// Span of the dot is important due to
// https://github.com/rust-lang/rust/issues/47312
Expand All @@ -90,16 +91,16 @@ impl FieldName {

pub fn span(&self) -> Span {
match *self {
FieldName::Named(x) => x.span(),
FieldName::Named(ref x) => x.span(),
FieldName::Unnamed(ref x) => x.span,
}
}
}

impl quote::ToTokens for FieldName {
fn to_tokens(&self, tokens: &mut quote::Tokens) {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match *self {
FieldName::Named(x) => x.to_tokens(tokens),
FieldName::Named(ref x) => x.to_tokens(tokens),
FieldName::Unnamed(ref x) => x.to_tokens(tokens),
}
}
Expand Down
18 changes: 10 additions & 8 deletions diesel_derives/src/from_sql_row.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use quote::Tokens;
use syn;

use meta::*;
use proc_macro2::{self, Ident, Span};
use syn;
use util::*;

pub fn derive(mut item: syn::DeriveInput) -> Result<Tokens, Diagnostic> {
pub fn derive(mut item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let flags =
MetaItem::with_name(&item.attrs, "diesel").unwrap_or_else(|| MetaItem::empty("diesel"));
let struct_ty = ty_for_foreign_derive(&item, &flags)?;
Expand All @@ -24,10 +23,13 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<Tokens, Diagnostic> {
}
let (impl_generics, _, where_clause) = item.generics.split_for_impl();

let dummy_mod = format!(
"_impl_from_sql_row_for_{}",
item.ident.as_ref().to_lowercase()
).into();
let dummy_mod = Ident::new(
&format!(
"_impl_from_sql_row_for_{}",
item.ident.to_string().to_lowercase()
),
Span::call_site(),
);
Ok(wrap_in_dummy_mod(
dummy_mod,
quote! {
Expand Down
11 changes: 5 additions & 6 deletions diesel_derives/src/identifiable.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use quote;
use syn;

use model::*;
use proc_macro2;
use syn;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let model = Model::from_item(&item)?;
let struct_name = model.name;
let struct_name = &model.name;
let table_name = model.table_name();

let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
Expand All @@ -17,7 +16,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
let (field_ty, field_access): (Vec<_>, Vec<_>) = model
.primary_key_names
.iter()
.filter_map(|&pk| model.find_column(pk).emit_error())
.filter_map(|ref pk| model.find_column(pk).emit_error())
.map(|f| (&f.ty, f.name.access()))
.unzip();

Expand Down
25 changes: 16 additions & 9 deletions diesel_derives/src/insertable.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use proc_macro2::{self, Span};
use syn;
use quote;
use proc_macro2::Span;

use field::*;
use model::*;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
let model = Model::from_item(&item)?;

if model.fields().is_empty() {
Expand All @@ -31,8 +30,8 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
.iter()
.map(|f| {
(
(field_ty(f, table_name, None)),
(field_expr(f, table_name, None)),
(field_ty(f, &table_name, None)),
(field_expr(f, &table_name, None)),
)
})
.unzip();
Expand All @@ -42,8 +41,8 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
.iter()
.map(|f| {
(
(field_ty(f, table_name, Some(quote!(&'insert)))),
(field_expr(f, table_name, Some(quote!(&)))),
(field_ty(f, &table_name, Some(quote!(&'insert)))),
(field_expr(f, &table_name, Some(quote!(&)))),
)
})
.unzip();
Expand Down Expand Up @@ -85,7 +84,11 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
))
}

fn field_ty(field: &Field, table_name: syn::Ident, lifetime: Option<quote::Tokens>) -> syn::Type {
fn field_ty(
field: &Field,
table_name: &syn::Ident,
lifetime: Option<proc_macro2::TokenStream>,
) -> syn::Type {
if field.has_flag("embed") {
let field_ty = &field.ty;
parse_quote!(#lifetime #field_ty)
Expand All @@ -101,7 +104,11 @@ fn field_ty(field: &Field, table_name: syn::Ident, lifetime: Option<quote::Token
}
}

fn field_expr(field: &Field, table_name: syn::Ident, lifetime: Option<quote::Tokens>) -> syn::Expr {
fn field_expr(
field: &Field,
table_name: &syn::Ident,
lifetime: Option<proc_macro2::TokenStream>,
) -> syn::Expr {
let field_access = field.name.access();
if field.has_flag("embed") {
parse_quote!(#lifetime self#field_access)
Expand Down
19 changes: 12 additions & 7 deletions diesel_derives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
#![cfg_attr(feature = "clippy", allow(option_map_unwrap_or_else, option_map_unwrap_or))]
#![cfg_attr(feature = "clippy",
warn(wrong_pub_self_convention, mut_mut, non_ascii_literal, similar_names,
unicode_not_nfc, if_not_else, items_after_statements, used_underscore_binding))]
#![cfg_attr(
feature = "clippy",
warn(
wrong_pub_self_convention, mut_mut, non_ascii_literal, similar_names, unicode_not_nfc,
if_not_else, items_after_statements, used_underscore_binding
)
)]
#![cfg_attr(feature = "nightly", feature(proc_macro))]

extern crate proc_macro;
Expand Down Expand Up @@ -41,8 +45,9 @@ mod sql_type;

use diagnostic_shim::*;

#[proc_macro_derive(AsChangeset,
attributes(table_name, primary_key, column_name, changeset_options))]
#[proc_macro_derive(
AsChangeset, attributes(table_name, primary_key, column_name, changeset_options)
)]
pub fn derive_as_changeset(input: TokenStream) -> TokenStream {
expand_derive(input, as_changeset::derive)
}
Expand Down Expand Up @@ -98,8 +103,8 @@ pub fn derive_sql_type(input: TokenStream) -> TokenStream {
}

fn expand_derive(
input: TokenStream,
f: fn(syn::DeriveInput) -> Result<quote::Tokens, Diagnostic>,
input: proc_macro::TokenStream,
f: fn(syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic>,
) -> TokenStream {
let item = syn::parse(input).unwrap();
match f(item) {
Expand Down
Loading