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

Pass the try_from_str functions a &str instead of a &String. #282

Merged
merged 2 commits into from Nov 22, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# Upcoming

* `try_from_str` functions are now called with a `&str` instead of a `&String` ([#282](https://github.com/TeXitoi/structopt/pull/282))

# v0.3.4 (2019-11-08)

* `rename_all` does not apply to fields that were annotated with explicit
Expand Down
2 changes: 1 addition & 1 deletion structopt-derive/src/lib.rs
Expand Up @@ -126,7 +126,7 @@ fn gen_augmentation(
let validator = match *parser.kind {
ParserKind::TryFromStr => quote_spanned! { func.span()=>
.validator(|s| {
#func(&s)
#func(s.as_str())
.map(|_: #convert_type| ())
.map_err(|e| e.to_string())
})
Expand Down
16 changes: 15 additions & 1 deletion tests/custom-string-parsers.rs
Expand Up @@ -8,7 +8,7 @@

use structopt::StructOpt;

use std::ffi::{OsStr, OsString};
use std::ffi::{CString, OsStr, OsString};
use std::num::ParseIntError;
use std::path::PathBuf;

Expand Down Expand Up @@ -290,3 +290,17 @@ fn test_custom_bool() {
Opt::from_iter(&["test", "-dtrue", "-bfalse", "-btrue", "-bfalse", "-bfalse"])
);
}

#[test]
fn test_cstring() {
#[derive(StructOpt)]
struct Opt {
#[structopt(parse(try_from_str = CString::new))]
c_string: CString,
}
assert!(Opt::clap().get_matches_from_safe(&["test"]).is_err());
assert_eq!(Opt::from_iter(&["test", "bla"]).c_string.to_bytes(), b"bla");
assert!(Opt::clap()
.get_matches_from_safe(&["test", "bla\0bla"])
.is_err());
}