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
/// Creates the Database struct with the fitting logic. Use `open` for creating/opening one
/// at a specified `path` or use `open_in_memory` to only use one in memory.
///
/// Lock the `AtomicDatabase` using `read()` / `write()` to access and change it values.
/// The saving of changes will be applied after the used variables are dropped.
///
/// ```
/// use light_magic::db;
///
/// db! {
///     // `Table` is the identifier that this will use the builtin table type
///     // `User` is the table name
///     // `{...}` is the table data
///     // the first field, like here `id`, is the `primary_key`
///     Table<User> => { id: usize, name: String, kind: String },
///     // to not use the builtin table type use `Custom` as the identifier of the table
///     // using `:` after the table name you can add your own derives
///     // like here `PartialEq`
///     Custom<Criminal: PartialEq> => { user_name: String, entry: String }
/// }
/// ```
#[macro_export]
macro_rules! db {
    (
        $(
            $table_ty:ident<$table:ty $( : $($derive:ident),* )?> => {
                $($field_name:ident : $field_ty:ty),*
            }
        ),* $(,)?
    ) => {
        use $crate::serde::{Serialize, Deserialize};
        use $crate::atomic::DataStore;
        use $crate::paste::paste;

        paste! {
            /// The Database Struct
            #[derive(Default, Debug, Serialize, Deserialize)]
            pub struct Database {
                $(
                        #[doc = "The " $table:camel " Table"]
                        pub [<$table:snake>]: db!(@expand_table_ty $table_ty, db!(@get_first_type $($field_name : $field_ty),*), [<$table:camel>]),
                )*
            }

            impl<'a> DataStore for Database {}

            $(
                #[derive(Default, Debug, Clone, Serialize, Deserialize, $( $( $derive),*)?)]
                pub struct [<$table:camel>] {
                    $(
                        pub $field_name: $field_ty,
                    )*
                }

                db!(@impls $table_ty, [<$table:camel>], $($field_name : $field_ty),*);
            )*
        }
    };

    // Creating impls for specific table types
    (@impls Table, $table_name:ident, $($field_name:ident : $field_ty:ty),*) => {
        impl $crate::table::Matches for $table_name {
            fn matches(&self, query: &str) -> bool {
                $(
                    if format!("{:?}", self.$field_name).to_lowercase().contains(&query.to_lowercase()) {
                        return true;
                    }
                )*
                false
            }
        }

        impl $crate::table::FirstField for $table_name {
            type FieldType = db!(@get_first_type $($field_name : $field_ty),*);

            fn first_field(&self) -> &Self::FieldType {
                &db!(@get_first_name self, $($field_name),*)
            }
        }
    };
    (@impls Custom, $table_name:ident, $($field_name:ident : $field_ty:ty),*) => {};

    // Helper for expanding the table type conditionally
    (@expand_table_ty Table, $first_type:ty, $table_name:ident) => {
        $crate::table::Table<$first_type, $table_name>
    };
    (@expand_table_ty Custom, $first_type:ty, $table_name:ident) => {
        $table_name
    };

    // Helper for getting the first name of a struct
    (@get_first_name $value:expr, $first_name:ident, $($rest_name:ident),*) => {
        $value.$first_name
    };
    (@get_first_name $value:expr, $first_name:ident) => {
        $value.$first_name
    };

    // Helper for getting the first type of a struct
    (@get_first_type $first_name:ident : $first_ty:ty, $($rest_name:ident : $rest_ty:ty),*) => {
        $first_ty
    };
    (@get_first_type $first_name:ident : $first_ty:ty) => {
        $first_ty
    };
}

/// Joins Data of different `Tables` in the Database together
///
/// ```
/// use light_magic::{db, join};
///
/// db! {
///     Table<User> => { id: usize, name: String, kind: String },
///     Table<Criminal> => { user_name: String, entry: String }
/// }
///
/// let db = Database::open_in_memory();
/// // Firstly specify the Database which should be used, then the key,
/// // and lastly the joined items with the field which will be compared with the key
/// let joined = join!(db.read(), "Nils", user => name, criminal => user_name);
/// ```
#[macro_export]
macro_rules! join {
    ($db:expr, $key:expr, $($table:ident => $field:ident),* $(,)?) => {{
        $crate::paste::paste! {
            let mut combined_results = Vec::new();

            $(
                let [<$table _results>]: Vec<_> = $db.$table.values()
                    .filter(|val| val.$field == $key)
                    .cloned()
                    .collect();
            )*

            let len = vec![$([<$table _results>].len()),*].into_iter().min().unwrap_or(0);

            for i in 0..len {
                combined_results.push((
                    $([<$table _results>][i].clone(),)*
                ));
            }

            combined_results
        }
    }}
}