Skip to content

Commit

Permalink
Merge pull request #348 from vallentin/master
Browse files Browse the repository at this point in the history
Added anywhere support for derive
  • Loading branch information
pacak authored Mar 19, 2024
2 parents 4141714 + e3ec840 commit b7c0b16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bpaf_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ impl ToTokens for PostParse {
PostParse::Optional { .. } => quote!(optional()),
PostParse::Parse { f, .. } => quote!(parse(#f)),
PostParse::Strict { .. } => quote!(strict()),
PostParse::Anywhere { .. } => quote!(anywhere()),
}
.to_tokens(tokens);
}
Expand Down Expand Up @@ -255,6 +256,7 @@ pub(crate) enum PostParse {
Optional { span: Span },
Parse { span: Span, f: Box<Expr> },
Strict { span: Span },
Anywhere { span: Span },
}
impl PostParse {
fn span(&self) -> Span {
Expand All @@ -268,7 +270,8 @@ impl PostParse {
| Self::Map { span, .. }
| Self::Optional { span }
| Self::Parse { span, .. }
| Self::Strict { span } => *span,
| Self::Strict { span }
| Self::Anywhere { span } => *span,
}
}
}
Expand Down Expand Up @@ -480,6 +483,8 @@ impl PostParse {
} else if kw == "some" {
let msg = parse_arg(input)?;
Self::Some_ { span, msg }
} else if kw == "anywhere" {
Self::Anywhere { span }
} else {
return Ok(None);
}))
Expand Down
12 changes: 12 additions & 0 deletions bpaf_derive/src/field_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,15 @@ fn raw_literal() {
};
assert_eq!(input.to_token_stream().to_string(), output.to_string());
}

#[test]
fn any_anywhere() {
let input: NamedField = parse_quote! {
#[bpaf(any::<isize>("LIMIT", isize_to_usize), anywhere)]
num: isize
};
let output = quote! {
::bpaf::any::<isize, _, _>("LIMIT", isize_to_usize).anywhere()
};
assert_eq!(input.to_token_stream().to_string(), output.to_string());
}

0 comments on commit b7c0b16

Please sign in to comment.