Skip to content

Commit

Permalink
style: Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 15, 2022
1 parent 2f0d916 commit 15d8b3b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
22 changes: 12 additions & 10 deletions clap_lex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! verbosity: 0,
//! };
//!
//! let raw = clap_lex::RawArgs::from_iter(raw);
//! let raw = clap_lex::RawArgs::new(raw);
//! let mut cursor = raw.cursor();
//! raw.next(&mut cursor); // Skip the bin
//! while let Some(arg) = raw.next(&mut cursor) {
Expand Down Expand Up @@ -124,6 +124,8 @@ pub struct RawArgs {
impl RawArgs {
//// Create an argument list to parse
///
/// **NOTE:** The argument returned will be the current binary.
///
/// # Example
///
/// ```rust,no_run
Expand All @@ -136,7 +138,7 @@ impl RawArgs {
/// println!("{:?}", paths);
/// ```
pub fn from_args() -> Self {
Self::from_iter(std::env::args_os())
Self::new(std::env::args_os())
}

//// Create an argument list to parse
Expand All @@ -145,14 +147,14 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
/// let mut paths = raw.remaining(&mut cursor).map(PathBuf::from).collect::<Vec<_>>();
/// println!("{:?}", paths);
/// ```
pub fn from_iter(iter: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
pub fn new(iter: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
let iter = iter.into_iter();
Self::from(iter)
}
Expand All @@ -163,7 +165,7 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
Expand Down Expand Up @@ -202,7 +204,7 @@ impl RawArgs {
///
/// ```rust,no_run
/// # use std::path::PathBuf;
/// let raw = clap_lex::RawArgs::from_iter(["bin", "foo.txt"]);
/// let raw = clap_lex::RawArgs::new(["bin", "foo.txt"]);
/// let mut cursor = raw.cursor();
/// let _bin = raw.next_os(&mut cursor);
///
Expand Down Expand Up @@ -299,7 +301,7 @@ impl<'s> ParsedArg<'s> {
pub fn to_long(&self) -> Option<(Result<&str, &RawOsStr>, Option<&RawOsStr>)> {
if let Some(raw) = self.utf8 {
let remainder = raw.strip_prefix("--")?;
let (flag, value) = if let Some((p0, p1)) = remainder.split_once("=") {
let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
(p0, Some(p1))
} else {
(remainder, None)
Expand All @@ -310,12 +312,12 @@ impl<'s> ParsedArg<'s> {
} else {
let raw = self.inner.as_ref();
let remainder = raw.strip_prefix("--")?;
let (flag, value) = if let Some((p0, p1)) = remainder.split_once("=") {
let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
(p0, Some(p1))
} else {
(remainder, None)
};
let flag = flag.to_str().ok_or_else(|| flag);
let flag = flag.to_str().ok_or(flag);
Some((flag, value))
}
}
Expand Down Expand Up @@ -363,7 +365,7 @@ impl<'s> ParsedArg<'s> {
///
/// **NOTE:** May return a flag or an escape.
pub fn to_value(&self) -> Result<&str, &RawOsStr> {
self.utf8.ok_or(self.inner.as_ref())
self.utf8.ok_or_else(|| self.inner.as_ref())
}

/// Safely print an argument that may contain non-UTF8 content
Expand Down
4 changes: 2 additions & 2 deletions clap_lex/tests/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[test]
fn insert() {
let mut raw = clap_lex::RawArgs::from_iter(["bin", "a", "b", "c"]);
let mut raw = clap_lex::RawArgs::new(["bin", "a", "b", "c"]);
let mut cursor = raw.cursor();

assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("a")));
raw.insert(&mut cursor, &["1", "2", "3"]);
raw.insert(&cursor, &["1", "2", "3"]);
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("1")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("2")));
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("3")));
Expand Down
32 changes: 16 additions & 16 deletions clap_lex/tests/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// the start of a long because there is no valid value to return.
#[test]
fn to_long_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -14,7 +14,7 @@ fn to_long_stdio() {

#[test]
fn to_long_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -28,7 +28,7 @@ fn to_long_escape() {

#[test]
fn to_long_no_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -42,7 +42,7 @@ fn to_long_no_value() {

#[test]
fn to_long_with_empty_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long="]);
let raw = clap_lex::RawArgs::new(["bin", "--long="]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -56,7 +56,7 @@ fn to_long_with_empty_value() {

#[test]
fn to_long_with_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long=hello"]);
let raw = clap_lex::RawArgs::new(["bin", "--long=hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -70,7 +70,7 @@ fn to_long_with_value() {

#[test]
fn to_short_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -83,7 +83,7 @@ fn to_short_stdio() {

#[test]
fn to_short_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -95,7 +95,7 @@ fn to_short_escape() {

#[test]
fn to_short_long() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--long"]);
let raw = clap_lex::RawArgs::new(["bin", "--long"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -107,7 +107,7 @@ fn to_short_long() {

#[test]
fn to_short() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -121,7 +121,7 @@ fn to_short() {

#[test]
fn is_negative_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "-10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -131,7 +131,7 @@ fn is_negative_number() {

#[test]
fn is_positive_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -141,7 +141,7 @@ fn is_positive_number() {

#[test]
fn is_not_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--10.0"]);
let raw = clap_lex::RawArgs::new(["bin", "--10.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -151,7 +151,7 @@ fn is_not_number() {

#[test]
fn is_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -161,7 +161,7 @@ fn is_stdio() {

#[test]
fn is_not_stdio() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -171,7 +171,7 @@ fn is_not_stdio() {

#[test]
fn is_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "--"]);
let raw = clap_lex::RawArgs::new(["bin", "--"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -181,7 +181,7 @@ fn is_escape() {

#[test]
fn is_not_escape() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand Down
30 changes: 15 additions & 15 deletions clap_lex/tests/shorts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[test]
fn iter() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -12,7 +12,7 @@ fn iter() {

#[test]
fn next_flag() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -31,7 +31,7 @@ fn next_flag() {

#[test]
fn next_value_os() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -44,7 +44,7 @@ fn next_value_os() {

#[test]
fn next_flag_with_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -58,7 +58,7 @@ fn next_flag_with_value() {

#[test]
fn next_flag_with_no_value() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -75,7 +75,7 @@ fn next_flag_with_no_value() {

#[test]
fn advance_by_nothing() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -89,7 +89,7 @@ fn advance_by_nothing() {

#[test]
fn advance_by_nothing_with_nothing() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -103,7 +103,7 @@ fn advance_by_nothing_with_nothing() {

#[test]
fn advance_by_something() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -117,7 +117,7 @@ fn advance_by_something() {

#[test]
fn advance_by_out_of_bounds() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-short"]);
let raw = clap_lex::RawArgs::new(["bin", "-short"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -131,7 +131,7 @@ fn advance_by_out_of_bounds() {

#[test]
fn is_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -142,7 +142,7 @@ fn is_empty() {

#[test]
fn is_not_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -153,7 +153,7 @@ fn is_not_empty() {

#[test]
fn is_partial_not_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -165,7 +165,7 @@ fn is_partial_not_empty() {

#[test]
fn is_exhausted_empty() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-"]);
let raw = clap_lex::RawArgs::new(["bin", "-"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -177,7 +177,7 @@ fn is_exhausted_empty() {

#[test]
fn is_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-1.0"]);
let raw = clap_lex::RawArgs::new(["bin", "-1.0"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand All @@ -188,7 +188,7 @@ fn is_number() {

#[test]
fn is_not_number() {
let raw = clap_lex::RawArgs::from_iter(["bin", "-hello"]);
let raw = clap_lex::RawArgs::new(["bin", "-hello"]);
let mut cursor = raw.cursor();
assert_eq!(raw.next_os(&mut cursor), Some(std::ffi::OsStr::new("bin")));
let next = raw.next(&mut cursor).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/build/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ impl<'help> App<'help> {
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let mut raw_args = clap_lex::RawArgs::from(itr.into_iter());
let mut raw_args = clap_lex::RawArgs::new(itr.into_iter());
let mut cursor = raw_args.cursor();

#[cfg(feature = "unstable-multicall")]
Expand Down
Loading

0 comments on commit 15d8b3b

Please sign in to comment.