fix(macros): duplicated if statements
This commit is contained in:
parent
ed547c757a
commit
a4aac23cbb
5 changed files with 29 additions and 90 deletions
|
@ -1,42 +0,0 @@
|
||||||
name: checks
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- "main"
|
|
||||||
- "feat/*"
|
|
||||||
pull_request:
|
|
||||||
pull_request_target:
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
RUST_BACKTRACE: full
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: self-hosted
|
|
||||||
timeout-minutes: 10
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Run tests
|
|
||||||
run: cargo test --verbose --all-features --workspace --tests --bins --lib
|
|
||||||
- name: Run doctests
|
|
||||||
run: cargo test --verbose --all-features --workspace --doc
|
|
||||||
|
|
||||||
clippy:
|
|
||||||
runs-on: self-hosted
|
|
||||||
timeout-minutes: 10
|
|
||||||
steps:
|
|
||||||
- name: Checkout sources
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: Run clippy
|
|
||||||
run: cargo clippy -- -D warnings
|
|
||||||
|
|
||||||
format:
|
|
||||||
runs-on: self-hosted
|
|
||||||
timeout-minutes: 10
|
|
||||||
steps:
|
|
||||||
- name: Checkout sources
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: Run cargo fmt
|
|
||||||
run: cargo fmt --all -- --check
|
|
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -1,17 +1,17 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "binf"
|
name = "binf"
|
||||||
version = "1.1.2"
|
version = "1.1.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"binf_macros",
|
"binf_macros",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "binf_macros"
|
name = "binf_macros"
|
||||||
version = "1.1.2"
|
version = "1.1.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"proc-macro2-diagnostics",
|
"proc-macro2-diagnostics",
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
[package]
|
[package]
|
||||||
name = "binf"
|
name = "binf"
|
||||||
version = "1.1.3"
|
version = "1.1.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "A crate that adds utilities for dealing with binary flags"
|
description = "A crate that adds utilities for dealing with binary flags"
|
||||||
authors = ["DavidOnTop <me@davidon.top>"]
|
authors = ["DavidOnTop <me@davidon.top>"]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://git.davidon.top/davidontop/binf.git"
|
repository = "https://github.com/davidon-top/binf.git"
|
||||||
documentation = "https://docs.rs/binf"
|
documentation = "https://docs.rs/binf"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["macros"]
|
members = ["macros"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
binf_macros = { version = "1.1.3", path = "macros" }
|
binf_macros = { version = "1.1.4", path = "macros" }
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
[package]
|
[package]
|
||||||
name = "binf_macros"
|
name = "binf_macros"
|
||||||
version = "1.1.3"
|
version = "1.1.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "A crate that adds utilities for dealing with binary flags"
|
description = "A crate that adds utilities for dealing with binary flags"
|
||||||
authors = ["DavidOnTop <me@davidon.top>"]
|
authors = ["DavidOnTop <me@davidon.top>"]
|
||||||
readme = "../README.md"
|
readme = "../README.md"
|
||||||
repository = "https://git.davidon.top/davidontop/binf.git"
|
repository = "https://github.com/davidon-top/binf.git"
|
||||||
documentation = "https://docs.rs/binf_macros/"
|
documentation = "https://docs.rs/binf_macros/"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -27,12 +27,11 @@ use proc_macro2_diagnostics::Diagnostic;
|
||||||
pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
let mut diagnostics = Vec::new();
|
let mut diagnostics = Vec::new();
|
||||||
|
|
||||||
let structdef: syn::ItemStruct = syn::parse_macro_input!(input as syn::ItemStruct);
|
let struct_def: syn::ItemStruct = syn::parse_macro_input!(input as syn::ItemStruct);
|
||||||
// check if the struct has correct shape
|
check_struct(&mut diagnostics, &struct_def);
|
||||||
check_struct(&mut diagnostics, &structdef);
|
|
||||||
|
|
||||||
let structname = structdef.ident;
|
let struct_name = struct_def.ident;
|
||||||
let structfields = match &structdef.fields {
|
let struct_fields = match &struct_def.fields {
|
||||||
syn::Fields::Named(f) => f
|
syn::Fields::Named(f) => f
|
||||||
.named
|
.named
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -41,8 +40,8 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
_ => {
|
_ => {
|
||||||
diagnostics.push(
|
diagnostics.push(
|
||||||
syn::Error::new_spanned(
|
syn::Error::new_spanned(
|
||||||
&structdef.fields,
|
&struct_def.fields,
|
||||||
"struct has incorrect shape, only works on struct with named fields",
|
"bitflags: struct has incorrect shape, only works on struct with named fields",
|
||||||
)
|
)
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
|
@ -50,12 +49,12 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let vis = structdef.vis.clone();
|
let vis = struct_def.vis.clone();
|
||||||
let attrs = structdef.attrs.clone();
|
let attrs = struct_def.attrs.clone();
|
||||||
|
|
||||||
// a unsigned int type of a size larger then the number of fields in the struct
|
// unsigned int type of size larger than the number of fields in the struct
|
||||||
let u_type = {
|
let u_type = {
|
||||||
let fields_len = structfields.len();
|
let fields_len = struct_fields.len();
|
||||||
match fields_len {
|
match fields_len {
|
||||||
0..=8 => quote::quote! { u8 },
|
0..=8 => quote::quote! { u8 },
|
||||||
9..=16 => quote::quote! { u16 },
|
9..=16 => quote::quote! { u16 },
|
||||||
|
@ -64,7 +63,7 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
65..=128 => quote::quote! { u128 },
|
65..=128 => quote::quote! { u128 },
|
||||||
_ => {
|
_ => {
|
||||||
diagnostics.push(
|
diagnostics.push(
|
||||||
syn::Error::new_spanned(structdef.fields, "struct has too many fields").into(),
|
syn::Error::new_spanned(struct_def.fields, "bitflags: struct has too many fields").into(),
|
||||||
);
|
);
|
||||||
quote::quote! { u128 }
|
quote::quote! { u128 }
|
||||||
}
|
}
|
||||||
|
@ -73,14 +72,14 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
let newstruct = quote::quote! {
|
let newstruct = quote::quote! {
|
||||||
#(#attrs)*
|
#(#attrs)*
|
||||||
#vis struct #structname {
|
#vis struct #struct_name {
|
||||||
value: #u_type
|
value: #u_type
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// make functions for each field in the struct
|
// make functions for each field in the struct
|
||||||
let mut functions = Vec::new();
|
let mut functions = Vec::new();
|
||||||
for (i, field) in structfields.iter().enumerate() {
|
for (i, field) in struct_fields.iter().enumerate() {
|
||||||
let field = field.clone();
|
let field = field.clone();
|
||||||
let i = i as u8;
|
let i = i as u8;
|
||||||
let set_ident = syn::Ident::new(&format!("set_{}", field), field.span());
|
let set_ident = syn::Ident::new(&format!("set_{}", field), field.span());
|
||||||
|
@ -95,7 +94,7 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
let impls = quote::quote! {
|
let impls = quote::quote! {
|
||||||
impl #structname {
|
impl #struct_name {
|
||||||
#(#functions)*
|
#(#functions)*
|
||||||
|
|
||||||
pub fn new(val: #u_type) -> Self {
|
pub fn new(val: #u_type) -> Self {
|
||||||
|
@ -111,7 +110,7 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
};
|
};
|
||||||
|
|
||||||
let deref_impl = quote::quote! {
|
let deref_impl = quote::quote! {
|
||||||
impl std::ops::Deref for #structname {
|
impl std::ops::Deref for #struct_name {
|
||||||
type Target = #u_type;
|
type Target = #u_type;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.value
|
&self.value
|
||||||
|
@ -120,7 +119,7 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
};
|
};
|
||||||
|
|
||||||
let deref_mut_impl = quote::quote! {
|
let deref_mut_impl = quote::quote! {
|
||||||
impl std::ops::DerefMut for #structname {
|
impl std::ops::DerefMut for #struct_name {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.value
|
&mut self.value
|
||||||
}
|
}
|
||||||
|
@ -140,25 +139,7 @@ pub fn bitflag(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
/// returns true if the struct has a correct shape
|
/// returns true if the struct has a correct shape
|
||||||
fn check_struct(diagnostics: &mut Vec<Diagnostic>, input: &syn::ItemStruct) {
|
fn check_struct(diagnostics: &mut Vec<Diagnostic>, input: &syn::ItemStruct) {
|
||||||
if input.generics.lt_token.is_some() {
|
if input.generics.lt_token.is_some() || input.generics.gt_token.is_some() || input.generics.where_clause.is_some() || !input.generics.params.is_empty() {
|
||||||
diagnostics.push(
|
|
||||||
syn::Error::new_spanned(&input.generics, "generics not allowed in bitflag structs")
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if input.generics.gt_token.is_some() {
|
|
||||||
diagnostics.push(
|
|
||||||
syn::Error::new_spanned(&input.generics, "generics not allowed in bitflag structs")
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if input.generics.where_clause.is_some() {
|
|
||||||
diagnostics.push(
|
|
||||||
syn::Error::new_spanned(&input.generics, "generics not allowed in bitflag structs")
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if !input.generics.params.is_empty() {
|
|
||||||
diagnostics.push(
|
diagnostics.push(
|
||||||
syn::Error::new_spanned(&input.generics, "generics not allowed in bitflag structs")
|
syn::Error::new_spanned(&input.generics, "generics not allowed in bitflag structs")
|
||||||
.into(),
|
.into(),
|
||||||
|
@ -167,14 +148,14 @@ fn check_struct(diagnostics: &mut Vec<Diagnostic>, input: &syn::ItemStruct) {
|
||||||
|
|
||||||
match &input.fields {
|
match &input.fields {
|
||||||
syn::Fields::Unnamed(f) => diagnostics.push(
|
syn::Fields::Unnamed(f) => diagnostics.push(
|
||||||
syn::Error::new_spanned(f, "struct has incorrect shape, found tuple struct").into(),
|
syn::Error::new_spanned(f, "bitflags: struct has incorrect shape, found tuple struct").into(),
|
||||||
),
|
),
|
||||||
syn::Fields::Unit => diagnostics.push(
|
syn::Fields::Unit => diagnostics.push(
|
||||||
syn::Error::new_spanned(input, "struct has incorrect shape, found unit struct").into(),
|
syn::Error::new_spanned(input, "bitflags: struct has incorrect shape, found unit struct").into(),
|
||||||
),
|
),
|
||||||
syn::Fields::Named(f) => {
|
syn::Fields::Named(f) => {
|
||||||
if f.named.len() > 128 {
|
if f.named.len() > 128 {
|
||||||
diagnostics.push(syn::Error::new_spanned(f, "struct has too many fields").into());
|
diagnostics.push(syn::Error::new_spanned(f, "bitflags: struct has too many fields").into());
|
||||||
}
|
}
|
||||||
for field in f.named.iter() {
|
for field in f.named.iter() {
|
||||||
match &field.ty {
|
match &field.ty {
|
||||||
|
@ -182,7 +163,7 @@ fn check_struct(diagnostics: &mut Vec<Diagnostic>, input: &syn::ItemStruct) {
|
||||||
_ => diagnostics.push(
|
_ => diagnostics.push(
|
||||||
syn::Error::new_spanned(
|
syn::Error::new_spanned(
|
||||||
field,
|
field,
|
||||||
"struct has incorrect shape, found non bool field",
|
"bitflags: struct has incorrect shape, found non bool field",
|
||||||
)
|
)
|
||||||
.into(),
|
.into(),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue