diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index e212d2d1..aa9221d5 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -24,6 +24,7 @@ jobs: with: key: ${{ matrix.target }} - run: cargo test + - run: cargo test --no-default-features doc_fmt: name: Document and check formatting diff --git a/Cargo.toml b/Cargo.toml index 67c0e628..44e79cdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" rust-version = "1.66" [dependencies] -unicode-width = "0.2.0" +unicode-width = { version = "0.2.0", optional = true } std = { version = "1.0", package = "rustc-std-workspace-std", optional = true } core = { version = "1.0", package = "rustc-std-workspace-core", optional = true } @@ -18,4 +18,6 @@ core = { version = "1.0", package = "rustc-std-workspace-core", optional = true log = "0.4" [features] -rustc-dep-of-std = ["unicode-width/rustc-dep-of-std", "std", "core"] +default = ["unicode"] +rustc-dep-of-std = ["std", "core"] +unicode = ["dep:unicode-width"] diff --git a/src/lib.rs b/src/lib.rs index 0c47ce61..f5c8e3cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,7 +104,6 @@ #[cfg(test)] #[macro_use] extern crate log; -extern crate unicode_width; use self::Fail::*; use self::HasArg::*; @@ -119,8 +118,21 @@ use std::iter::{repeat, IntoIterator}; use std::result; use std::str::FromStr; +#[cfg(feature = "unicode")] use unicode_width::UnicodeWidthStr; +#[cfg(not(feature = "unicode"))] +trait UnicodeWidthStr { + fn width(&self) -> usize; +} + +#[cfg(not(feature = "unicode"))] +impl UnicodeWidthStr for str { + fn width(&self) -> usize { + self.len() + } +} + #[cfg(test)] mod tests; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4a040871..17e689c4 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -50,9 +50,9 @@ fn test_reqopt() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => { @@ -111,7 +111,7 @@ fn test_optopt() { Ok(ref m) => { assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -119,9 +119,9 @@ fn test_optopt() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -443,9 +443,9 @@ fn test_optmulti() { opts.optmulti("t", "test", "testing", "TEST"); match opts.parse(&long_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -453,9 +453,9 @@ fn test_optmulti() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -576,16 +576,16 @@ fn test_combined() { assert!(m.free[1] == "free1"); assert_eq!(m.opt_str("s").unwrap(), "20"); assert!(m.free[2] == "free2"); - assert!((m.opt_present("flag"))); + assert!(m.opt_present("flag")); assert_eq!(m.opt_str("long").unwrap(), "30"); - assert!((m.opt_present("f"))); + assert!(m.opt_present("f")); let pair = m.opt_strs("m"); assert!(pair[0] == "40"); assert!(pair[1] == "50"); let pair = m.opt_strs("n"); assert!(pair[0] == "-A B"); assert!(pair[1] == "-60 70"); - assert!((!m.opt_present("notpresent"))); + assert!(!m.opt_present("notpresent")); } _ => panic!(), } @@ -888,6 +888,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_description_multibyte_handling() { let mut opts = Options::new(); opts.optflag( @@ -919,6 +920,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_description_newline_handling() { let mut opts = Options::new(); opts.optflag( @@ -950,6 +952,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_multiwidth() { let mut opts = Options::new(); opts.optflag("a", "apple", "apple description");