butane_codegen 0.8.1

Macros for Butane. Do not use this crate directly -- use the butane crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Macros for butane

//The quote macro can require a high recursion limit
#![recursion_limit = "256"]
#![deny(missing_docs)]
extern crate proc_macro;

use std::path::PathBuf;

use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier};
use butane_core::{codegen, make_compile_error, migrations, SqlType};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::TokenTree;
use quote::quote;
use syn::{Expr, Ident};

mod filter;

/// Attribute macro which marks a struct as being a data model and
/// generates an implementation of [`DataObject`](butane_core::DataObject). This
/// macro will also write information to disk at compile time necessary to
/// generate migrations
///
/// ## Restrictions on model types:
/// 1. The type of each field must implement [`FieldType`] or be [`Many`].
/// 2. There must be a primary key field. This must be either annotated with a `#[pk]` attribute or named `id`.
///
/// ## Helper Attributes
/// * `#[table = "NAME"]` used on the struct to specify the name of the table (defaults to struct name)
/// * `#[pk]` on a field to specify that it is the primary key.
/// * `#[unique]` on a field indicates that the field's value must be unique
///   (perhaps implemented as the SQL UNIQUE constraint by some backends).
/// * `#[default]` should be used on fields added by later migrations to avoid errors on existing objects.
///   Unnecessary if the new field is an `Option<>`
///
/// For example
/// ```ignore
/// #[model]
/// #[table = "posts"]
/// pub struct Post {
///   #[pk] // unnecessary if identifier were named id instead
///   pub identifier: AutoPk<i32>,
///   pub title: String,
///   pub content: String,
///   #[default = false]
///   pub published: bool,
/// }
/// ```
///
///
/// [`FieldType`]: crate::FieldType
/// [`Many`]: butane_core::many::Many
#[proc_macro_attribute]
pub fn model(_args: TokenStream, input: TokenStream) -> TokenStream {
    codegen::model_with_migrations(input.into(), &mut migrations_for_dir()).into()
}

/// Attribute macro which generates an implementation of
/// [`DataResult`](butane_core::DataResult). Continuing with our blog
/// post example from [model](macro@model), we could create a `DataResult` with
/// only some of the fields from `Post` (to avoid fetching all of them in a query).
///
/// ```ignore
/// #[dataresult(Post)]
/// pub struct PostMetadata {
///   pub id: i64,
///   pub title: String,
///   pub pub_time: Option<NaiveDateTime>,
/// }
/// ```
///
/// Note that the attribute takes a parameter saying which Model this
/// result is a subset of. Every field named in the DataResult must be
/// present in the Model.
#[proc_macro_attribute]
pub fn dataresult(args: TokenStream, input: TokenStream) -> TokenStream {
    codegen::dataresult(args.into(), input.into()).into()
}

