mongo_orm/
macros.rs

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
#[macro_export]
macro_rules! mongo_entity {
    ($name:ident, { $($field_name:ident : $field_type:ty),* $(,)? }) => {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        pub struct $name {
            #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
            pub id: Option<ObjectId>,
            $(pub $field_name: $field_type,)*
        }

        impl $name {
            // Constructor without ID
            pub fn new($($field_name: $field_type),*) -> Self {
                Self {
                    id: None,
                    $($field_name),*
                }
            }

            // Constructor with ID
            pub fn with_id(id: ObjectId, $($field_name: $field_type),*) -> Self {
                Self {
                    id: Some(id),
                    $($field_name),*
                }
            }
        }
    };
}