Skip to content

Commit

Permalink
Use *last* path segment not first
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Feb 1, 2024
1 parent 52eba9f commit 30f768f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ pub fn superstruct(args: TokenStream, input: TokenStream) -> TokenStream {
let mut next_variant_field = output_field.clone();
match &mut next_variant_field.ty {
Type::Path(ref mut p) => {
let first_segment = &mut p
let last_segment = &mut p
.path
.segments
.first_mut()
.last_mut()
.expect("path should have at least one segment");
let inner_ty_name = first_segment.ident.clone();
let inner_ty_name = last_segment.ident.clone();
let next_variant_ty_name = format_ident!("{}{}", inner_ty_name, variant);
first_segment.ident = next_variant_ty_name;
last_segment.ident = next_variant_ty_name;
}
_ => panic!("field must be a path"),
};
Expand Down
21 changes: 14 additions & 7 deletions tests/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ fn flatten_subset() {

#[test]
fn flatten_not_first_field() {
#[superstruct(variants(A, B), variant_attributes(derive(Debug, PartialEq, Eq)))]
#[derive(Debug, PartialEq, Eq)]
struct InnerMessageTwo {
pub x: u64,
#[superstruct(only(B))]
pub y: u64,
use test_mod::*;

// Put this type in a submodule to test path parsing in `flatten`.
pub mod test_mod {
use superstruct::superstruct;

#[superstruct(variants(A, B), variant_attributes(derive(Debug, PartialEq, Eq)))]
#[derive(Debug, PartialEq, Eq)]
pub struct InnerMessageTwo {
pub x: u64,
#[superstruct(only(B))]
pub y: u64,
}
}

#[superstruct(variants(A, B), variant_attributes(derive(Debug, PartialEq, Eq)))]
Expand All @@ -110,7 +117,7 @@ fn flatten_not_first_field() {
#[superstruct(only(A), partial_getter(copy))]
pub other: u64,
#[superstruct(flatten)]
pub inner: InnerMessageTwo,
pub inner: test_mod::InnerMessageTwo,
}

let message_a = MessageTwo::A(MessageTwoA {
Expand Down

0 comments on commit 30f768f

Please sign in to comment.