/// Macro to construct a [`BoolExpr`] (for use with a [`Query`]) from
/// an expression with Rust syntax.
///
/// Using this macro instead of constructing a `BoolExpr` has two
/// advantages:
/// 1. It will generally be more ergonomic
/// 2. References to nonexistent fields or type mismatches
///    (e.g. comparing a number to a string) will generate a compilation error
///
/// Usage: `filter!(Foo, expr)` where `Foo` is a model type (with the
/// `#[model]` attribute applied) and `expr` is a Rust-like expression
/// with a boolean value. `Foo`'s fields may be referred to as if they
/// were variables.
///
/// # Rust values
/// To refer to values from the surrounding rust function, enclose
/// them in braces, like `filter!(Foo, bar == {bar})`
///
/// # Function-like operations
/// Filters support some operations for which Rust does not have operators and which are instead
/// represented syntactically as function calls.
/// * `like`: parameter is a SQL LIKE expression string, e.g. `title.like("M%").
/// * `matches`: Parameter is a sub-expression. Use with a
///   [`ForeignKey`] field to evaluate as true if the referent
///   matches. For example, to find all posts made in blogs by people
///   named "Pete" we might say `filter!(Post, `blog.matches(author == "Pete"))`.
/// * `contains`: Essentially the many-to-many version of `matches`.
///   Parameter is a sub-expression. Use with a [`Many`]
///   field to evaluate as true if one of the many referents matches
///   the given expression. For example, in a blog post model with a field
///   `tags: Many<Tag>` we could filter to posts with a "cats" with
///   the following `tags.contains(tag == "cats"). If the expression
///   is single literal, it is assumed to be used to match the
///   primary key.
/// * `is_in`: checks if a value is one of the provided parameters, e.g. `title.is_in(vec!["Foo", "Bar"])`.
///
/// # Examples
/// ```ignore
/// # use butane_core::query::BoolExpr;
/// # use butane_codegen::model;
/// # use butane_codegen::filter;
/// #[model]
/// struct Contestant {
///   #[pk]
///   name: String,
///   rank: i32,
///   nationality: String
/// }
/// let e: BoolExpr = filter!(Contestant, nationality == "US" && rank < 42);
/// let first_place = 1;
/// let e2 = filter!(Contestant, rank == { first_place });
/// let e3 = filter!(Contestant, name.like("A%"));
/// ```
///
/// [`BoolExpr`]: butane_core::query::BoolExpr
/// [`ForeignKey`]: butane_core::fkey::ForeignKey
/// [`Many`]: butane_core::many::Many
/// [`Query`]: butane_core::query::Query
#[proc_macro]
pub fn filter(input: TokenStream) -> TokenStream {
    let input: TokenStream2 = input.into();
    let args: Vec<TokenTree> = input.into_iter().collect();
    if args.len() < 2 {
        return make_compile_error!("Expected filter!(Type, expression)").into();
    }
    let tyid: Ident = match &args[0] {
        TokenTree::Ident(tyid) => tyid.clone(),
        TokenTree::Group(g) => match syn::parse2::<Ident>(g.stream()) {
            Ok(ident) => ident,
            Err(_) => {
                return make_compile_error!("Unexpected tokens in database object type {:?}", &g)
                    .into()
            }
        },
        _ => {
            return make_compile_error!("Unexpected tokens in database object type {:?}", &args[0])
                .into()
        }
    };

    if matches!(args[1], TokenTree::Punct(_)) {
    } else {
        return make_compile_error!("Expected filter!(Type, expression)").into();
    }

    let expr: TokenStream2 = args.into_iter().skip(2).collect();
    let expr: Expr = match syn::parse2(expr) {
        Ok(expr) => expr,
        Err(_) => {
            return make_compile_error!(
                "Expected filter!(Type, expression) but could not parse expression"
            )
            .into()
        }
    };
    filter::for_expr(&tyid, &expr).into()
}

/// Attribute macro which marks a type as being available to butane
/// for use in models.
///
/// May be used on type aliases, structs, or enums. Except when used
/// on type aliases, it must be given a parameter specifying the
/// SqlType it can be converted to.
///
/// E.g.
/// ```ignore
/// #[butane_type]
/// pub type CurrencyAmount = f64;
///
/// #[butane_type(Text)]
/// pub enum Currency {
///   Dollars,
///   Pounds,
///   Euros,
/// }
/// impl ToSql for Currency {
///   fn to_sql(&self) -> SqlVal {
///      SqlVal::Text(
///          match self {
///              Self::Dollars => "dollars",
///              Self::Pounds => "pounds",
///              Self::Euros => "euros",
///          }
///          .to_string())
///  }
/// }
/// ```
#[proc_macro_attribute]
pub fn butane_type(args: TokenStream, input: TokenStream) -> TokenStream {
    codegen::butane_type_with_migrations(args.into(), input.into(), &mut migrations_for_dir())
        .into()
}

fn migrations_for_dir() -> migrations::FsMigrations {
    migrations::from_root(migrations_dir())
}

