Skip to content

Commit

Permalink
fix: parse syn::Lit instead of syn::Expr (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaralice committed May 14, 2024
1 parent a619bef commit c3621f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 11 additions & 0 deletions tests/unformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ fn test_unformat_typed_named_captures() {
);
assert_eq!(addr, SocketAddr::from_str("127.0.0.1:3000").ok());
}

#[test]
fn test_declmacro() {
macro_rules! test_declmacro {
($fmt:literal, $input:expr) => {
unformat!($fmt, $input)
};
}

test_declmacro!("abc", "abcd");
}
13 changes: 4 additions & 9 deletions unfmt_macros/unfmt_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream, Result},
parse_macro_input, parse_str, Expr, ExprLit, Ident, Lit, LitBool, LitByteStr, Token, TypePath,
parse_macro_input, parse_str, Expr, Ident, Lit, LitBool, LitByteStr, Token, TypePath,
};

struct Unformat {
Expand All @@ -24,14 +24,9 @@ struct Unformat {
impl Parse for Unformat {
fn parse(input: ParseStream) -> Result<Self> {
#[allow(clippy::wildcard_enum_match_arm)]
let (pattern, is_pattern_str) = match input.parse::<Expr>()? {
Expr::Lit(ExprLit {
lit: Lit::Str(str), ..
}) => (str.value().into_bytes(), true),
Expr::Lit(ExprLit {
lit: Lit::ByteStr(byte_str),
..
}) => (byte_str.value(), false),
let (pattern, is_pattern_str) = match input.parse::<Lit>()? {
Lit::Str(str) => (str.value().into_bytes(), true),
Lit::ByteStr(byte_str) => (byte_str.value(), false),
_ => return Err(input.error("expected a string literal")),
};

Expand Down

0 comments on commit c3621f8

Please sign in to comment.