pg_worm_derive/
lib.rs

1mod parse;
2
3use darling::FromDeriveInput;
4use proc_macro::{self, TokenStream};
5use syn::parse_macro_input;
6
7use parse::ModelInput;
8
9/// Automatically implement `Model` for your struct.
10///
11/// ## Attributes
12///  * `table` - for structs:
13///      - `table_name: String`: optional. Overwrites the table name
14///  * `column` - for struct fields:
15///      - `dtype: String`: optional. Overwrites the postgres datatype
16///      - `unique: bool`: optional, default: `false`. Enables the `unqiue` constraint.
17///      - `auto: bool`: optional, default: `false`. This autogenerated the
18///        values.
19///      - `column_name: String`: optional. Overwrites the column name.
20#[proc_macro_derive(Model, attributes(table, column))]
21pub fn derive(input: TokenStream) -> TokenStream {
22    let input = ModelInput::from_derive_input(&parse_macro_input!(input)).unwrap();
23
24    let output = input.impl_everything();
25
26    output.into()
27}
28
29#[cfg(test)]
30mod tests {
31    use darling::FromDeriveInput;
32    use syn::parse_str;
33
34    use crate::parse::ModelInput;
35
36    #[test]
37    fn test() {
38        let input = r#"
39            #[derive(Model)]
40            struct Book {
41                #[column(primary_key, auto)]
42                id: i64,
43                title: String
44            }
45        "#;
46        let tokens = parse_str(input).unwrap();
47        let parsed_input = ModelInput::from_derive_input(&tokens).unwrap();
48        let _output = parsed_input.impl_everything();
49    }
50}