fn migrations_dir() -> PathBuf {
    let mut dir = PathBuf::from(
        std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR expected to be set"),
    );
    dir.push(".butane");
    dir.push("migrations");
    dir
}

/// Derive macro for `FieldType`.
/// Produces a String field for simple enums, otherwise uses a JSON field if json feature is enabled.
/// E.g.
/// ```ignore
/// #[derive(FieldType)]
/// pub enum Currency {
///   Dollars,
///   Pounds,
///   Euros,
/// }
/// ```
#[proc_macro_derive(FieldType)]
pub fn derive_field_type(input: TokenStream) -> TokenStream {
    let derive_input = syn::parse_macro_input!(input as syn::DeriveInput);
    let ident = &derive_input.ident;
    match derive_input.data {
        syn::Data::Struct(syn::DataStruct {
            fields: syn::Fields::Unnamed(syn::FieldsUnnamed { unnamed, .. }),
            ..
        }) => {
            if unnamed.len() == 1 {
                let field = unnamed.first().unwrap();
                let path = codegen::extract_path_from_type(&field.ty);
                if let Some(DeferredSqlType::KnownId(TypeIdentifier::Ty(sqltype))) =
                    codegen::get_primitive_sql_type(path)
                {
                    return derive_field_type_for_newtype(ident, sqltype);
                }
            }
            derive_field_type_with_json(ident)
        }
        syn::Data::Struct(syn::DataStruct {
            fields: syn::Fields::Named { .. },
            ..
        })
        | syn::Data::Struct(syn::DataStruct {
            fields: syn::Fields::Unit,
            ..
        }) => derive_field_type_with_json(ident),
        syn::Data::Enum(data_enum) => derive_field_type_for_enum(ident, data_enum),
        syn::Data::Union(_) => derive_field_type_with_json(ident),
    }
}

fn derive_field_type_for_newtype(ident: &Ident, sqltype: SqlType) -> TokenStream {
    let sqltype_name = serde_variant::to_variant_name(&sqltype).unwrap();
    let sqltype_ident = syn::Ident::new(sqltype_name, proc_macro2::Span::call_site());

    let mut migrations = migrations_for_dir();
    codegen::add_custom_type(
        &mut migrations,
        ident.to_string(),
        DeferredSqlType::KnownId(TypeIdentifier::Ty(sqltype)),
    )
    .unwrap();

    quote!(
        impl butane::ToSql for #ident
        {
            fn to_sql(&self) -> butane::SqlVal {
                self.0.to_sql()
            }
            fn to_sql_ref(&self) -> butane::SqlValRef<'_> {
                self.0.to_sql_ref()
            }
        }
        impl butane::FromSql for #ident
        {
            fn from_sql_ref(val: butane::SqlValRef) -> std::result::Result<Self, butane::Error> {
                let inner = butane::FromSql::from_sql_ref(val)?;
                Ok(Self ( inner ))
            }
        }
        impl butane::FieldType for #ident
        {
            type RefType = Self;
            const SQLTYPE: butane::SqlType = butane::SqlType:: #sqltype_ident;
        }
    )
    .into()
}

