diff --git a/clap_builder/src/builder/os_str.rs b/clap_builder/src/builder/os_str.rs index a42066e1997..1177cdd7735 100644 --- a/clap_builder/src/builder/os_str.rs +++ b/clap_builder/src/builder/os_str.rs @@ -1,4 +1,5 @@ use crate::builder::Str; +use std::borrow::Cow; /// A UTF-8-encoded fixed string /// @@ -126,6 +127,16 @@ impl From<&'_ &'static str> for OsStr { } } +#[cfg(feature = "string")] +impl From> for OsStr { + fn from(cow: Cow<'static, str>) -> Self { + match cow { + Cow::Borrowed(s) => Self::from(s), + Cow::Owned(s) => Self::from(s), + } + } +} + impl From for std::ffi::OsString { fn from(name: OsStr) -> Self { name.name.into_os_string() @@ -328,3 +339,25 @@ impl std::hash::Hash for Inner { self.as_os_str().hash(state); } } + +#[cfg(test)] +#[cfg(feature = "string")] +mod tests { + use super::*; + + #[test] + #[cfg(feature = "string")] + fn from_cow_borrowed() { + let cow = Cow::Borrowed("hello"); + let osstr = OsStr::from(cow); + assert_eq!(osstr, OsStr::from("hello")) + } + + #[test] + #[cfg(feature = "string")] + fn from_cow_owned() { + let cow = Cow::Owned("world".to_string()); + let osstr = OsStr::from(cow); + assert_eq!(osstr, OsStr::from("world")); + } +} diff --git a/clap_builder/src/builder/str.rs b/clap_builder/src/builder/str.rs index b812def4029..5329c05d575 100644 --- a/clap_builder/src/builder/str.rs +++ b/clap_builder/src/builder/str.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + /// A UTF-8-encoded fixed string /// ///
@@ -74,6 +76,16 @@ impl From<&'_ &'static str> for Str { } } +#[cfg(feature = "string")] +impl From> for Str { + fn from(cow: Cow<'static, str>) -> Self { + match cow { + Cow::Borrowed(s) => Self::from(s), + Cow::Owned(s) => Self::from(s), + } + } +} + impl From for String { fn from(name: Str) -> Self { name.name.into_string() @@ -312,3 +324,25 @@ impl std::hash::Hash for Inner { self.as_str().hash(state); } } + +#[cfg(test)] +#[cfg(feature = "string")] +mod tests { + use super::*; + + #[test] + #[cfg(feature = "string")] + fn from_cow_borrowed() { + let cow = Cow::Borrowed("hello"); + let str = Str::from(cow); + assert_eq!(str, Str::from("hello")) + } + + #[test] + #[cfg(feature = "string")] + fn from_cow_owned() { + let cow = Cow::Owned("world".to_string()); + let str = Str::from(cow); + assert_eq!(str, Str::from("world")); + } +} diff --git a/clap_builder/src/builder/styled_str.rs b/clap_builder/src/builder/styled_str.rs index 90b666239dc..4eee4bd0b18 100644 --- a/clap_builder/src/builder/styled_str.rs +++ b/clap_builder/src/builder/styled_str.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "usage"), allow(dead_code))] +use std::borrow::Cow; + /// Terminal-styling container /// /// Styling may be encoded as [ANSI Escape Code](https://en.wikipedia.org/wiki/ANSI_escape_code) @@ -185,6 +187,15 @@ impl From<&'_ &'static str> for StyledStr { } } +impl From> for StyledStr { + fn from(cow: Cow<'static, str>) -> Self { + match cow { + Cow::Borrowed(s) => StyledStr::from(s), + Cow::Owned(s) => StyledStr::from(s), + } + } +} + impl std::fmt::Write for StyledStr { #[inline] fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> { @@ -252,3 +263,22 @@ mod wrap_tests { ); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_cow_borrowed() { + let cow = Cow::Borrowed("hello"); + let styled = StyledStr::from(cow); + assert_eq!(styled, StyledStr::from("hello")) + } + + #[test] + fn from_cow_owned() { + let cow = Cow::Owned("world".to_string()); + let styled = StyledStr::from(cow); + assert_eq!(styled, StyledStr::from("world")); + } +} diff --git a/clap_builder/src/util/id.rs b/clap_builder/src/util/id.rs index c2bc545eb10..a6221368a58 100644 --- a/clap_builder/src/util/id.rs +++ b/clap_builder/src/util/id.rs @@ -1,4 +1,5 @@ use crate::builder::Str; +use std::borrow::Cow; /// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier /// @@ -71,6 +72,13 @@ impl From<&'_ &'static str> for Id { } } +#[cfg(feature = "string")] +impl From> for Id { + fn from(name: Cow<'static, str>) -> Self { + Self(name.into()) + } +} + impl From for Str { fn from(name: Id) -> Self { name.0 @@ -162,3 +170,25 @@ impl PartialEq for String { PartialEq::eq(other, self) } } + +#[cfg(test)] +#[cfg(feature = "string")] +mod tests { + use super::*; + + #[test] + #[cfg(feature = "string")] + fn from_cow_borrowed() { + let cow = Cow::Borrowed("hello"); + let id = Id::from(cow); + assert_eq!(id, Id::from("hello")) + } + + #[test] + #[cfg(feature = "string")] + fn from_cow_owned() { + let cow = Cow::Owned("world".to_string()); + let id = Id::from(cow); + assert_eq!(id, Id::from("world")); + } +} \ No newline at end of file