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 FromMetaItem for integer types #15

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `impl FromMetaItem` for integer types [#15](https://github.com/TedDriggs/darling/pull/15)

## v0.2.0 (June 18, 2017)

- Added support for returning multiple errors from parsing [#5](https://github.com/TedDriggs/darling/pull/5)
Expand Down
80 changes: 73 additions & 7 deletions core/src/from_meta_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use syn::{self, Lit, MetaItem, NestedMetaItem};

use {Error, Result};

/// Create an instance from an item in an attribute declaration.
///
/// Create an instance from an item in an attribute declaration.
///
/// # Implementing `FromMetaItem`
/// * Do not take a dependency on the `ident` of the passed-in meta item. The ident will be set by the field name of the containing struct.
/// * Implement only the `from_*` methods that you intend to support. The default implementations will return useful errors.
///
/// # Provided Implementations
/// ## bool
///
///
/// * Word with no value specified - becomes `true`.
/// * As a boolean literal, e.g. `foo = true`.
/// * As a string literal, e.g. `foo = "true"`.
Expand All @@ -27,7 +27,7 @@ use {Error, Result};
/// * As a raw string literal, e.g. `foo = r#"hello "world""#`.
///
/// ## ()
/// * Word with no value specified, e.g. `foo`. This is best used with `Option`.
/// * Word with no value specified, e.g. `foo`. This is best used with `Option`.
/// See `darling::util::Flag` for a more strongly-typed alternative.
///
/// ## Option
Expand Down Expand Up @@ -130,6 +130,66 @@ impl FromMetaItem for String {
}
}

impl FromMetaItem for u8 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for u16 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for u32 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for u64 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for usize {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for i8 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for i16 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for i32 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for i64 {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for isize {
fn from_string(s: &str) -> Result<Self> {
s.parse().or_else(|_| Err(Error::unknown_value(s)))
}
}

impl FromMetaItem for syn::Ident {
fn from_string(value: &str) -> Result<Self> {
Ok(syn::Ident::new(value))
Expand Down Expand Up @@ -223,10 +283,10 @@ impl<V: FromMetaItem> FromMetaItem for HashMap<String, V> {
if let syn::NestedMetaItem::MetaItem(ref inner) = *item {
match map.entry(inner.name().to_string()) {
Entry::Occupied(_) => return Err(Error::duplicate_field(inner.name())),
Entry::Vacant(entry) => {
Entry::Vacant(entry) => {
entry.insert(
FromMetaItem::from_meta_item(inner).map_err(|e| e.at(inner.name()))?
);
);
}
}
}
Expand All @@ -241,7 +301,7 @@ impl<V: FromMetaItem> FromMetaItem for HashMap<String, V> {
#[cfg(test)]
mod tests {
use syn;

use {FromMetaItem, Result};

/// parse a string as a syn::MetaItem instance.
Expand Down Expand Up @@ -282,6 +342,12 @@ mod tests {
assert_eq!(&fmi::<String>(r##"ignore = r#"world"#"##), "world");
}

#[test]
fn number_succeeds() {
assert_eq!(fmi::<u8>(r#"ignore = "2""#), 2u8);
assert_eq!(fmi::<i16>(r#"ignore="-25""#), -25i16);
}

#[test]
fn meta_item_succeeds() {
use syn::MetaItem;
Expand Down