mongo_orm/
macros.rs

1#[macro_export]
2macro_rules! mongo_entity {
3    ($name:ident, { $($field_name:ident : $field_type:ty),* $(,)? }) => {
4        #[derive(Debug, Serialize, Deserialize, PartialEq)]
5        pub struct $name {
6            #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
7            pub id: Option<ObjectId>,
8            $(pub $field_name: $field_type,)*
9        }
10
11        impl $name {
12            // Constructor without ID
13            pub fn new($($field_name: $field_type),*) -> Self {
14                Self {
15                    id: None,
16                    $($field_name),*
17                }
18            }
19
20            // Constructor with ID
21            pub fn with_id(id: ObjectId, $($field_name: $field_type),*) -> Self {
22                Self {
23                    id: Some(id),
24                    $($field_name),*
25                }
26            }
27        }
28    };
29}