Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions clap_builder/src/builder/os_str.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::builder::Str;
use std::borrow::Cow;

/// A UTF-8-encoded fixed string
///
Expand Down Expand Up @@ -126,6 +127,16 @@ impl From<&'_ &'static str> for OsStr {
}
}

#[cfg(feature = "string")]
impl From<Cow<'static, str>> 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<OsStr> for std::ffi::OsString {
fn from(name: OsStr) -> Self {
name.name.into_os_string()
Expand Down Expand Up @@ -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"));
}
}
34 changes: 34 additions & 0 deletions clap_builder/src/builder/str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

/// A UTF-8-encoded fixed string
///
/// <div class="warning">
Expand Down Expand Up @@ -74,6 +76,16 @@ impl From<&'_ &'static str> for Str {
}
}

#[cfg(feature = "string")]
impl From<Cow<'static, str>> 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<Str> for String {
fn from(name: Str) -> Self {
name.name.into_string()
Expand Down Expand Up @@ -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"));
}
}
30 changes: 30 additions & 0 deletions clap_builder/src/builder/styled_str.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -185,6 +187,15 @@ impl From<&'_ &'static str> for StyledStr {
}
}

impl From<Cow<'static, str>> 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> {
Expand Down Expand Up @@ -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"));
}
}
30 changes: 30 additions & 0 deletions clap_builder/src/util/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::builder::Str;
use std::borrow::Cow;

/// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier
///
Expand Down Expand Up @@ -71,6 +72,13 @@ impl From<&'_ &'static str> for Id {
}
}

#[cfg(feature = "string")]
impl From<Cow<'static, str>> for Id {
fn from(name: Cow<'static, str>) -> Self {
Self(name.into())
}
}

impl From<Id> for Str {
fn from(name: Id) -> Self {
name.0
Expand Down Expand Up @@ -162,3 +170,25 @@ impl PartialEq<Id> 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"));
}
}
Loading