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

Remove failure #12

Merged
merged 2 commits into from Jul 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions structopt-toml-derive/src/lib.rs
Expand Up @@ -5,11 +5,10 @@ extern crate quote;

use proc_macro::TokenStream;
use proc_macro2::TokenTree;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::parse::{ParseStream, Parse};
use syn::{DataStruct, DeriveInput, Field, Ident, LitStr, buffer::Cursor};

use syn::{buffer::Cursor, DataStruct, DeriveInput, Field, Ident, LitStr};

#[proc_macro_derive(StructOptToml, attributes(structopt))]
pub fn structopt_toml(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -114,7 +113,7 @@ fn load_explicit_name(field: &Field) -> Option<String> {
// find name = `value` in attribute
syn::parse2::<NameVal>(ts).map(|nv| nv.0).ok()
})
.nth(0)
.next()
}

/// Checks whether the attribute is marked as flattened
Expand Down Expand Up @@ -144,11 +143,10 @@ fn is_flatten(field: &Field) -> bool {
};
path.is_ident("flatten")
})
.nth(0)
.next()
.unwrap_or(false)
}


#[derive(Debug)]
struct NameVal(String);

Expand All @@ -168,7 +166,9 @@ impl Parse for NameVal {
TokenTree::Ident(ident) if ident == "name" && state == Match::NameToken => {
state = Match::PunctEq;
}
TokenTree::Punct(punct) if punct.as_char() == '=' && state == Match::PunctEq => {
TokenTree::Punct(punct)
if punct.as_char() == '=' && state == Match::PunctEq =>
{
state = Match::LitVal;
}
TokenTree::Literal(lit) if state == Match::LitVal => {
Expand All @@ -183,6 +183,6 @@ impl Parse for NameVal {
}
Err(cursor.error("End reached"))
});
result.map(|lit| Self(lit)).map_err(|_| input.error("Not found"))
result.map(Self).map_err(|_| input.error("Not found"))
}
}
2 changes: 1 addition & 1 deletion structopt-toml/Cargo.toml
Expand Up @@ -20,7 +20,7 @@ default = ["clap/default", "structopt/default"]

[dependencies]
clap = { version = "2.33.0", default-features = false }
failure = "0.1.7"
anyhow = "1.0.42"
toml = "0.5.6"
serde = "1.0.104"
serde_derive = "1.0.104"
Expand Down
26 changes: 12 additions & 14 deletions structopt-toml/src/lib.rs
Expand Up @@ -19,14 +19,12 @@
//! #[structopt(default_value = "0", short = "b")] b: i32,
//! }
//!
//! fn main() {
//! let toml_str = r#"
//! a = 10
//! "#;
//! let opt = Opt::from_args_with_toml(toml_str).expect("toml parse failed");
//! println!("a:{}", opt.a);
//! println!("b:{}", opt.b);
//! }
//! let toml_str = r#"
//! a = 10
//! "#;
//! let opt = Opt::from_args_with_toml(toml_str).expect("toml parse failed");
//! println!("a:{}", opt.a);
//! println!("b:{}", opt.b);
//! ```
//!
//! The execution result of the above example is below.
Expand All @@ -41,8 +39,8 @@
//! b:0
//! ```

extern crate anyhow;
extern crate clap as _clap;
extern crate failure;
extern crate serde as _serde;
extern crate structopt as _structopt;
extern crate toml as _toml;
Expand Down Expand Up @@ -81,19 +79,19 @@ pub trait StructOptToml {
fn from_clap_with_toml<'a>(
toml_str: &'a str,
args: &_clap::ArgMatches,
) -> Result<Self, failure::Error>
) -> Result<Self, anyhow::Error>
where
Self: Sized,
Self: _structopt::StructOpt,
Self: _serde::de::Deserialize<'a>,
{
let from_args: Self = _structopt::StructOpt::from_clap(&args);
let from_args: Self = _structopt::StructOpt::from_clap(args);
let from_toml: Self = _toml::from_str(toml_str)?;
Ok(Self::merge(from_toml, from_args, &args))
Ok(Self::merge(from_toml, from_args, args))
}

/// Creates the struct from command line arguments with initial values from TOML.
fn from_args_with_toml<'a>(toml_str: &'a str) -> Result<Self, failure::Error>
fn from_args_with_toml<'a>(toml_str: &'a str) -> Result<Self, anyhow::Error>
where
Self: Sized,
Self: _structopt::StructOpt,
Expand All @@ -105,7 +103,7 @@ pub trait StructOptToml {
}

/// Creates the struct from iterator with initial values from TOML.
fn from_iter_with_toml<'a, I>(toml_str: &'a str, iter: I) -> Result<Self, failure::Error>
fn from_iter_with_toml<'a, I>(toml_str: &'a str, iter: I) -> Result<Self, anyhow::Error>
where
Self: Sized,
Self: _structopt::StructOpt,
Expand Down
3 changes: 1 addition & 2 deletions structopt-toml/tests/test.rs
Expand Up @@ -80,7 +80,6 @@ fn test() {
assert_eq!(test.d3, vec![233]);
}


static POSSIBLE_VALUES: &[&str] = &["one", "two"];

#[derive(Debug, Deserialize, StructOpt, StructOptToml)]
Expand Down Expand Up @@ -108,7 +107,7 @@ struct Outer {
#[structopt(long = "one", default_value = "1")]
one: u32,
#[structopt(flatten)]
two: Inner
two: Inner,
}

#[derive(Debug, Deserialize, StructOpt, StructOptToml)]
Expand Down