Skip to main content

diesel_derives/
util.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::parse::{Parse, ParseStream, Peek, Result};
4use syn::token::Eq;
5use syn::{Data, DeriveInput, GenericArgument, Ident, Type, parenthesized, parse_quote};
6
7use crate::model::Model;
8
9pub const COLUMN_NAME_NOTE: &str = "column_name = foo";
10pub const SQL_TYPE_NOTE: &str = "sql_type = Foo";
11pub const SERIALIZE_AS_NOTE: &str = "serialize_as = Foo";
12pub const DESERIALIZE_AS_NOTE: &str = "deserialize_as = Foo";
13pub const TABLE_NAME_NOTE: &str = "table_name = foo";
14pub const TREAT_NONE_AS_DEFAULT_VALUE_NOTE: &str = "treat_none_as_default_value = true";
15pub const TREAT_NONE_AS_NULL_NOTE: &str = "treat_none_as_null = true";
16pub const BELONGS_TO_NOTE: &str = "belongs_to(Foo, foreign_key = foo_id)";
17pub const MYSQL_TYPE_NOTE: &str = "mysql_type(name = \"foo\")";
18pub const SQLITE_TYPE_NOTE: &str = "sqlite_type(name = \"foo\")";
19pub const POSTGRES_TYPE_NOTE: &str = "postgres_type(name = \"foo\", schema = \"public\")";
20pub const POSTGRES_TYPE_NOTE_ID: &str = "postgres_type(oid = 37, array_oid = 54)";
21pub const SELECT_EXPRESSION_NOTE: &str =
22    "select_expression = schema::table_name::column_name.is_not_null()";
23pub const SELECT_EXPRESSION_TYPE_NOTE: &str =
24    "select_expression_type = dsl::IsNotNull<schema::table_name::column_name>";
25pub const CHECK_FOR_BACKEND_NOTE: &str = "diesel::pg::Pg";
26pub const BASE_QUERY_NOTE: &str =
27    "base_query = schema::table_name::table.order_by(schema::table_name::id)";
28pub const BASE_QUERY_TYPE_NOTE: &str =
29    "base_query_type = dsl::OrderBy<schema::table_name::table, schema::table_name::id>";
30
31pub fn unknown_attribute(name: &Ident, valid: &[&str]) -> syn::Error {
32    let prefix = if valid.len() == 1 { "" } else { " one of" };
33
34    syn::Error::new(
35        name.span(),
36        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unknown attribute, expected{1} `{0}`",
                valid.join("`, `"), prefix))
    })format!(
37            "unknown attribute, expected{prefix} `{}`",
38            valid.join("`, `")
39        ),
40    )
41}
42
43pub fn parse_eq<T: Parse>(input: ParseStream, help: &str) -> Result<T> {
44    if input.is_empty() {
45        return Err(syn::Error::new(
46            input.span(),
47            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unexpected end of input, expected `=`\nhelp: the correct format looks like `#[diesel({0})]`",
                help))
    })format!(
48                "unexpected end of input, expected `=`\n\
49                 help: the correct format looks like `#[diesel({help})]`",
50            ),
51        ));
52    }
53
54    input.parse::<Eq>()?;
55    input.parse()
56}
57
58/// Specialized version of `parse_eq` for `syn::Type` with a customized error message for readability.
59/// This is useful because a great variety of tokens would be valid to parse as a `syn::Type`.
60pub fn parse_eq_type(input: ParseStream, help: &str) -> Result<Type> {
61    if input.is_empty() {
62        return Err(syn::Error::new(
63            input.span(),
64            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unexpected end of input, expected `=`\nhelp: the correct format looks like `#[diesel({0})]`",
                help))
    })format!(
65                "unexpected end of input, expected `=`\n\
66                 help: the correct format looks like `#[diesel({help})]`",
67            ),
68        ));
69    }
70
71    input.parse::<Eq>()?;
72    input
73        .parse::<Type>()
74        .map_err(|e| syn::Error::new(e.span(), "expected type"))
75}
76
77pub fn parse_paren<T: Parse>(input: ParseStream, help: &str) -> Result<T> {
78    if input.is_empty() {
79        return Err(syn::Error::new(
80            input.span(),
81            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unexpected end of input, expected parentheses\nhelp: the correct format looks like `#[diesel({0})]`",
                help))
    })format!(
82                "unexpected end of input, expected parentheses\n\
83                 help: the correct format looks like `#[diesel({help})]`",
84            ),
85        ));
86    }
87
88    let content;
89    match ::syn::__private::parse_parens(&input) {
    ::syn::__private::Ok(parens) => {
        content = parens.content;
        _ = content;
        parens.token
    }
    ::syn::__private::Err(error) => { return ::syn::__private::Err(error); }
};parenthesized!(content in input);
90    content.parse()
91}
92
93pub fn parse_paren_list<T, D>(
94    input: ParseStream,
95    help: &str,
96    sep: D,
97) -> Result<syn::punctuated::Punctuated<T, <D as Peek>::Token>>
98where
99    T: Parse,
100    D: Peek,
101    D::Token: Parse,
102{
103    if input.is_empty() {
104        return Err(syn::Error::new(
105            input.span(),
106            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unexpected end of input, expected parentheses\nhelp: the correct format looks like `#[diesel({0})]`",
                help))
    })format!(
107                "unexpected end of input, expected parentheses\n\
108                 help: the correct format looks like `#[diesel({help})]`",
109            ),
110        ));
111    }
112
113    let content;
114    match ::syn::__private::parse_parens(&input) {
    ::syn::__private::Ok(parens) => {
        content = parens.content;
        _ = content;
        parens.token
    }
    ::syn::__private::Err(error) => { return ::syn::__private::Err(error); }
};parenthesized!(content in input);
115    content.parse_terminated(T::parse, sep)
116}
117
118pub fn wrap_in_dummy_mod(item: TokenStream) -> TokenStream {
119    {
    let mut _s = ::quote::__private::TokenStream::new();
    ::quote::__private::push_ident(&mut _s, "const");
    ::quote::__private::push_underscore(&mut _s);
    ::quote::__private::push_colon(&mut _s);
    ::quote::__private::push_group(&mut _s,
        ::quote::__private::Delimiter::Parenthesis,
        ::quote::__private::TokenStream::new());
    ::quote::__private::push_eq(&mut _s);
    ::quote::__private::push_group(&mut _s,
        ::quote::__private::Delimiter::Brace,
        {
            let mut _s = ::quote::__private::TokenStream::new();
            ::quote::__private::push_ident(&mut _s, "use");
            ::quote::__private::push_ident(&mut _s, "diesel");
            ::quote::__private::push_semi(&mut _s);
            ::quote::ToTokens::to_tokens(&item, &mut _s);
            _s
        });
    ::quote::__private::push_semi(&mut _s);
    _s
}quote! {
120        const _: () = {
121            // This import is not actually redundant. When using diesel_derives
122            // inside of diesel, `diesel` doesn't exist as an extern crate, and
123            // to work around that it contains a private
124            // `mod diesel { pub use super::*; }` that this import will then
125            // refer to. In all other cases, this imports refers to the extern
126            // crate diesel.
127            use diesel;
128
129            #item
130        };
131    }
132}
133
134pub fn inner_of_option_ty(ty: &Type) -> &Type {
135    option_ty_arg(ty).unwrap_or(ty)
136}
137
138pub fn is_option_ty(ty: &Type) -> bool {
139    option_ty_arg(ty).is_some()
140}
141
142fn option_ty_arg(mut ty: &Type) -> Option<&Type> {
143    use syn::PathArguments::AngleBracketed;
144
145    // Check the inner equivalent type
146    loop {
147        match ty {
148            Type::Group(group) => ty = &group.elem,
149            Type::Paren(paren) => ty = &paren.elem,
150            _ => break,
151        }
152    }
153
154    match *ty {
155        Type::Path(ref ty) => {
156            let last_segment = ty.path.segments.iter().next_back().unwrap();
157            match last_segment.arguments {
158                AngleBracketed(ref args) if last_segment.ident == "Option" => {
159                    match args.args.iter().next_back() {
160                        Some(GenericArgument::Type(ty)) => Some(ty),
161                        _ => None,
162                    }
163                }
164                _ => None,
165            }
166        }
167        _ => None,
168    }
169}
170
171pub fn ty_for_foreign_derive(item: &DeriveInput, model: &Model) -> Result<Type> {
172    if model.foreign_derive {
173        match item.data {
174            Data::Struct(ref body) => match body.fields.iter().next() {
175                Some(field) => Ok(field.ty.clone()),
176                None => Err(syn::Error::new(
177                    proc_macro2::Span::mixed_site(),
178                    "foreign_derive requires at least one field",
179                )),
180            },
181            _ => Err(syn::Error::new(
182                proc_macro2::Span::mixed_site(),
183                "foreign_derive can only be used with structs",
184            )),
185        }
186    } else {
187        let ident = &item.ident;
188        let (_, ty_generics, ..) = item.generics.split_for_impl();
189        Ok(::syn::__private::parse_quote({
        let mut _s = ::quote::__private::TokenStream::new();
        ::quote::ToTokens::to_tokens(&ident, &mut _s);
        ::quote::ToTokens::to_tokens(&ty_generics, &mut _s);
        _s
    })parse_quote!(#ident #ty_generics))
190    }
191}
192
193pub fn camel_to_snake(name: &str) -> String {
194    let mut result = String::with_capacity(name.len());
195    result.push_str(&name[..1].to_lowercase());
196    for character in name[1..].chars() {
197        if character.is_uppercase() {
198            result.push('_');
199            for lowercase in character.to_lowercase() {
200                result.push(lowercase);
201            }
202        } else {
203            result.push(character);
204        }
205    }
206    result
207}