fn derive_field_type_for_enum(ident: &Ident, data_enum: syn::DataEnum) -> TokenStream {
    if data_enum
        .variants
        .iter()
        .any(|variant| variant.fields != syn::Fields::Unit)
    {
        // Non-simple enum, fall back to json derive
        return derive_field_type_with_json(ident);
    }

    let mut migrations = migrations_for_dir();

    codegen::add_custom_type(
        &mut migrations,
        ident.to_string(),
        DeferredSqlType::KnownId(TypeIdentifier::Ty(SqlType::Text)),
    )
    .unwrap();

    let match_arms_to_string: Vec<TokenStream2> = data_enum
        .variants
        .iter()
        .map(|variant| {
            let v_ident = &variant.ident;
            let ident_literal = codegen::make_ident_literal_str(v_ident);
            quote!(Self::#v_ident => #ident_literal,)
        })
        .collect();
    let match_arms_from_string: Vec<TokenStream2> = data_enum
        .variants
        .iter()
        .map(|variant| {
            let v_ident = &variant.ident;
            let ident_literal = codegen::make_ident_literal_str(v_ident);
            quote!(#ident_literal => Ok(Self::#v_ident),)
        })
        .collect();
    quote!(
        impl #ident {
            fn to_string_for_butane(&self) -> &'static str {
                match self {
                    #(#match_arms_to_string)*
                }
            }
            fn from_string_for_butane(s: &str) -> std::result::Result<Self, butane::Error> {
                match s {
                    #(#match_arms_from_string)*
                    _ => Err(butane::Error::UnknownEnumVariant(s.to_string()))
                }
            }
        }
        impl butane::ToSql for #ident
        {
            fn to_sql(&self) -> butane::SqlVal {
                butane::SqlVal::Text(self.to_string_for_butane().to_string())
            }
            fn to_sql_ref(&self) -> butane::SqlValRef<'_> {
                butane::SqlValRef::Text(self.to_string_for_butane())
            }
        }

        impl butane::FromSql for #ident
        {
            fn from_sql_ref(val: butane::SqlValRef) -> std::result::Result<Self, butane::Error> {
                if let butane::SqlValRef::Text(v) = val {
                    return Self::from_string_for_butane(v);
                }
                Err(butane::Error::CannotConvertSqlVal(
                    butane::SqlType::Text,
                    val.into(),
                ))
            }
        }
        impl butane::FieldType for #ident
        {
            type RefType = Self;
            const SQLTYPE: butane::SqlType = butane::SqlType::Text;
        }
    )
    .into()
}

#[cfg(feature = "json")]
fn derive_field_type_with_json(struct_name: &Ident) -> TokenStream {
    let mut migrations = migrations_for_dir();

    codegen::add_custom_type(
        &mut migrations,
        struct_name.to_string(),
        DeferredSqlType::KnownId(TypeIdentifier::Ty(SqlType::Json)),
    )
    .unwrap();
    quote!(
        impl butane::ToSql for #struct_name
        {
            fn to_sql(&self) -> butane::SqlVal {
                self.to_sql_ref().into()
            }
            fn to_sql_ref(&self) -> butane::SqlValRef<'_> {
                butane::SqlValRef::Json(serde_json::to_value(self).unwrap())
            }
        }

        impl butane::FromSql for #struct_name
        {
            fn from_sql_ref(val: butane::SqlValRef) -> std::result::Result<Self, butane::Error> {
                if let butane::SqlValRef::Json(v) = val {
                    use ::serde::Deserialize;
                    return Ok(#struct_name::deserialize(v).unwrap());
                }
                Err(butane::Error::CannotConvertSqlVal(
                    butane::SqlType::Json,
                    val.into(),
                ))
            }
        }
        impl butane::FieldType for #struct_name
        {
            type RefType = Self;
            const SQLTYPE: butane::SqlType = butane::SqlType::Json;
        }
    )
    .into()
}

#[cfg(not(feature = "json"))]
fn derive_field_type_with_json(_struct_name: &Ident) -> TokenStream {
    panic!("Feature 'json' is required to derive FieldType")
}

/// Derive macro for marker trait `PrimaryKeyType`.
/// E.g.
/// ```ignore
/// #[derive(FieldType, PrimaryKeyType)]
/// pub struct PostId(pub uuid::Uuid);
/// ```
#[proc_macro_derive(PrimaryKeyType)]
pub fn derive_primary_key_type(input: TokenStream) -> TokenStream {
    let derive_input = syn::parse_macro_input!(input as syn::DeriveInput);
    let ident = &derive_input.ident;
    quote!(
        impl butane::PrimaryKeyType for #ident {}
    )
    .into()
}