Skip to content

Commit

Permalink
port sort_by to syn2 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
valsteen authored Aug 6, 2023
1 parent 5f1d36a commit 5fb698e
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 414 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ doctest = false

[dependencies]
proc-macro2 = "1.0.43"
syn = { version = "1.0.99", features = ["full"] }
syn = { version = "2.0.28", features = ["full"] }
quote = "1.0.21"
either = "1.8.0"

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
# sort_by_derive

<!-- TOC -->
* [Usage](#usage)
* [SortBy](#sortby)
* [EnumAccessor](#enumaccessor)
* [Field accessor](#field-accessor)
* [EnumSequence](#enumsequence)
* [Example](#example)
* [sort_by_derive](#sortbyderive)
* [Usage](#usage)
* [SortBy](#sortby)
* [EnumAccessor](#enumaccessor)
* [Field accessor](#field-accessor)
* [EnumSequence](#enumsequence)
* [Example](#example)
<!-- TOC -->

This crate provides 3 derive macros `SortBy`, `EnumAccessor` and `EnumSequence`.
Expand Down
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

max_width = 120
reorder_imports = true
19 changes: 7 additions & 12 deletions src/enum_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ pub fn impl_enum_sequence(input: DeriveInput) -> TokenStream {
let input_span = input.span();
let trait_ident = Ident::new(format!("{ident}EnumSequence").as_str(), input_span);

let enu = match input.data {
Data::Enum(enu) => enu,
_ => {
let Data::Enum(enu) = input.data else {
return syn::Error::new(input_span, ENUM_HELP).into_compile_error();
}
};
};

let mut match_branches = Vec::new();

Expand All @@ -31,7 +28,7 @@ pub fn impl_enum_sequence(input: DeriveInput) -> TokenStream {

match_branches.push(quote::quote_spanned! { span =>
#pattern => #i
})
});
}

let vis = &input.vis;
Expand All @@ -41,10 +38,10 @@ pub fn impl_enum_sequence(input: DeriveInput) -> TokenStream {
.generics
.params
.iter()
.flat_map(|p| match p {
.filter_map(|p| match p {
GenericParam::Type(t) => Some(&t.ident),
GenericParam::Const(t) => Some(&t.ident),
_ => None,
GenericParam::Lifetime(_) => None,
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -78,9 +75,7 @@ mod test {
};

let output = crate::enum_sequence::impl_enum_sequence(syn::parse2(input).unwrap());
let output = rust_format::RustFmt::default()
.format_str(output.to_string())
.unwrap();
let output = rust_format::RustFmt::default().format_str(output.to_string()).unwrap();

assert_eq!(
output,
Expand All @@ -97,6 +92,6 @@ impl EEnumSequence for E {
}
}
"#,
)
);
}
}
Loading

0 comments on commit 5fb698e

Please sign in to comment.