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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use crate::migrations::adb::{DeferredSqlType, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::{SqlType, SqlVal};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span, TokenTree};
use quote::{quote, ToTokens};
use syn::parse_quote;
use syn::{
    punctuated::Punctuated, Attribute, Field, ItemEnum, ItemStruct, ItemType, Lit, LitStr, Meta,
    MetaNameValue, NestedMeta,
};

#[macro_export]
macro_rules! make_compile_error {
    ($span:expr=> $($arg:tt)*) => ({
        let lit = crate::codegen::make_lit(&std::fmt::format(format_args!($($arg)*)));
        quote_spanned!($span=> compile_error!(#lit))
    });
    ($($arg:tt)*) => ({
        let lit = crate::codegen::make_lit(&std::fmt::format(format_args!($($arg)*)));
        quote!(compile_error!(#lit))
    })
}

mod dbobj;
mod migration;

pub fn model_with_migrations<M>(
    input: TokenStream2,
    ms: &mut impl MigrationsMut<M = M>,
) -> TokenStream2
where
    M: MigrationMut,
{
    // Transform into a derive because derives can have helper
    // attributes but proc macro attributes can't yet (nor can they
    // create field attributes)
    let mut ast_struct: ItemStruct = syn::parse2(input).unwrap();
    let config: dbobj::Config = config_from_attributes(&ast_struct);

    // Filter out our helper attributes
    let attrs: Vec<Attribute> = filter_helper_attributes(&ast_struct);

    let state_attrs = if has_derive_serialize(&attrs) {
        quote!(#[serde(skip)])
    } else {
        TokenStream2::new()
    };

    let vis = &ast_struct.vis;

    migration::write_table_to_disk(ms, &ast_struct, &config).unwrap();

    let impltraits = dbobj::impl_dbobject(&ast_struct, &config);
    let fieldexprs = dbobj::add_fieldexprs(&ast_struct);

    let fields: Punctuated<Field, syn::token::Comma> =
        match remove_helper_field_attributes(&mut ast_struct.fields) {
            Ok(fields) => fields.named.clone(),
            Err(err) => return err,
        };

    // If the program already declared a state field, remove it
    let fields = remove_existing_state_field(fields);

    let ident = ast_struct.ident;

    quote!(
        #(#attrs)*
        #vis struct #ident {
            #state_attrs
            pub state: butane::ObjectState,
            #fields
        }
        #impltraits
        #fieldexprs
    )
}

pub fn dataresult(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
    let dbo: Ident = syn::parse2(args)
        .expect("Model type must be specified as argument to dataresult attribute");
    let mut ast_struct: ItemStruct = syn::parse2(input).unwrap();

    // Filter out our helper attributes
    let attrs: Vec<Attribute> = filter_helper_attributes(&ast_struct);

    let state_attrs = if has_derive_serialize(&attrs) {
        quote!(#[serde(skip)])
    } else {
        TokenStream2::new()
    };

    let vis = &ast_struct.vis;

    let impltraits = dbobj::impl_dataresult(&ast_struct, &dbo);

    let fields = match remove_helper_field_attributes(&mut ast_struct.fields) {
        Ok(fields) => &fields.named,
        Err(err) => return err,
    };

    let ident = ast_struct.ident;

    quote!(
        #(#attrs)*
        #vis struct #ident {
            #state_attrs
            #fields
        }
        #impltraits
    )
}

pub fn butane_type_with_migrations<M>(
    args: TokenStream2,
    input: TokenStream2,
    ms: &mut impl MigrationsMut<M = M>,
) -> TokenStream2
where
    M: MigrationMut,
{
    let mut tyinfo: Option<CustomTypeInfo> = None;
    let type_alias: syn::Result<ItemType> = syn::parse2(input.clone());
    if let Ok(type_alias) = type_alias {
        tyinfo = Some(CustomTypeInfo {
            name: type_alias.ident.to_string(),
            ty: get_deferred_sql_type(&type_alias.ty),
        })
    }

    if tyinfo.is_none() {
        // For types below here, we need the SqlType given to us
        let args: TokenStream2 = args;
        let args: Vec<TokenTree> = args.into_iter().collect();
        if args.len() != 1 {
            return quote!(compile_error!("Expected butane_type(sqltype)"););
        }
        let tyid = match &args[0] {
            TokenTree::Ident(tyid) => tyid.clone(),
            _ => return quote!(compile_error!("Unexpected tokens in butane_type");),
        };
        let sqltype = match sqltype_from_name(&tyid) {
            Some(ty) => ty,
            None => {
                eprintln!("No SqlType value named {}", tyid.to_string());
                return quote!(compile_error!("No SqlType value with the given name"););
            }
        };

        if let Ok(item) = syn::parse2::<ItemStruct>(input.clone()) {
            tyinfo = Some(CustomTypeInfo {
                name: item.ident.to_string(),
                ty: DeferredSqlType::Known(sqltype),
            });
        } else if let Ok(item) = syn::parse2::<ItemEnum>(input.clone()) {
            tyinfo = Some(CustomTypeInfo {
                name: item.ident.to_string(),
                ty: DeferredSqlType::Known(sqltype),
            });
        }
    }

    match tyinfo {
        Some(tyinfo) => match add_custom_type(ms, tyinfo.name, tyinfo.ty) {
            Ok(()) => input,
            Err(e) => {
                eprintln!("unable to save type {}", e);
                quote!(compile_error!("unable to save type");)
            }
        },
        None => {
            quote!(compile_error!("The #[butane_type] macro wasn't expected to be used here");)
        }
    }
}

fn make_ident_literal_str(ident: &Ident) -> LitStr {
    let as_str = format!("{}", ident);
    LitStr::new(&as_str, Span::call_site())
}

pub fn make_lit(s: &str) -> LitStr {
    LitStr::new(s, Span::call_site())
}

fn filter_helper_attributes(ast_struct: &ItemStruct) -> Vec<Attribute> {
    ast_struct
        .attrs
        .clone()
        .into_iter()
        .filter(|a| !a.path.is_ident("table"))
        .collect()
}

fn config_from_attributes(ast_struct: &ItemStruct) -> dbobj::Config {
    let mut config = dbobj::Config::default();
    for attr in &ast_struct.attrs {
        if let Ok(Meta::NameValue(MetaNameValue {
            path,
            lit: Lit::Str(s),
            ..
        })) = attr.parse_meta()
        {
            if path.is_ident("table") {
                config.table_name = Some(s.value())
            }
        }
    }
    config
}

fn remove_helper_field_attributes(
    fields: &mut syn::Fields,
) -> std::result::Result<&syn::FieldsNamed, TokenStream2> {
    match fields {
        syn::Fields::Named(fields) => {
            for field in &mut fields.named {
                field.attrs.retain(|a| {
                    !a.path.is_ident("pk")
                        && !a.path.is_ident("auto")
                        && !a.path.is_ident("sqltype")
                        && !a.path.is_ident("default")
                });
            }
            Ok(fields)
        }
        _ => Err(make_compile_error!("Fields must be named")),
    }
}

// We allow model structs to declare the state: butane::ObjectState
// field for convenience so it doesn't appear so magical, but then we
// recreate it.
fn remove_existing_state_field(
    fields: Punctuated<Field, syn::token::Comma>,
) -> Punctuated<Field, syn::token::Comma> {
    fields
        .into_iter()
        .filter(|f| match (&f.ident, &f.ty) {
            (Some(ident), syn::Type::Path(typ)) => {
                ident != "state"
                    || typ
                        .path
                        .segments
                        .last()
                        .map_or(true, |seg| seg.ident != "ObjectState")
            }
            (_, _) => true,
        })
        .collect()
}

fn pk_field(ast_struct: &ItemStruct) -> Option<Field> {
    let pk_by_attribute =
        fields(ast_struct).find(|f| f.attrs.iter().any(|attr| attr.path.is_ident("pk")));
    if let Some(id_field) = pk_by_attribute {
        return Some(id_field.clone());
    }
    let pk_by_name = ast_struct.fields.iter().find(|f| match &f.ident {
        Some(ident) => *ident == "id",
        None => false,
    });
    if let Some(id_field) = pk_by_name {
        Some(id_field.clone())
    } else {
        None
    }
}

fn is_auto(field: &Field) -> bool {
    field.attrs.iter().any(|attr| attr.path.is_ident("auto"))
}

fn fields(ast_struct: &ItemStruct) -> impl Iterator<Item = &Field> {
    ast_struct
        .fields
        .iter()
        .filter(|f| f.ident.clone().unwrap() != "state")
}

fn get_option_sql_type(ty: &syn::Type) -> Option<DeferredSqlType> {
    get_foreign_type_argument(ty, "Option").map(|path| {
        let inner_ty: syn::Type = syn::TypePath {
            qself: None,
            path: path.clone(),
        }
        .into();

        get_deferred_sql_type(&inner_ty)
    })
}

fn get_many_sql_type(field: &Field) -> Option<DeferredSqlType> {
    get_foreign_sql_type(&field.ty, "Many")
}

fn is_many_to_many(field: &Field) -> bool {
    get_many_sql_type(field).is_some()
}

fn is_option(field: &Field) -> bool {
    get_foreign_type_argument(&field.ty, "Option").is_some()
}

/// Check for special fields which won't correspond to rows and don't
/// implement FieldType
fn is_row_field(f: &Field) -> bool {
    !is_many_to_many(f)
}

fn get_foreign_type_argument<'a>(ty: &'a syn::Type, tyname: &'static str) -> Option<&'a syn::Path> {
    let path = match ty {
        syn::Type::Path(path) => &path.path,
        _ => return None,
    };
    let seg = if path.segments.len() == 2 && path.segments.first().unwrap().ident == "butane" {
        path.segments.last()
    } else {
        path.segments.first()
    }?;
    if seg.ident != tyname {
        return None;
    }
    let args = match &seg.arguments {
        syn::PathArguments::AngleBracketed(args) => &args.args,
        _ => return None,
    };
    if args.len() != 1 {
        panic!("{} should have a single type argument", tyname)
    }
    match args.last().unwrap() {
        syn::GenericArgument::Type(syn::Type::Path(typath)) => Some(&typath.path),
        _ => panic!("{} argument should be a type.", tyname),
    }
}

fn get_foreign_sql_type(ty: &syn::Type, tyname: &'static str) -> Option<DeferredSqlType> {
    let typath = get_foreign_type_argument(ty, tyname);
    typath.map(|typath| {
        DeferredSqlType::Deferred(TypeKey::PK(
            typath
                .segments
                .last()
                .unwrap_or_else(|| panic!("{} must have an argument", tyname))
                .ident
                .to_string(),
        ))
    })
}

pub fn get_deferred_sql_type(ty: &syn::Type) -> DeferredSqlType {
    get_primitive_sql_type(ty)
        .or_else(|| get_option_sql_type(ty))
        .or_else(|| get_foreign_sql_type(ty, "ForeignKey"))
        .unwrap_or_else(|| {
            DeferredSqlType::Deferred(TypeKey::CustomType(
                ty.clone().into_token_stream().to_string(),
            ))
        })
}

/// Defaults are used for fields added by later migrations
/// Example
/// #[default = 42]
fn get_default(field: &Field) -> std::result::Result<Option<SqlVal>, CompilerErrorMsg> {
    let attr: Option<&Attribute> = field
        .attrs
        .iter()
        .find(|attr| attr.path.is_ident("default"));
    let lit: Lit = match attr {
        None => return Ok(None),
        Some(attr) => match attr.parse_meta() {
            Ok(Meta::NameValue(meta)) => meta.lit,
            _ => return Err(make_compile_error!("malformed default value").into()),
        },
    };
    Ok(Some(sqlval_from_lit(lit)?))
}

/// If the field refers to a primitive, return its SqlType
fn get_primitive_sql_type(ty: &syn::Type) -> Option<DeferredSqlType> {
    if *ty == parse_quote!(bool) {
        return Some(DeferredSqlType::Known(SqlType::Bool));
    } else if *ty == parse_quote!(u8)
        || *ty == parse_quote!(i8)
        || *ty == parse_quote!(u16)
        || *ty == parse_quote!(i16)
        || *ty == parse_quote!(u16)
        || *ty == parse_quote!(i32)
    {
        return Some(DeferredSqlType::Known(SqlType::Int));
    } else if *ty == parse_quote!(u32) || *ty == parse_quote!(i64) {
        // Future improvement: better support unsigned integers
        // here. Sqlite has no u64, though Postgres does
        return Some(DeferredSqlType::Known(SqlType::BigInt));
    } else if *ty == parse_quote!(f32) || *ty == parse_quote!(f64) {
        return Some(DeferredSqlType::Known(SqlType::Real));
    } else if *ty == parse_quote!(String) {
        return Some(DeferredSqlType::Known(SqlType::Text));
    } else if *ty == parse_quote!(Vec<u8>) {
        return Some(DeferredSqlType::Known(SqlType::Blob));
    }

    #[cfg(feature = "datetime")]
    {
        if *ty == parse_quote!(NaiveDateTime) {
            return Some(DeferredSqlType::Known(SqlType::Timestamp));
        }
    }

    #[cfg(feature = "uuid")]
    {
        if *ty == parse_quote!(Uuid) || *ty == parse_quote!(uuid::Uuid) {
            return Some(DeferredSqlType::Known(SqlType::Blob));
        }
    }

    None
}

fn has_derive_serialize(attrs: &[Attribute]) -> bool {
    for attr in attrs {
        if let Ok(Meta::List(ml)) = attr.parse_meta() {
            if ml.path.is_ident("derive")
                && ml.nested.iter().any(|nm| match nm {
                    NestedMeta::Meta(Meta::Path(path)) => path.is_ident("Serialize"),
                    _ => false,
                })
            {
                return true;
            }
        }
    }
    false
}

fn sqlval_from_lit(lit: Lit) -> std::result::Result<SqlVal, CompilerErrorMsg> {
    match lit {
        Lit::Str(lit) => Ok(SqlVal::Text(lit.value())),
        Lit::ByteStr(lit) => Ok(SqlVal::Blob(lit.value())),
        Lit::Byte(_) => Err(make_compile_error!("single byte literal is not supported").into()),
        Lit::Char(_) => Err(make_compile_error!("single char literal is not supported").into()),
        Lit::Int(lit) => Ok(SqlVal::Int(lit.base10_parse().unwrap())),
        Lit::Float(lit) => Ok(SqlVal::Real(lit.base10_parse().unwrap())),
        Lit::Bool(lit) => Ok(SqlVal::Bool(lit.value)),
        Lit::Verbatim(_) => {
            Err(make_compile_error!("raw verbatim literals are not supported").into())
        }
    }
}

struct CustomTypeInfo {
    name: String,
    ty: DeferredSqlType,
}

fn add_custom_type<M>(
    ms: &mut impl MigrationsMut<M = M>,
    name: String,
    ty: DeferredSqlType,
) -> crate::Result<()>
where
    M: MigrationMut,
{
    let current_migration = ms.current();
    let key = TypeKey::CustomType(name);
    current_migration.add_type(key, ty)
}

fn sqltype_from_name(name: &Ident) -> Option<SqlType> {
    let name = name.to_string();
    match name.as_ref() {
        "Bool" => Some(SqlType::Bool),
        "Int" => Some(SqlType::Int),
        "BigInt" => Some(SqlType::BigInt),
        "Real" => Some(SqlType::Real),
        "Text" => Some(SqlType::Text),
        #[cfg(feature = "datetime")]
        "Timestamp" => Some(SqlType::Timestamp),
        "Blob" => Some(SqlType::Blob),
        _ => None,
    }
}

#[derive(Debug)]
struct CompilerErrorMsg {
    ts: TokenStream2,
}
impl CompilerErrorMsg {
    fn new(ts: TokenStream2) -> Self {
        CompilerErrorMsg { ts }
    }
}
impl From<TokenStream2> for CompilerErrorMsg {
    fn from(ts: TokenStream2) -> Self {
        CompilerErrorMsg::new(ts)
    }
}