use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
use std::path::Path;
use std::path::PathBuf;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Argument(OsString);
impl Argument {
pub fn new(argument: impl Into<OsString>) -> Self {
Self(argument.into())
}
pub fn get(&self) -> &OsStr {
&self.0
}
}
impl Display for Argument {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
write!(formatter, "{}", self.0.to_string_lossy())
}
}
impl From<&str> for Argument {
fn from(argument: &str) -> Self {
Self::new(argument)
}
}
impl From<String> for Argument {
fn from(argument: String) -> Self {
Self::new(argument)
}
}
impl From<&OsStr> for Argument {
fn from(argument: &OsStr) -> Self {
Self::new(argument)
}
}
impl From<OsString> for Argument {
fn from(argument: OsString) -> Self {
Self::new(argument)
}
}
impl From<&Path> for Argument {
fn from(argument: &Path) -> Self {
Self::new(argument)
}
}
impl From<PathBuf> for Argument {
fn from(argument: PathBuf) -> Self {
Self::new(argument)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn from_os_string_keeps_the_string() {
let argument = Argument::from(OsString::from("--short"));
assert_eq!(argument.get(), "--short");
}
#[test]
fn from_path_keeps_the_path() {
let argument = Argument::from(Path::new("src/main.rs"));
assert_eq!(argument.get(), "src/main.rs");
}
#[test]
fn to_string_returns_the_argument() {
let argument = Argument::from("two words");
assert_eq!(argument.to_string(), "two words");
}
}