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
/// Statically check presence of field in a given struct and stringify it. /// /// Note that it sadly won't work with `#[serde(rename = "...")]` and `#[serde(rename_all = "...")]`. /// /// # Example /// /// ``` /// use mongodm::mongo::bson::doc; /// use mongodm::field; /// use mongodm::operator::*; /// /// struct MyModel { /// foo: i64, /// bar: i64, /// lorem: String, /// } /// /// // Statically checked /// let a = doc! { /// And: [ /// { field!(foo in MyModel): { Exists: true } }, /// { /// Or: [ /// { field!(bar in MyModel): { GreaterThan: 100 } }, /// { field!(lorem in MyModel): "ipsum" } /// ] /// } /// ] /// }; /// /// // Hardcoded strings /// let b = doc! { /// "$and": [ /// { "foo": { "$exists": true } }, /// { /// "$or": [ /// { "bar": { "$gt": 100 } }, /// { "lorem": "ipsum" } /// ] /// } /// ] /// }; /// /// // Generated document are identicals /// assert_eq!(a, b); /// ``` /// /// If the field doesn't exist, compilation will fail. /// /// ```compile_fail /// use mongodm::mongo::bson::doc; /// use mongodm::field; /// use mongodm::operator::*; /// /// struct MyModel { /// bar: i64, /// } /// /// // Doesn't compile because `foo` isn't a member of `MyModel` /// doc! { field!(foo in MyModel): 0 }; /// ``` #[macro_export] macro_rules! field { ( $field:ident in $type:path ) => {{ #[allow(unknown_lints, unneeded_field_pattern)] const _: fn() = || { let $type { $field: _, .. }; }; stringify!($field) }}; } /// Shorthand for `field!`. /// /// # Example /// /// ``` /// use mongodm::mongo::bson::doc; /// use mongodm::f; /// use mongodm::operator::*; /// /// struct MyModel { /// foo: i64, /// bar: i64, /// lorem: String, /// } /// /// // Statically checked /// let a = doc! { /// And: [ /// { f!(foo in MyModel): { Exists: true } }, /// { /// Or: [ /// { f!(bar in MyModel): { GreaterThan: 100 } }, /// { f!(lorem in MyModel): "ipsum" } /// ] /// } /// ] /// }; /// /// // Hardcoded strings /// let b = doc! { /// "$and": [ /// { "foo": { "$exists": true } }, /// { /// "$or": [ /// { "bar": { "$gt": 100 } }, /// { "lorem": "ipsum" } /// ] /// } /// ] /// }; /// /// // Generated document are identicals /// assert_eq!(a, b); /// ``` #[macro_export] macro_rules! f { ( $field:ident in $type:path ) => {{ $crate::field!($field in $type) }}; }