Skip to content

Commit

Permalink
feat(macro): support add attributes to source and context fields
Browse files Browse the repository at this point in the history
  • Loading branch information
loichyan committed Jun 18, 2021
1 parent 9f73de5 commit 9c6f050
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ thisctx! {
pub enum Error {
#[error("I/O failed '{}': {src}", .ctx.path.display())]
IoFaild {
#[source]
@source
src: std::io::Error,
@context
Expand All @@ -32,8 +33,15 @@ thisctx! {
path: std::path::PathBuf,
},
},
#[error("not UTF-8: {0}")]
NotUtf8 (
#[source]
@source
std::str::Utf8Error
),
#[error("invalid argument: '{}'", 0.0)]
InvalidArg (
@context
#[derive(Debug)]
struct (String)
),
Expand Down
13 changes: 12 additions & 1 deletion impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl Variant {
name,
colon_token,
anon_struct,
..
}| {
let from = quote!(self);
let convert_struct_body = anon_struct
Expand All @@ -185,6 +186,7 @@ impl Variant {
)
}

// TODO: impl from source for enum
fn to_impl_from_ctx_for_enum(&self, enum_name: &Ident) -> Option<TokenStream> {
let Self {
name: variant_name,
Expand All @@ -201,6 +203,7 @@ impl Variant {
name,
colon_token,
anon_struct,
..
}| {
let convert_struct_body = anon_struct
.body
Expand Down Expand Up @@ -241,14 +244,15 @@ impl ToTokens for Variant {
let struct_body = body.map_fields(
SourceField::to_token_stream,
|ContextField {
attrs,
name,
colon_token,
anon_struct,
}| {
let generic = anon_struct
.body
.map_fields_to_generic(StructBody::GENERIC_TY_F);
quote!(#name #colon_token #variant_name #generic)
quote!(#attrs #name #colon_token #variant_name #generic)
},
);
attrs.to_tokens(tokens);
Expand Down Expand Up @@ -334,6 +338,7 @@ impl VariantFields {
let mut src = None;
let mut ctx = None;
Punctuated::<_, Token![,]>::visit_parse_with(input, |input| {
let attrs = input.parse()?;
input.parse::<Token![@]>()?;
let lookhead = input.lookahead1();
if lookhead.peek(kw::source) {
Expand All @@ -344,6 +349,7 @@ impl VariantFields {
let (name, colon_token) = parse_name_colon_token()?;
let ty = input.parse()?;
src = Some(SourceField {
attrs,
name,
colon_token,
ty,
Expand All @@ -356,6 +362,7 @@ impl VariantFields {
let (name, colon_token) = parse_name_colon_token()?;
let anon_struct = input.parse()?;
ctx = Some(ContextField {
attrs,
name,
colon_token,
anon_struct,
Expand All @@ -371,6 +378,7 @@ impl VariantFields {
}

struct SourceField {
attrs: Attributes,
name: Option<Ident>,
colon_token: Option<Token![:]>,
ty: Type,
Expand All @@ -379,17 +387,20 @@ struct SourceField {
impl ToTokens for SourceField {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self {
attrs,
name,
colon_token,
ty,
} = self;
attrs.to_tokens(tokens);
name.to_tokens(tokens);
colon_token.to_tokens(tokens);
ty.to_tokens(tokens);
}
}

struct ContextField {
attrs: Attributes,
name: Option<Ident>,
colon_token: Option<Token![:]>,
anon_struct: AnonStruct,
Expand Down
8 changes: 8 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#[derive(Debug)]
pub struct NoneError;

impl std::fmt::Display for NoneError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "an placeholder for none source error variant")
}
}

impl std::error::Error for NoneError {}

pub trait IntoError {
type Error;
type Source;
Expand Down
3 changes: 3 additions & 0 deletions tests/use_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ thisctx! {
pub enum Error {
#[error("I/O failed '{}': {src}", .ctx.path.display())]
IoFaild {
#[source]
@source
src: std::io::Error,
@context
Expand All @@ -17,6 +18,7 @@ thisctx! {
},
#[error("I/O failed: {src}")]
IoFaildWithoutPath {
#[source]
@source
src: std::io::Error,
},
Expand All @@ -38,6 +40,7 @@ thisctx! {
),
#[error("invalid argument: '{}'", 1.0)]
InvalidArg (
#[source]
@source
thisctx::NoneError,
@context
Expand Down

0 comments on commit 9c6f050

Please sign in to comment.