Skip to content

Commit

Permalink
Changed parse_{provided,cli} String -> AsRef<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyllingene committed Nov 26, 2023
1 parent 7c38619 commit c6a3928
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ impl ArgumentReader {
/// If any arguments fail to parse their values, this
/// will forward that error. Otherwise, see
/// [`ArgParseError`] for a list of all possible errors.
pub fn parse_provided<K: AsRef<str>, V: AsRef<str>, I: IntoIterator<Item = (K, V)>>(
pub fn parse_provided<
A: AsRef<str>,
K: AsRef<str>,
V: AsRef<str>,
I: IntoIterator<Item = (K, V)>,
>(
mut self,
cli: &[String],
cli: &[A],
env: I,
) -> Result<Arguments, ArgParseError> {
self.parse_env(env);
Expand Down Expand Up @@ -204,8 +209,8 @@ impl ArgumentReader {
/// # Errors
///
/// See [`parse`](ArgumentReader::parse) for details.
fn parse_cli(mut self, args: &[String]) -> Result<Arguments, ArgParseError> {
let mut args = args.iter().peekable();
fn parse_cli<A: AsRef<str>>(mut self, args: &[A]) -> Result<Arguments, ArgParseError> {
let mut args = args.iter().peekable().map(|arg| arg.as_ref());
let mut remainder = Vec::new();

while let Some(arg) = args.next() {
Expand All @@ -225,7 +230,7 @@ impl ArgumentReader {

let val = if arg.consumes {
if val.is_none() {
args.next().cloned()
args.next().map(str::to_string)
} else {
val.map(str::to_string)
}
Expand All @@ -252,7 +257,7 @@ impl ArgumentReader {

let next = if arg.consumes {
consumed = true;
args.next().cloned()
args.next().map(str::to_string)
} else {
None
};
Expand All @@ -261,7 +266,7 @@ impl ArgumentReader {
}
}
} else {
remainder.push(arg.clone());
remainder.push(arg.to_string());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn basic_env_var() {
let args = create_env!["CONFIG_DIR", "/cfg"];

let args = parser
.parse_provided(&[], args)
.parse_provided(&[] as &[String], args)
.expect("Failed to parse environment");

assert_eq!(cfg.get(&args), Some(Ok("/cfg".to_string())));
Expand All @@ -152,7 +152,7 @@ fn many_env_vars() {
let args = create_env!["CONFIG_DIR", "/cfg", "SILENT", "0", "THREADS", "16",];

let args = parser
.parse_provided(&[], args)
.parse_provided(&[] as &[String], args)
.expect("Failed to parse environment");

assert_eq!(cfg.get(&args), Some(Ok("/cfg".to_string())));
Expand Down

0 comments on commit c6a3928

Please sign in to comment.