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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/// 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" } /// ] /// }, /// // dollar and double dollar signs can inserted by prefixing with @ /// { field!(@foo in MyModel): field!(@@bar in MyModel) } /// ] /// }; /// /// // Hardcoded strings /// let b = doc! { /// "$and": [ /// { "foo": { "$exists": true } }, /// { /// "$or": [ /// { "bar": { "$gt": 100 } }, /// { "lorem": "ipsum" } /// ] /// }, /// { "$foo": "$$bar" } /// ] /// }; /// /// // Generated document are identicals /// assert_eq!(a, b); /// ``` /// /// Nested structs /// ``` /// use mongodm::mongo::bson::doc; /// use mongodm::field; /// /// struct Foo { /// bar: Bar, /// } /// /// struct Bar { /// lorem: String, /// baz: Baz, /// } /// /// struct Baz { /// dolor: String, /// } /// /// assert_eq!( /// doc! { field!((bar in Foo).(lorem in Bar)): "ipsum" }, /// doc! { "bar.lorem": "ipsum" }, /// ); /// /// assert_eq!( /// doc! { field!(@@(bar in Foo).(baz in Bar).(dolor in Baz)): "sit amet" }, /// doc! { "$$bar.baz.dolor": "sit amet" }, /// ); /// ``` /// /// If the field doesn't exist, compilation will fail. /// /// ```compile_fail /// use mongodm::mongo::bson::doc; /// use mongodm::field; /// /// struct MyModel { /// foo: i64, /// bar: Bar, /// } /// /// struct Bar { /// baz: i64, /// } /// /// // Doesn't compile because `baz` isn't a member of `MyModel` /// doc! { field!(baz in MyModel): 0 }; /// /// // Doesn't compile because `foo` isn't a member of `Bar` /// doc! { field!((bar in MyModel).(foo in Bar)): 0 }; /// ``` #[macro_export] macro_rules! field { ( $field:ident in $type:path ) => {{ #[allow(unknown_lints, unneeded_field_pattern)] const _: fn() = || { let $type { $field: _, .. }; }; stringify!( $field ) }}; ( @ $field:ident in $type:path ) => {{ #[allow(unknown_lints, unneeded_field_pattern)] const _: fn() = || { let $type { $field: _, .. }; }; concat!("$", stringify!($field)) }}; ( @ @ $field:ident in $type:path ) => {{ #[allow(unknown_lints, unneeded_field_pattern)] const _: fn() = || { let $type { $field: _, .. }; }; concat!("$$", stringify!($field)) }}; ( ( $field:ident in $type:path ) ) => {{ $crate::field!( $field in $type ) }}; // FIXME: ideally we want a compile-time string instead of format! ( ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ format!("{}.{}", $crate::field!( $field in $type ), $crate::field!( $( $rest ).+ )) }}; ( @ ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ format!("${}.{}", $crate::field!( $field in $type ), $crate::field!( $( $rest ).+ )) }}; ( @ @ ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ format!("$${}.{}", $crate::field!( $field in $type ), $crate::field!( $( $rest ).+ )) }}; } /// 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" } /// ] /// }, /// // dollar and double dollar signs can inserted by prefixing with @ /// { f!(@foo in MyModel): f!(@@bar in MyModel) } /// ] /// }; /// /// // Hardcoded strings /// let b = doc! { /// "$and": [ /// { "foo": { "$exists": true } }, /// { /// "$or": [ /// { "bar": { "$gt": 100 } }, /// { "lorem": "ipsum" } /// ] /// }, /// { "$foo": "$$bar" } /// ] /// }; /// /// // Generated document are identicals /// assert_eq!(a, b); /// ``` #[macro_export] macro_rules! f { ( $field:ident in $type:path ) => {{ $crate::field!($field in $type) }}; ( @ $field:ident in $type:path ) => {{ $crate::field!( @ $field in $type) }}; ( @ @ $field:ident in $type:path ) => {{ $crate::field!( @ @ $field in $type) }}; ( ( $field:ident in $type:path ) ) => {{ $crate::field!( ( $field in $type ) ) }}; ( ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ $crate::field!( ( $field in $type ) . $( $rest ).+ ) }}; ( @ ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ $crate::field!( @ ( $field in $type ) . $( $rest ).+ ) }}; ( @ @ ( $field:ident in $type:path ) . $( $rest:tt ).+ ) => {{ $crate::field!( @ @ ( $field in $type ) . $( $rest ).+ ) }}; }