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 VariableValue(OsString);
impl VariableValue {
pub fn new(value: impl Into<OsString>) -> Self {
Self(value.into())
}
pub fn get(&self) -> &OsStr {
&self.0
}
}
impl Display for VariableValue {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
write!(formatter, "{}", self.0.to_string_lossy())
}
}
impl From<&str> for VariableValue {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for VariableValue {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&OsStr> for VariableValue {
fn from(value: &OsStr) -> Self {
Self::new(value)
}
}
impl From<OsString> for VariableValue {
fn from(value: OsString) -> Self {
Self::new(value)
}
}
impl From<&Path> for VariableValue {
fn from(value: &Path) -> Self {
Self::new(value)
}
}
impl From<PathBuf> for VariableValue {
fn from(value: PathBuf) -> Self {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn from_os_string_keeps_the_value() {
let value = VariableValue::from(OsString::from("always"));
assert_eq!(value.get(), "always");
}
#[test]
fn from_path_keeps_the_path() {
let value = VariableValue::from(Path::new("target/coverage"));
assert_eq!(value.get(), "target/coverage");
}
#[test]
fn to_string_returns_the_value() {
let value = VariableValue::from("-D warnings");
assert_eq!(value.to_string(), "-D warnings");
}
}