diff options
author | HampusM <hampus@hampusmat.com> | 2023-03-18 17:14:42 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-03-18 17:15:30 +0100 |
commit | c48271aef7e6b0819c497f302127c161845a83d7 (patch) | |
tree | a18d7b5fc8e017b4b7e0917a55534b28a01fe57d /macros/src/mock_input.rs | |
parent | 2ca8017deebe7bfe5aac368aead777a2c4910ca2 (diff) |
refactor: rewrite the mock macro as a procedural macro
Diffstat (limited to 'macros/src/mock_input.rs')
-rw-r--r-- | macros/src/mock_input.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/macros/src/mock_input.rs b/macros/src/mock_input.rs new file mode 100644 index 0000000..379c342 --- /dev/null +++ b/macros/src/mock_input.rs @@ -0,0 +1,51 @@ +use syn::parse::{Parse, ParseStream}; +use syn::{braced, Ident, Token, TraitItem, TypePath, WhereClause}; + +pub struct MockInput +{ + pub mock: Ident, + pub mocked_trait: TypePath, + pub items: Vec<TraitItem>, +} + +impl Parse for MockInput +{ + fn parse(input: ParseStream) -> Result<Self, syn::Error> + { + let mock = input.parse()?; + + let _generics = input.parse::<Option<WhereClause>>()?; + + let _braced_content; + + let _brace = braced!(_braced_content in input); + + input.parse::<Token![impl]>()?; + + let mocked_trait = input.parse()?; + + input.parse::<Token![for]>()?; + + let impl_target = input.parse::<Ident>()?; + + if impl_target != mock { + return Err(input.error("Expected this to be the mock")); + } + + let content; + + braced!(content in input); + + let mut items = Vec::new(); + + while !content.is_empty() { + items.push(content.parse()?); + } + + Ok(Self { + mock, + mocked_trait, + items, + }) + } +} |