Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix positional tuple fields in Display impl #20

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compile_tests/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fn compile_tests() {
if rustversion::cfg!(all(stable, since(1.68.0))) {
t.compile_fail("compile_tests/no_display_no_impl.rs");
}
t.pass("compile_tests/skip_positional.rs");
}
2 changes: 2 additions & 0 deletions compile_tests/empty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
enum Error {}

Expand Down
9 changes: 9 additions & 0 deletions compile_tests/skip_positional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
enum Error {
/// Skip positional tuple fields {1}
Skip(u8, u8),
}

fn main() {}
19 changes: 9 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,21 @@ pub fn derive_error(input: TokenStream) -> TokenStream {
v.display_fields
.iter()
.fold(String::new(), |mut fields, field| {
write!(fields, "{field},").unwrap();
let _ = write!(fields, "{field},");
fields
});

Ok(match &v.ty {
VariantType::Unit => format!("Self::{name} => write!(f, {display:?}),"),
VariantType::Tuple => {
let fields = (0..v.fields.len())
.map(|i| {
if v.display_fields.contains(&Rc::from(format!("field_{i}"))) {
format!("field_{i},")
} else {
String::from("_,")
}
})
.collect::<String>();
let fields = (0..v.fields.len()).fold(String::new(), |mut fields, i| {
if v.display_fields.contains(&Rc::from(format!("field_{i}"))) {
let _ = write!(fields, "field_{i},");
} else {
let _ = fields.write_str("_,");
}
fields
});
format!("Self::{name}({fields}) => write!(f, {display:?}, {display_fields}),")
}
VariantType::Struct => {
Expand Down
12 changes: 11 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Variant {
.to_string();

// Collect field references.
let display_fields = display
let display_fields: Vec<Rc<str>> = display
.split('{')
.skip(1)
.filter_map(|s| s.split('}').next())
Expand All @@ -170,6 +170,16 @@ impl Variant {

// Remove field references from format string.
for field in &display_fields {
// Special case for tuples
if ty == VariantType::Tuple {
if let Some(num) = field.strip_prefix("field_") {
display = display
.replace(&format!("{{{num}:"), "{:")
.replace(&format!("{{{num}}}"), "{}");
continue;
}
}

display = display
.replace(&format!("{{{field}:"), "{:")
.replace(&format!("{{{field}}}"), "{}");
Expand Down