From 178267c701c233542078c09fe6b19802f9642dbd Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 30 Jan 2023 21:29:21 +0100 Subject: feat: improve macro error messages --- macros/src/macro_flag.rs | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'macros/src/macro_flag.rs') diff --git a/macros/src/macro_flag.rs b/macros/src/macro_flag.rs index 97a8ff2..f0e3a70 100644 --- a/macros/src/macro_flag.rs +++ b/macros/src/macro_flag.rs @@ -1,22 +1,37 @@ +use std::hash::Hash; + +use proc_macro2::Span; use syn::parse::{Parse, ParseStream}; use syn::{Ident, LitBool, Token}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Eq, Clone)] pub struct MacroFlag { pub flag: Ident, pub is_on: LitBool, } -impl Parse for MacroFlag +impl MacroFlag { - fn parse(input: ParseStream) -> syn::Result + pub fn new_off(flag: &str) -> Self { - let input_forked = input.fork(); + Self { + flag: Ident::new(flag, Span::call_site()), + is_on: LitBool::new(false, Span::call_site()), + } + } - let flag: Ident = input_forked.parse()?; + pub fn is_on(&self) -> bool + { + self.is_on.value + } +} - input.parse::()?; +impl Parse for MacroFlag +{ + fn parse(input: ParseStream) -> syn::Result + { + let flag = input.parse::()?; input.parse::()?; @@ -26,6 +41,22 @@ impl Parse for MacroFlag } } +impl PartialEq for MacroFlag +{ + fn eq(&self, other: &Self) -> bool + { + self.flag == other.flag + } +} + +impl Hash for MacroFlag +{ + fn hash(&self, state: &mut H) + { + self.flag.hash(state); + } +} + #[cfg(test)] mod tests { -- cgit v1.2.3-18-g5258