Skip to content

Commit

Permalink
Reformat with cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
DCNick3 committed Feb 27, 2024
1 parent 4e6901d commit d5ae149
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 34 deletions.
44 changes: 26 additions & 18 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use proc_macro::{Span, TokenStream};
use syn::{
Error, Result,
Ident, ItemEnum, Token,
parse::{Parse, ParseStream},
punctuated::Punctuated,
Error, Ident, ItemEnum, Result, Token,
};

pub fn parse(attr: TokenStream, mut item: ItemEnum) -> Result<TokenStream> {
let typ = parse_typ(attr)?;

let config = if let Some(idx) = item.attrs.iter().enumerate().find_map(
|(idx, attr)| attr.path().is_ident("bitmask_config").then_some(idx)
) {
let config = if let Some(idx) = item
.attrs
.iter()
.enumerate()
.find_map(|(idx, attr)| attr.path().is_ident("bitmask_config").then_some(idx))
{
item.attrs.remove(idx).parse_args::<Config>()?
} else {
Config::new()
Expand Down Expand Up @@ -47,17 +49,20 @@ pub fn parse(attr: TokenStream, mut item: ItemEnum) -> Result<TokenStream> {
expr
};

let i_flag = config.inverted_flags.then(|| {
let i_ident = Ident::new(&format!("Inverted{}", v_ident), v_ident.span());
let i_flag = config
.inverted_flags
.then(|| {
let i_ident = Ident::new(&format!("Inverted{}", v_ident), v_ident.span());

all_flags.push(quote::quote!(Self::#i_ident));
all_flags_names.push(quote::quote!(stringify!(#i_ident)));
all_flags.push(quote::quote!(Self::#i_ident));
all_flags_names.push(quote::quote!(stringify!(#i_ident)));

quote::quote!(
#(#v_attrs)*
#vis const #i_ident: #ident = Self { bits: (#expr) ^ !0 };
)
}).into_iter();
quote::quote!(
#(#v_attrs)*
#vis const #i_ident: #ident = Self { bits: (#expr) ^ !0 };
)
})
.into_iter();

flags.push(quote::quote!(
#(#v_attrs)*
Expand All @@ -81,7 +86,7 @@ pub fn parse(attr: TokenStream, mut item: ItemEnum) -> Result<TokenStream> {
}
}
}
} else {
} else {
quote::quote! {
impl core::fmt::Debug for #ident {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
Expand Down Expand Up @@ -319,21 +324,24 @@ fn parse_typ(attr: TokenStream) -> Result<Ident> {
#[rustfmt::skip]
"u8" | "u16" | "u32" | "u64" | "u128" | "usize" |
"i8" | "i16" | "i32" | "i64" | "i128" | "isize" => Ok(ident),
_ => Err(Error::new_spanned(ident, "type can only be an (un)signed integer")),
_ => Err(Error::new_spanned(
ident,
"type can only be an (un)signed integer",
)),
}
}
}

struct Config {
inverted_flags: bool,
vec_debug: bool
vec_debug: bool,
}

impl Config {
fn new() -> Self {
Self {
inverted_flags: false,
vec_debug: false
vec_debug: false,
}
}
}
Expand Down
84 changes: 68 additions & 16 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ mod tests {
fn test_full() {
let full = Bitmask::full();
assert_eq!(full.is_full(), true);
assert_eq!(full, Bitmask::Flag1 | Bitmask::Flag2 | Bitmask::Flag3 | Bitmask::Flag4 | Bitmask::Flag5 | Bitmask::Flag6 | Bitmask::Flag7 | Bitmask::Flag8);
assert_eq!(
full,
Bitmask::Flag1
| Bitmask::Flag2
| Bitmask::Flag3
| Bitmask::Flag4
| Bitmask::Flag5
| Bitmask::Flag6
| Bitmask::Flag7
| Bitmask::Flag8
);
}

#[test]
Expand Down Expand Up @@ -347,19 +357,43 @@ mod tests {
Flag3,
}

assert_eq!(format!("{:?}", BitmaskVecDebug::none()), "BitmaskVecDebug[]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag1), "BitmaskVecDebug[Flag1]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag2), "BitmaskVecDebug[Flag2]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag12), "BitmaskVecDebug[Flag1, Flag2, Flag12]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag3), "BitmaskVecDebug[Flag3]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag2.or(BitmaskVecDebug::Flag3)), "BitmaskVecDebug[Flag2, Flag3]");
assert_eq!(format!("{:?}", BitmaskVecDebug::full()), "BitmaskVecDebug[Flag1, Flag2, Flag12, Flag3]");
assert_eq!(
format!("{:?}", BitmaskVecDebug::none()),
"BitmaskVecDebug[]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag1),
"BitmaskVecDebug[Flag1]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag2),
"BitmaskVecDebug[Flag2]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag12),
"BitmaskVecDebug[Flag1, Flag2, Flag12]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag3),
"BitmaskVecDebug[Flag3]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag2.or(BitmaskVecDebug::Flag3)),
"BitmaskVecDebug[Flag2, Flag3]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::full()),
"BitmaskVecDebug[Flag1, Flag2, Flag12, Flag3]"
);

// default formatting
assert_eq!(format!("{:?}", Bitmask::none()), "Bitmask { bits: 0 }");
assert_eq!(format!("{:?}", Bitmask::Flag1), "Bitmask { bits: 1 }");
assert_eq!(format!("{:?}", Bitmask::Flag2), "Bitmask { bits: 2 }");
assert_eq!(format!("{:?}", Bitmask::Flag1.or(Bitmask::Flag2)), "Bitmask { bits: 3 }");
assert_eq!(
format!("{:?}", Bitmask::Flag1.or(Bitmask::Flag2)),
"Bitmask { bits: 3 }"
);
assert_eq!(format!("{:?}", Bitmask::Flag3), "Bitmask { bits: 4 }");
}

Expand All @@ -369,14 +403,32 @@ mod tests {
#[bitmask_config(vec_debug, inverted_flags)]
pub enum BitmaskVecDebug {
Flag1,
Flag2
Flag2,
}

assert_eq!(format!("{:?}", BitmaskVecDebug::none()), "BitmaskVecDebug[]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag1), "BitmaskVecDebug[Flag1]");
assert_eq!(format!("{:?}", BitmaskVecDebug::Flag2), "BitmaskVecDebug[Flag2]");
assert_eq!(format!("{:?}", BitmaskVecDebug::InvertedFlag1), "BitmaskVecDebug[InvertedFlag1, Flag2]");
assert_eq!(format!("{:?}", BitmaskVecDebug::InvertedFlag2), "BitmaskVecDebug[Flag1, InvertedFlag2]");
assert_eq!(format!("{:?}", BitmaskVecDebug::full()), "BitmaskVecDebug[Flag1, InvertedFlag1, Flag2, InvertedFlag2]");
assert_eq!(
format!("{:?}", BitmaskVecDebug::none()),
"BitmaskVecDebug[]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag1),
"BitmaskVecDebug[Flag1]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::Flag2),
"BitmaskVecDebug[Flag2]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::InvertedFlag1),
"BitmaskVecDebug[InvertedFlag1, Flag2]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::InvertedFlag2),
"BitmaskVecDebug[Flag1, InvertedFlag2]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::full()),
"BitmaskVecDebug[Flag1, InvertedFlag1, Flag2, InvertedFlag2]"
);
}
}

0 comments on commit d5ae149

Please sign in to comment.