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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
//! This file groups all macros used throughout the library. Since interop with //! javascript often gets unwieldy, macros are used extensively to follow DRY //! principles. //! //! The documentation tries to give a good enough picture of how the macros //! should be used, but are in no way a formal description. For a better //! understanding of the `macro_rule!` arcane yet simple syntax, have a look //! at [`Macros, A Methodical Introduction`][macro-book] //! //! [macro-book]: https://danielkeep.github.io/tlborm/book/mbe-README.html pub use stdweb::{ __js_deserializable_serde_boilerplate, __js_raw_asm, __js_serializable_boilerplate, __js_serializable_serde_boilerplate, _js_impl, js, js_deserializable, js_serializable, }; /// Used to get data from a javascript reference back into rust code. /// /// Macro syntax (`$name` are expressions): /// /// ```ignore /// js_unwrap!($jsExpr) /// ``` /// /// For reference, `js!()` is a macro that returns a `stdweb::Value` enum. /// See <https://docs.rs/stdweb/0.4.8/stdweb/enum.Value.html>. /// /// Here, `js_unwrap!()` takes any valid javascript expression (expresses a /// value) and will attempt conversion to the receiving variable type using /// `try_into`. For example: /// ``` /// let s: u32 = js_unwrap!(Game.time); /// ``` /// /// This will be be converted to /// ``` /// let s: u32 = js!(return Game.time;).try_into().expect('Some Err Msg'); /// ``` /// /// Since `Game.time` returns a javascript `number`, `js!` spits out a /// `stdweb::Value::Number` which is convertible to a u32 and should work /// without problem. /// /// A non-exhaustive list of types that work (use your judgement) /// /// js | rust /// ------------------------ /// Number | u32, i32, f32 /// String | String /// bool | Bool /// /// For the full list, see the documentation for [`stdweb::unstable::TryFrom`]. /// (If unavailable: <https://docs.rs/stdweb/0.4.8/stdweb/unstable/trait.TryFrom.html> ) /// /// Note: for unwrapping reference types, use [`js_unwrap_ref!`] to avoid /// instanceof checks. macro_rules! js_unwrap { ($($code:tt)*) => ( crate::traits::TryInto::try_into(js! { return $($code)*; }) .expect(concat!("js_unwrap at ", line!(), " in ", file!())) ) } /// Macro similar to [`js_unwrap!`], but with fewer `instanceof` checks. /// /// # Example /// /// ```ignore /// let x: Creep = js_unwrap_ref!(Game.creeps.John); /// ``` /// /// This will generate code /// /// ``` /// let x: Creep = js!({ return Game.creeps.John; }).cast_expected_type().expect(...); /// ``` /// /// `cast_expected_type` will ensure that the return value is a /// [`stdweb::Reference`], but it won't do any more than that. If the JavaScript /// behaves incorrectly and returns something other than a Creep, and the /// `"check-all-casts"` feature is not enabled, it will silently make a /// [`screeps::Creep`] containing the wrong value which will fail when used. macro_rules! js_unwrap_ref { ($($code:tt)*) => ( crate::traits::IntoExpectedType::into_expected_type(js! { return $($code)*; }) .expect(concat!("js_unwrap_ref at ", line!(), " in ", file!())) ) } /// Creates a getter method to unwrap a field of a javascript object. /// /// Macro Syntax (`$name` are expressions): /// /// ```ignore /// get_from_js!($method_name -> {$js_statement} -> $rust_type) /// get_from_js!($method_name($param1, $param2, ...) -> {$js_statement} -> $rust_type) /// ``` /// /// Building on top of `js_unwrap!()`, this creates an accessor to a javascript /// object method or attribute. /// /// # Example /// ``` /// get_from_js!( /// limit -> { /// Game.cpu.limit /// } -> u32 /// ) /// ``` /// /// Will become: /// ``` /// pub fn limit() -> u32 { /// js_unwrap!(Game.cpu.limit) /// } /// ``` /// which would best be used inside the implementation for `cpu` in this case. macro_rules! get_from_js { ($name:ident -> { $js_side:expr } -> $rust_ret_type:ty) => ( get_from_js!($name() -> { $js_side } -> $rust_ret_type); ); ( $name:ident( $($param_ident:ident: $param_ty:ty),* ) -> { $($js_side:tt)* } -> $rust_ret_type:ty ) => ( pub fn $name( $($param_ident: $param_ty),* ) -> $rust_ret_type { js_unwrap!($($js_side)*) } ) } /// Macro used to encapsulate all screeps game objects /// /// Macro syntax: /// /// ``` /// reference_wrapper! { /// #[reference(instance_of = "Creep")] /// Creep, /// #[reference(instance_of = "Room")], /// Room, /// // ... /// } /// ``` /// /// Screeps game objects, in javascript, can be accessed via stdweb's /// `Reference` object. For each ident `objJ` mentioned, this macro: /// /// - Creates a struct named `objX`; /// - Uses `#[derive(Clone, ReferenceType)]` which implements these traits for /// `objX`: /// - `InstanceOf` /// - `AsRef<Reference>` /// - `ReferenceType` /// - `Into<Reference>` /// - `TryInto<Reference>` /// - `TryFrom<Reference>` /// - `TryFrom<&Reference>` /// - `TryFrom<Value>` /// - `TryFrom<&Value>` /// - Implements `FromExpectedType<Reference>` for `objJ` macro_rules! reference_wrappers { ( $( $(#[ $attr:meta ])* $name:ident ),* $(,)* ) => { $( #[derive(Clone, ReferenceType)] $( #[$attr] )* pub struct $name(Reference); impl crate::traits::FromExpectedType<Reference> for $name { fn from_expected_type(reference: Reference) -> Result<Self, ConversionError> { #[cfg(feature = "check-all-casts")] { $crate::traits::TryFrom::try_from(reference) } #[cfg(not(feature = "check-all-casts"))] { unsafe { Ok(stdweb::ReferenceType::from_reference_unchecked(reference)) } } } } )* }; } /// Automatically creates simple accessors to fields of screep objects /// /// On top of an object created from `reference_wrapper!`, this macro creates an /// implementation of the struct for a collection of fields from the screeps /// object. /// /// Method Syntax: /// /// ```ignore /// simple_accessor! { /// $struct_name; /// ($rust_method_name1 -> $js_field_name1 -> $rust_type1), /// ($rust_method_name2 -> $js_field_name2 -> $rust_type2), /// ... /// } /// ``` macro_rules! simple_accessors { ($struct_name:ident; $(($method:ident -> $prop:ident -> $ret:ty)),* $(,)*) => ( impl $struct_name { $( pub fn $method(&self) -> $ret { js_unwrap!(@{self.as_ref()}.$prop) } )* } ) } /// Macro for mass implementing `StructureProperties`, `PartialEq` and `Eq` for /// a type. /// /// Macro syntax: /// /// ```ignore /// impl_structure_properties!{ /// $struct1, /// $struct2, /// ... /// } /// ``` /// /// This macro accepts a comma-separated list of types on which to implement the /// unsafe `StructureProperties` trait on a screeps object. /// From that implementation, the type gets the `id` method which is used to /// implement `PartialEq` and `Eq`. /// /// # Safety /// The macro assumes that it is implementing the trait to a valid `Reference` /// (See `reference_wrapper` macro) which will support all `StructureProperties` /// methods. macro_rules! impl_structure_properties { ( $( $struct_name:ty ),+ ) => {$( unsafe impl StructureProperties for $struct_name {} )*}; } /// Implements `id` method for Structures and Creep /// /// This generates the implementation, for the structures given, of the `HasId`, /// `PartialEq` and `Eq` traits. The last two are implemented using the `id()` /// method. /// /// Macro Syntax: /// ```ignore /// impl_has_id! { /// $struct_name1; /// $struct_name2; /// ... /// } /// ``` macro_rules! impl_has_id { ($($struct_name:ty);* $(;)*) => {$( unsafe impl HasId for $struct_name {} impl PartialEq for $struct_name { fn eq(&self, other: &$struct_name) -> bool { self.id() == other.id() } } impl Eq for $struct_name {} )*}; } /// Implements action methods for creeps /// /// This macro is used to implement generic `creep` methods that returns a /// `ReturnCode`, a number indicating the status of the action requested. /// /// Macro Syntax: /// ```ignore /// creep_simple_generic_action!{ /// ($rust_method_name1($action_target_trait1) -> js_method_name1), /// ($rust_method_name2($action_target_trait2) -> js_method_name2), /// ... /// } /// ``` /// /// For this macro, the last comma is facultative. /// /// The generic comes from the fact that this implements the method to be able /// to target any object that conforms to the `action_target_trait` trait. macro_rules! creep_simple_generic_action { ($(($method:ident($trait:ident) -> $js_name:ident)),* $(,)*) => ( impl Creep { $( pub fn $method<T>(&self, target: &T) -> ReturnCode where T: ?Sized + $trait, { js_unwrap!(@{self.as_ref()}.$js_name(@{target.as_ref()})) } )* } ) } /// Implements action methods for creeps /// /// This macro is used to implement concrete `creep` methods that returns a /// `ReturnCode`, a number indicating the status of the action requested. /// /// Macro Syntax: /// ```ignore /// creep_simple_generic_action!{ /// ($rust_method_name1($target_type1) -> js_method_name1), /// ($rust_method_name2($target_type2) -> js_method_name2), /// ... /// } /// ``` /// /// For this macro, the last comma is facultative. /// /// The concrete comes from the fact that this implements the method to be able /// to target only the `type` given. macro_rules! creep_simple_concrete_action { ($(($method:ident($type:ty) -> $js_name:ident)),* $(,)*) => ( impl Creep { $( pub fn $method(&self, target: &$type) -> ReturnCode { js_unwrap!(@{self.as_ref()}.$js_name(@{target.as_ref()})) } )* } ) } /// Declares an item with a doc attribute computed by some macro expression. /// This allows documentation to be dynamically generated based on input. /// Necessary to work around https://github.com/rust-lang/rust/issues/52607. macro_rules! calculated_doc { ( $( #[doc = $doc:expr] $thing:item )* ) => ( $( #[doc = $doc] $thing )* ); } macro_rules! typesafe_find_constants { ( $( $constant_name:ident, $value:expr, $result:path; )* ) => ( $( calculated_doc! { #[doc = concat!( "Zero-sized constant representing the `FIND_", stringify!($constant_name), "` constant." )] #[allow(bad_style)] #[derive(Copy, Clone, Debug, Default)] pub struct $constant_name; } unsafe impl FindConstant for $constant_name { type Item = $result; #[inline] fn find_code(&self) -> i16 { $value } } )* ); } macro_rules! typesafe_look_constants { ( $($constant_name:ident, $value:expr, $result:path, $conversion_method:expr;)* ) => ( $( #[allow(bad_style)] pub struct $constant_name; unsafe impl LookConstant for $constant_name { type Item = $result; fn convert_and_check_items(reference: ::stdweb::Value) -> Vec<Self::Item> { ($conversion_method)(reference) .expect(concat!("LookConstant ", stringify!($constant_name), "expected correct type at ", line!(), " in ", file!())) } #[inline] fn look_code(&self) -> Look { $value } } )* ); } // Todo: this way of handling a return object isn't consistent with some others // used elsewhere (eg: signs) /// Creates accessors for the main game collections /// /// Macro syntax: /// ```ignore /// game_map_access!($rust_object_accessed1, $js_code_to_access1); /// ``` /// /// Best used inside a module. It builds four functions, `keys`, `values`, `get` /// and `hashmap`. For example, to retreive a vector of all creeps names: /// /// ``` /// screeps::game::creeps::keys(); /// ``` /// /// This macro defines functions for retreiving the `keys` (names) of the /// collection, the `values` as `rust_object_accessedX` and a single object /// via the `get` function. macro_rules! game_map_access { ($type:path, $js_inner:expr $(,)?) => { use std::collections::HashMap; use crate::{macros::*, objects}; calculated_doc! { #[doc = concat!("Retrieve the full `HashMap<String, ", stringify!($type), ">`.") ] pub fn hashmap() -> HashMap<String, $type> { js_unwrap!($js_inner) } } /// Retrieve the string keys of this object. pub fn keys() -> Vec<String> { js_unwrap!(Object.keys($js_inner)) } /// Retrieve all values in this object. pub fn values() -> Vec<$type> { js_unwrap_ref!(Object.values($js_inner)) } /// Retrieve a specific value by key. pub fn get(name: &str) -> Option<$type> { js_unwrap_ref!($js_inner[@{name}]) } }; } /// Match on all variants of `Structure` and do the same thing for each of them. macro_rules! match_structure_variants { ($source:expr, $name:ident => $action:expr) => { match $source { Structure::Container($name) => $action, Structure::Controller($name) => $action, Structure::Extension($name) => $action, Structure::Extractor($name) => $action, Structure::KeeperLair($name) => $action, Structure::Lab($name) => $action, Structure::Link($name) => $action, Structure::Nuker($name) => $action, Structure::Observer($name) => $action, Structure::PowerBank($name) => $action, Structure::PowerSpawn($name) => $action, Structure::Portal($name) => $action, Structure::Rampart($name) => $action, Structure::Road($name) => $action, Structure::Spawn($name) => $action, Structure::Storage($name) => $action, Structure::Terminal($name) => $action, Structure::Tower($name) => $action, Structure::Wall($name) => $action, } }; } /// Match on all variants of `StructureType` and construct `Structure` variants /// from the same code for each of them. macro_rules! construct_structure_variants { ($source:expr => $action:expr) => { match $source { StructureType::Container => Structure::Container($action), StructureType::Controller => Structure::Controller($action), StructureType::Extension => Structure::Extension($action), StructureType::Extractor => Structure::Extractor($action), StructureType::KeeperLair => Structure::KeeperLair($action), StructureType::Lab => Structure::Lab($action), StructureType::Link => Structure::Link($action), StructureType::Nuker => Structure::Nuker($action), StructureType::Observer => Structure::Observer($action), StructureType::PowerBank => Structure::PowerBank($action), StructureType::PowerSpawn => Structure::PowerSpawn($action), StructureType::Portal => Structure::Portal($action), StructureType::Rampart => Structure::Rampart($action), StructureType::Road => Structure::Road($action), StructureType::Spawn => Structure::Spawn($action), StructureType::Storage => Structure::Storage($action), StructureType::Terminal => Structure::Terminal($action), StructureType::Tower => Structure::Tower($action), StructureType::Wall => Structure::Wall($action), } }; } /// Match on all variants of `Structure`, doing something wrapped in Some() for /// some of them, and None for others. macro_rules! match_some_structure_variants { ($source:expr, { $($allowed:ident),* $(,)* }, $name:ident => $action:expr) => { match $source { $( Structure::$allowed($name) => Some($action), )* _ => None, } }; } /// Implements `Iterator` for `js_vec::IntoIter` or `js_vec::Iter`, using /// `FromExpectedType` and panicking on incorrect types. macro_rules! impl_js_vec_iterators_from_expected_type_panic { ($($name:ident $(<$single_life_param:lifetime>)*),* $(,)*) => { $( impl<$($single_life_param, )* T> Iterator for $name<$($single_life_param, )* T> where T: FromExpectedType<Value>, { type Item = T; /// Gets the next item. /// /// # Panics /// /// Panics if the type is incorrect. fn next(&mut self) -> Option<Self::Item> { if self.index as usize >= self.inner.len() { None } else { let index = self.index; self.index += 1; Some(js_unwrap_ref!(@{AsRef::<Reference>::as_ref(&self.inner)}[@{index}])) } } fn size_hint(&self) -> (usize, Option<usize>) { let length = self.inner.len(); (length, Some(length)) } } impl<$($single_life_param, )* T> ::std::iter::ExactSizeIterator for $name<$($single_life_param, )* T> where T: FromExpectedType<Value>, { fn len(&self) -> usize { self.inner.len() } } )* } } /// Implements `Iterator` for `js_vec::IntoIter` or `js_vec::Iter`. macro_rules! impl_js_vec_iterators_from_expected_type_with_result { ($($name:ident $(<$single_life_param:lifetime>)*),* $(,)*) => { $( impl<$($single_life_param, )* T> Iterator for $name<$($single_life_param, )* T> where T: FromExpectedType<Value>, { type Item = Result<T, ConversionError>; fn next(&mut self) -> Option<Self::Item> { if self.index as usize >= self.inner.len() { None } else { let index = self.index; self.index += 1; Some(FromExpectedType::from_expected_type( js!(@{AsRef::<Reference>::as_ref(&self.inner)}[@{index}]) )) } } fn size_hint(&self) -> (usize, Option<usize>) { let length = self.inner.len(); (length, Some(length)) } } impl<$($single_life_param, )* T> ::std::iter::ExactSizeIterator for $name<$($single_life_param, )* T> where T: FromExpectedType<Value>, { fn len(&self) -> usize { self.inner.len() } } )* } } /// Get a value from memory given a path, returning `None` if any thing along /// the way does not exist. /// /// # Examples /// /// Get a reference with type u32 at the path creeps.John.count. /// /// ```no_run /// #[macro_use] /// extern crate screeps; /// /// # fn main() { /// let mem = screeps::memory::root(); /// let val = mem_get!(mem.creeps.John.count.i32); /// # } /// ``` /// /// Get something using a variable path. /// /// ```no_run /// #[macro_use] /// extern crate screeps; /// /// # fn main() { /// let mem = screeps::memory::root(); /// let creep_name = "John"; /// let what_to_get = "count"; /// let val1 = mem_get!(mem.creeps[creep_name][what_to_get].i32); /// let val2 = mem_get!(mem.creeps[creep_name].count.i32); /// assert_eq!(val1, val2); /// # } /// ``` /// /// Accepted suffixes for type are methods that exist on `MemoryReference`, such /// as `num`, `int`, `string`, `bool`, `arr` and `dict`. #[macro_export] macro_rules! mem_get { // Macro entry point ($memory_reference:ident $($rest:tt)*) => { mem_get!(@so_far { Ok(Some(&$memory_reference)) } @rest $($rest)*) }; // Access the last part with a variable (@so_far { $reference_so_far:expr } @rest [ $final_part_variable:expr ] . $accessor:ident) => { $reference_so_far.and_then(|opt| match opt { Some(v) => v.$accessor($final_part_variable), None => Ok(None), }) }; // Access the last part with a hardcoded ident (@so_far { $reference_so_far:expr } @rest . $final_part:ident . $accessor:ident) => { $reference_so_far.and_then(|opt| match opt { Some(v) => v.$accessor(stringify!($final_part)), None => Ok(None), }) }; // Access the next (but not last) part with a variable (@so_far { $reference_so_far:expr } @rest [ $next_part_variable:expr ] $($rest:tt)+) => { mem_get!( @so_far { $reference_so_far.and_then(|opt| match opt { Some(v) => v.dict($next_part_variable), None => Ok(None), }) } @rest $($rest)* ) }; // Access the next (but not last) part with a hardcoded ident (@so_far { $reference_so_far:expr } @rest . $next_part:ident $($rest:tt)+) => { mem_get!( @so_far { $reference_so_far.and_then(|opt| match opt { Some(v) => v.dict(stringify!($next_part)), None => Ok(None), }) } @rest $($rest)* ) }; ($($not_valid:tt)*) => { compile_error!(concat!("Unexpected usage of mem_get! usage: ", stringify!($($not_valid)*))) } } /// Set a value in memory given a path, creating dicts for intermediate places /// if they do not exist. /// /// # Return /// /// This macro produces a `Result<(), ::screeps::memory::UnexpectedTypeError>`. /// The error path will trigger if any of the intermediate memory keys exist but /// are not dictionaries. /// /// # Examples /// /// Set Memory.creeps.John.count to 42. /// /// ```no_run /// #[macro_use] /// extern crate screeps; /// /// # fn main() { /// let mem = screeps::memory::root(); /// mem_set!(mem.creeps.John.count = 42).unwrap(); /// # } /// ``` /// /// Set something using a variable path. /// /// ```no_run /// #[macro_use] /// extern crate screeps; /// /// # fn main() { /// let mem = screeps::memory::root(); /// let creep_name = "John"; /// let what_to_set = "count"; /// mem_set!(mem.creeps[creep_name][what_to_set] = 51).unwrap(); /// mem_set!(mem.creeps[creep_name].count = 52).unwrap(); /// # } /// ``` #[macro_export] macro_rules! mem_set { // Macro entry point ($memory_reference:ident $($rest:tt)*) => { mem_set!( @path_so_far { stringify!($memory_reference) } @so_far { Ok(&$memory_reference) } @rest $($rest)* ) }; // Perform the set given a variable for the last part of the path. ( @path_so_far { $path_so_far:expr } @so_far { $reference_so_far:expr } @rest [ $final_part_variable:expr ] = $value:expr ) => { $reference_so_far.map(|v| v.set($final_part_variable, $value)) }; // Perform the set given a hardcoded ident for the last part of the path. ( @path_so_far { $path_so_far:expr } @so_far { $reference_so_far:expr } @rest . $final_part:ident = $value:expr ) => { $reference_so_far.map(|v| v.set(stringify!($final_part), $value)) }; // Access the next (but not last) part with a variable ident. ( @path_so_far { $path_so_far:expr } @so_far { $reference_so_far:expr } @rest [ $next_part_variable:expr ] $($rest:tt)+ ) => { mem_set!( @path_so_far { concat!($path_so_far, ".", stringify!([$next_part_variable])) } @so_far { $reference_so_far.and_then(|v| v.dict_or_create($next_part_variable)) } @rest $($rest)* ) }; // Access the next (but not last) part with a hardcoded ident ( @path_so_far { $path_so_far:expr } @so_far { $reference_so_far:expr } @rest . $next_part:ident $($rest:tt)+ ) => { mem_set!( @path_so_far { concat!($path_so_far, ".", stringify!($next_part)) } @so_far { $reference_so_far.and_then(|v| v.dict_or_create(stringify!($next_part))) } @rest $($rest)* ) }; ($($not_valid:tt)*) => { compile_error!(concat!("Unexpected usage of mem_set! usage: ", stringify!($($not_valid)*))) } }