From c48271aef7e6b0819c497f302127c161845a83d7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 18 Mar 2023 17:14:42 +0100 Subject: refactor: rewrite the mock macro as a procedural macro --- macros/src/mock_input.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 macros/src/mock_input.rs (limited to 'macros/src/mock_input.rs') 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, +} + +impl Parse for MockInput +{ + fn parse(input: ParseStream) -> Result + { + let mock = input.parse()?; + + let _generics = input.parse::>()?; + + let _braced_content; + + let _brace = braced!(_braced_content in input); + + input.parse::()?; + + let mocked_trait = input.parse()?; + + input.parse::()?; + + let impl_target = input.parse::()?; + + 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, + }) + } +} -- cgit v1.2.3-18-g5258