| 1 | /* |
| 2 | Copyright ⓒ 2015 macro-attr contributors. |
| 3 | |
| 4 | Licensed under the MIT license (see LICENSE or <http://opensource.org |
| 5 | /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of |
| 6 | <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All |
| 7 | files in the project carrying such notice may not be copied, modified, |
| 8 | or distributed except according to those terms. |
| 9 | */ |
| 10 | #[macro_use] extern crate macro_attr; |
| 11 | extern crate rustc_serialize; |
| 12 | |
| 13 | macro_rules! StableEncodable { |
| 14 | ( |
| 15 | () $(pub)* enum $name:ident < $($tail:tt)* |
| 16 | ) => { |
| 17 | StableEncodable! { |
| 18 | @extract_gen_args (enum $name), |
| 19 | ($($tail)*) |
| 20 | -> bounds(), ty_clss(where) |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | ( |
| 25 | () $(pub)* enum $name:ident { $($body:tt)* } |
| 26 | ) => { |
| 27 | StableEncodable! { |
| 28 | @impl enum $name, |
| 29 | bounds(), |
| 30 | ty_clss(), |
| 31 | { $($body)* } |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | ( |
| 36 | () $(pub)* struct $name:ident { $($body:tt)* } |
| 37 | ) => { |
| 38 | StableEncodable! { |
| 39 | @impl struct $name, |
| 40 | bounds(), |
| 41 | ty_clss(), |
| 42 | { $($body)* } |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | ( |
| 47 | () $(pub)* struct $name:ident < $($tail:tt)* |
| 48 | ) => { |
| 49 | StableEncodable! { |
| 50 | @extract_gen_args (struct $name), |
| 51 | ($($tail)*) |
| 52 | -> bounds(), ty_clss(where) |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | ( |
| 57 | @impl enum $name:ident, |
| 58 | bounds($($bounds:tt)*), |
| 59 | ty_clss($($ty_clss:tt)*), |
| 60 | { $($body:tt)* } |
| 61 | ) => { |
| 62 | StableEncodable! { |
| 63 | @parse_variants (enum $name, bounds($($bounds)*), ty_clss($($ty_clss)*)), |
| 64 | 0usize, ($($body)*,) -> () |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | ( |
| 69 | @impl struct $name:ident, |
| 70 | bounds($($bounds:tt)*), |
| 71 | ty_clss($($ty_clss:tt)*), |
| 72 | { $($fnames:ident: $_ftys:ty),* $(,)* } |
| 73 | ) => { |
| 74 | StableEncodable! { |
| 75 | @as_item |
| 76 | impl<$($bounds)*> rustc_serialize::Encodable for $name<$($bounds)*> |
| 77 | $($ty_clss)* { |
| 78 | fn encode<StableEncodableEncoder>( |
| 79 | &self, |
| 80 | s: &mut StableEncodableEncoder |
| 81 | ) -> Result<(), StableEncodableEncoder::Error> |
| 82 | where StableEncodableEncoder: rustc_serialize::Encoder { |
| 83 | const NUM_FIELDS: usize = StableEncodable!(@count_tts $($fnames)*); |
| 84 | try!(s.emit_struct(stringify!($name), NUM_FIELDS, |s| { |
| 85 | // Poor man's enumerate!($($fnames)): |
| 86 | let mut idx = 0; |
| 87 | $( |
| 88 | try!(s.emit_struct_field(stringify!($fnames), idx, |s| { |
| 89 | self.$fnames.encode(s) |
| 90 | })); |
| 91 | idx += 1; |
| 92 | )* |
| 93 | let _ = idx; |
| 94 | Ok(()) |
| 95 | })); |
| 96 | Ok(()) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | (@as_item $i:item) => {$i}; |
| 103 | |
| 104 | ( |
| 105 | @extract_gen_args ($kind:ident $name:ident), |
| 106 | (> { $($tail:tt)* }) |
| 107 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 108 | ) => { |
| 109 | StableEncodable! { |
| 110 | @impl $kind $name, |
| 111 | bounds($($bounds)*), |
| 112 | ty_clss($($ty_clss)*), |
| 113 | { $($tail)* } |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | ( |
| 118 | @extract_gen_args $fixed:tt, |
| 119 | ($ty_name:ident: $($tail)*) |
| 120 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 121 | ) => { |
| 122 | StableEncodable! { |
| 123 | @skip_inline_bound $fixed, |
| 124 | ($($tail)*) |
| 125 | -> bounds($($bounds)* $ty_name:), |
| 126 | ty_clss($($ty_clss)* $ty_name: ::rustc_serialize::Encodable,) |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | ( |
| 131 | @extract_gen_args $fixed:tt, |
| 132 | ($ty_name:ident $($tail:tt)*) |
| 133 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 134 | ) => { |
| 135 | StableEncodable! { |
| 136 | @extract_gen_args $fixed, |
| 137 | ($($tail)*) |
| 138 | -> bounds($($bounds)* $ty_name), |
| 139 | ty_clss($($ty_clss)* $ty_name: ::rustc_serialize::Encodable,) |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | ( |
| 144 | @extract_gen_args $fixed:tt, |
| 145 | (, $($tail:tt)*) |
| 146 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 147 | ) => { |
| 148 | StableEncodable! { |
| 149 | @extract_gen_args $fixed, |
| 150 | ($($tail)*) |
| 151 | -> bounds($($bounds)* ,), ty_clss($($ty_clss)*) |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | ( |
| 156 | @extract_gen_args $fixed:tt, |
| 157 | ($lt:tt $($tail:tt)*) |
| 158 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 159 | ) => { |
| 160 | StableEncodable! { |
| 161 | @extract_gen_args $fixed, |
| 162 | ($($tail)*) |
| 163 | -> bounds($($bounds)* $lt), ty_clss($($ty_clss)*) |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | ( |
| 168 | @skip_inline_bound $fixed:tt, |
| 169 | (, $($tail:tt)*) |
| 170 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 171 | ) => { |
| 172 | StableEncodable! { |
| 173 | @extract_gen_args $fixed, |
| 174 | ($($tail)*) |
| 175 | -> bounds($($bounds)* ,), ty_clss($($ty_clss)*) |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | ( |
| 180 | @skip_inline_bound $fixed:tt, |
| 181 | (> { $($tail:tt)* }) |
| 182 | -> bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*) |
| 183 | ) => { |
| 184 | StableEncodable! { |
| 185 | @impl $fixed, |
| 186 | bounds($($bounds)*), |
| 187 | ty_clss($($ty_clss)*), |
| 188 | { $($tail)* } |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | ( |
| 193 | @parse_variants (enum $name:ident, bounds($($bounds:tt)*), ty_clss($($ty_clss:tt)*)), |
| 194 | $_id:expr, ($(,)*) -> ($($variants:tt)*) |
| 195 | ) => { |
| 196 | StableEncodable! { |
| 197 | @as_item |
| 198 | impl<$($bounds)*> rustc_serialize::Encodable for $name<$($bounds)*> |
| 199 | $($ty_clss)* { |
| 200 | fn encode<StableEncodableEncoder>( |
| 201 | &self, |
| 202 | s: &mut StableEncodableEncoder) |
| 203 | -> Result<(), StableEncodableEncoder::Error> |
| 204 | where StableEncodableEncoder: rustc_serialize::Encoder { |
| 205 | s.emit_enum(stringify!($name), |s| { |
| 206 | $( |
| 207 | StableEncodable!(@encode_variant $name, $variants, self, s); |
| 208 | )* |
| 209 | unreachable!(); |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | ( |
| 217 | @parse_variants $fixed:tt, |
| 218 | $id:expr, ($var_name:ident, $($tail:tt)*) -> ($($variants:tt)*) |
| 219 | ) => { |
| 220 | StableEncodable! { |
| 221 | @parse_variants $fixed, |
| 222 | ($id + 1usize), ($($tail)*) -> ($($variants)* ($var_name, $id)) |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | ( |
| 227 | @parse_variants $fixed:tt, |
| 228 | $id:expr, ($var_name:ident($(,)*), $($tail:tt)*) -> ($($variants:tt)*) |
| 229 | ) => { |
| 230 | StableEncodable! { |
| 231 | @parse_variants $fixed, |
| 232 | ($id + 1usize), ($($tail)*) -> ($($variants)* |
| 233 | ($var_name, $id)) |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | ( |
| 238 | @parse_variants $fixed:tt, |
| 239 | $id:expr, ($var_name:ident($_vta:ty), $($tail:tt)*) -> ($($variants:tt)*) |
| 240 | ) => { |
| 241 | StableEncodable! { |
| 242 | @parse_variants $fixed, |
| 243 | ($id + 1usize), ($($tail)*) -> ($($variants)* |
| 244 | ($var_name, $id, (a))) |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | ( |
| 249 | @parse_variants $fixed:tt, |
| 250 | $id:expr, ($var_name:ident($_vta:ty, $_vtb:ty), $($tail:tt)*) -> ($($variants:tt)*) |
| 251 | ) => { |
| 252 | StableEncodable! { |
| 253 | @parse_variants $fixed, |
| 254 | ($id + 1usize), ($($tail)*) -> ($($variants)* |
| 255 | ($var_name, $id, (a, b))) |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | ( |
| 260 | @parse_variants $fixed:tt, |
| 261 | $id:expr, ($var_name:ident($_vta:ty, $_vtb:ty, $_vtc:ty), $($tail:tt)*) -> ($($variants:tt)*) |
| 262 | ) => { |
| 263 | StableEncodable! { |
| 264 | @parse_variants $fixed, |
| 265 | ($id + 1usize), ($($tail)*) -> ($($variants)* |
| 266 | ($var_name, $id, (a, b, c))) |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | ( |
| 271 | @parse_variants $fixed:tt, |
| 272 | $id:expr, ($var_name:ident { $($vfn:ident: $_vft:ty),* $(,)* }, $($tail:tt)*) -> ($($variants:tt)*) |
| 273 | ) => { |
| 274 | StableEncodable! { |
| 275 | @parse_variants $fixed, |
| 276 | ($id + 1usize), ($($tail)*) -> ($($variants)* |
| 277 | ($var_name, $id, {$($vfn),*})) |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | ( |
| 282 | @encode_variant $name:ident, |
| 283 | ($var_name:ident, $var_id:expr), |
| 284 | $self_:expr, $s:ident |
| 285 | ) => { |
| 286 | { |
| 287 | if let $name::$var_name = *$self_ { |
| 288 | return $s.emit_enum_variant(stringify!($var_name), $var_id, 0, |_| Ok(())); |
| 289 | } |
| 290 | } |
| 291 | }; |
| 292 | |
| 293 | ( |
| 294 | @encode_variant $name:ident, |
| 295 | ($var_name:ident, $var_id:expr, ($($tup_elems:ident),*)), |
| 296 | $self_:expr, $s:ident |
| 297 | ) => { |
| 298 | { |
| 299 | if let $name::$var_name($(ref $tup_elems),*) = *$self_ { |
| 300 | return $s.emit_enum_variant( |
| 301 | stringify!($var_name), |
| 302 | $var_id, |
| 303 | StableEncodable!(@count_tts $($tup_elems)*), |
| 304 | |s| { |
| 305 | let mut idx = 0; |
| 306 | $( |
| 307 | try!(s.emit_enum_variant_arg(idx, |s| $tup_elems.encode(s))); |
| 308 | idx += 1; |
| 309 | )* |
| 310 | let _ = idx; |
| 311 | Ok(()) |
| 312 | } |
| 313 | ); |
| 314 | } |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | ( |
| 319 | @encode_variant $name:ident, |
| 320 | ($var_name:ident, $var_id:expr, {$($str_fields:ident),*}), |
| 321 | $self_:expr, $s:ident |
| 322 | ) => { |
| 323 | { |
| 324 | if let $name::$var_name { $(ref $str_fields),* } = *$self_ { |
| 325 | return $s.emit_enum_struct_variant( |
| 326 | stringify!($var_name), |
| 327 | $var_id, |
| 328 | StableEncodable!(@count_tts $($str_fields)*), |
| 329 | |s| { |
| 330 | let mut idx = 0; |
| 331 | $( |
| 332 | try!(s.emit_enum_struct_variant_field( |
| 333 | stringify!($str_fields), |
| 334 | idx, |
| 335 | |s| $str_fields.encode(s) |
| 336 | )); |
| 337 | idx += 1; |
| 338 | )* |
| 339 | let _ = idx; |
| 340 | Ok(()) |
| 341 | } |
| 342 | ); |
| 343 | } |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | (@count_tts) => {0usize}; |
| 348 | (@count_tts $_tt:tt $($tail:tt)*) => {1usize + StableEncodable!(@count_tts $($tail)*)}; |
| 349 | } |
| 350 | |
| 351 | macro_attr! { |
| 352 | #[derive(Debug, StableEncodable!)] |
| 353 | struct LazyEg<A> { a: A, b: i32, c: (u8, u8, u8) } |
| 354 | } |
| 355 | |
| 356 | macro_attr! { |
| 357 | #[derive(Clone, StableEncodable!)] |
| 358 | enum Wonky<S> { Flim, Flam, Flom(i32), Bees { say: S } } |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn test_stable_encodable() { |
| 363 | macro_rules! json { |
| 364 | ($e:expr) => (rustc_serialize::json::encode(&$e).unwrap()); |
| 365 | } |
| 366 | |
| 367 | let lazy_eg = LazyEg { |
| 368 | a: String::from("Oh hai!"), |
| 369 | b: 42, |
| 370 | c: (1, 3, 0), |
| 371 | }; |
| 372 | assert_eq!(&*json!(lazy_eg), r#"{"a":"Oh hai!","b":42,"c":[1,3,0]}"#); |
| 373 | |
| 374 | assert_eq!(&*json!(Wonky::Flim::<()>), r#""Flim""#); |
| 375 | assert_eq!(&*json!(Wonky::Flam::<()>), r#""Flam""#); |
| 376 | assert_eq!(&*json!(Wonky::Flom::<()>(42)), r#"{"variant":"Flom","fields":[42]}"#); |
| 377 | assert_eq!(&*json!(Wonky::Bees{say:"aaaaah!"}), r#"{"variant":"Bees","fields":["aaaaah!"]}"#); |
| 378 | } |