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

impl FromMeta for syn::ExprPath #170

Merged
merged 2 commits into from
Mar 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 29 additions & 73 deletions core/src/from_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,6 @@ macro_rules! from_meta_float {
from_meta_float!(f32);
from_meta_float!(f64);

/// Parsing support for identifiers. This attempts to preserve span information
/// when available, but also supports parsing strings with the call site as the
/// emitted span.
impl FromMeta for syn::Ident {
fn from_string(value: &str) -> Result<Self> {
syn::parse_str(value).map_err(|_| Error::unknown_value(value))
}

fn from_value(value: &Lit) -> Result<Self> {
if let Lit::Str(ref ident) = *value {
ident
.parse()
.map_err(|_| Error::unknown_lit_str_value(ident))
} else {
Err(Error::unexpected_lit_type(value))
}
}
}

/// Parsing support for punctuated. This attempts to preserve span information
/// when available, but also supports parsing strings with the call site as the
/// emitted span.
Expand All @@ -272,32 +253,32 @@ impl<T: syn::parse::Parse, P: syn::parse::Parse> FromMeta for syn::punctuated::P
}
}

/// Parsing support for an expression, i.e. `example = "1 + 2"`.
impl FromMeta for syn::Expr {
fn from_value(value: &Lit) -> Result<Self> {
if let Lit::Str(ref ident) = *value {
ident
.parse::<syn::Expr>()
.map_err(|_| Error::unknown_lit_str_value(ident))
} else {
Err(Error::unexpected_lit_type(value))
}
}
}
macro_rules! from_syn_parse {
($ty:path) => {
impl FromMeta for $ty {
fn from_string(value: &str) -> Result<Self> {
syn::parse_str(value).map_err(|_| Error::unknown_value(value))
}

/// Parsing support for an array, i.e. `example = "[1 + 2, 2 - 2, 3 * 4]"`.
impl FromMeta for syn::ExprArray {
fn from_value(value: &Lit) -> Result<Self> {
if let Lit::Str(ref ident) = *value {
ident
.parse::<syn::ExprArray>()
.map_err(|_| Error::unknown_lit_str_value(ident))
} else {
Err(Error::unexpected_lit_type(value))
fn from_value(value: &::syn::Lit) -> Result<Self> {
if let ::syn::Lit::Str(ref v) = *value {
v.parse::<$ty>()
.map_err(|_| Error::unknown_lit_str_value(v))
} else {
Err(Error::unexpected_lit_type(value))
}
}
}
}
};
}

from_syn_parse!(syn::Ident);
from_syn_parse!(syn::Expr);
from_syn_parse!(syn::ExprArray);
from_syn_parse!(syn::ExprPath);
from_syn_parse!(syn::Path);
from_syn_parse!(syn::WhereClause);

macro_rules! from_numeric_array {
($ty:ident) => {
/// Parsing an unsigned integer array, i.e. `example = "[1, 2, 3, 4]"`.
Expand Down Expand Up @@ -327,24 +308,6 @@ from_numeric_array!(u32);
from_numeric_array!(u64);
from_numeric_array!(usize);

/// Parsing support for paths. This attempts to preserve span information when available,
/// but also supports parsing strings with the call site as the emitted span.
impl FromMeta for syn::Path {
fn from_string(value: &str) -> Result<Self> {
syn::parse_str(value).map_err(|_| Error::unknown_value(value))
}

fn from_value(value: &Lit) -> Result<Self> {
if let Lit::Str(ref path_str) = *value {
path_str
.parse()
.map_err(|_| Error::unknown_lit_str_value(path_str))
} else {
Err(Error::unexpected_lit_type(value))
}
}
}

impl FromMeta for syn::Lit {
fn from_value(value: &Lit) -> Result<Self> {
Ok(value.clone())
Expand Down Expand Up @@ -380,20 +343,6 @@ impl FromMeta for syn::Meta {
}
}

impl FromMeta for syn::WhereClause {
fn from_string(value: &str) -> Result<Self> {
syn::parse_str(value).map_err(|_| Error::unknown_value(value))
}

fn from_value(value: &Lit) -> Result<Self> {
if let syn::Lit::Str(s) = value {
s.parse().map_err(|_| Error::unknown_lit_str_value(s))
} else {
Err(Error::unexpected_lit_type(value))
}
}
}

impl FromMeta for Vec<syn::WherePredicate> {
fn from_string(value: &str) -> Result<Self> {
syn::WhereClause::from_string(&format!("where {}", value))
Expand Down Expand Up @@ -823,6 +772,13 @@ mod tests {
fm::<syn::Expr>(quote!(ignore = "{ a_statement(); in_a_block }"));
}

#[test]
fn test_expr_path() {
fm::<syn::ExprPath>(quote!(ignore = "std::mem::replace"));
fm::<syn::ExprPath>(quote!(ignore = "x"));
fm::<syn::ExprPath>(quote!(ignore = "example::<Test>"));
}

#[test]
fn test_number_array() {
assert_eq!(
Expand Down