Skip to content

Commit 69df266

Browse files
committed
feat: tuple support
1 parent 665803b commit 69df266

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
A error library that supports tracking the call-site location of errors. Also features an anyhow-style `AnyError`.
66

7-
Usage
8-
97
```rust
108
use n0_error::{e, add_meta, Error, Result, StackResultExt, StdResultExt};
119

@@ -66,4 +64,20 @@ fn main() -> Result<()> {
6664
}
6765
Ok(())
6866
}
67+
68+
// You can also use the macros with tuple structs or enums.
69+
// In this case the meta field will be added as the last field.
70+
71+
#[add_meta]
72+
#[derive(Error)]
73+
#[display("tuple fail ({_0})")]
74+
struct TupleStruct(u32);
75+
76+
#[add_meta]
77+
#[derive(Error)]
78+
enum TupleEnum {
79+
#[display("io failed")]
80+
Io(#[error(source, std_err)] std::io::Error),
81+
}
82+
6983
```

n0-error-macros/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ impl<'a> VariantInfo<'a> {
227227
}
228228
None
229229
}
230-
fn fields(&self) -> &Vec<FieldInfo<'_>> {
231-
&self.fields
232-
}
233230

234231
fn field_binding_idents(&self) -> impl Iterator<Item = Ident> + '_ {
235232
self.fields.iter().map(|f| match f.ident {
@@ -398,7 +395,13 @@ impl<'a> VariantInfo<'a> {
398395
.iter()
399396
.zip(binds.iter())
400397
.map(|(f, b)| match f.ident {
401-
FieldIdent::Named(id) => quote! { #id: #b },
398+
FieldIdent::Named(id) => {
399+
if *id == *b {
400+
quote! { #id }
401+
} else {
402+
quote! { #id: #b }
403+
}
404+
}
402405
FieldIdent::Unnamed(_) => unreachable!(),
403406
});
404407
quote! { Self::#v_ident { #( #pairs ),* } }

0 commit comments

Comments
 (0)