use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct VariableName(OsString);
impl VariableName {
pub fn new(name: impl Into<OsString>) -> Self {
Self(name.into())
}
pub fn get(&self) -> &OsStr {
&self.0
}
}
impl Display for VariableName {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
write!(formatter, "{}", self.0.to_string_lossy())
}
}
impl From<&str> for VariableName {
fn from(name: &str) -> Self {
Self::new(name)
}
}
impl From<String> for VariableName {
fn from(name: String) -> Self {
Self::new(name)
}
}
impl From<&OsStr> for VariableName {
fn from(name: &OsStr) -> Self {
Self::new(name)
}
}
impl From<OsString> for VariableName {
fn from(name: OsString) -> Self {
Self::new(name)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn from_os_string_keeps_the_name() {
let name = VariableName::from(OsString::from("RUSTUP_TOOLCHAIN"));
assert_eq!(name.get(), "RUSTUP_TOOLCHAIN");
}
#[test]
fn to_string_returns_the_name() {
let name = VariableName::from("RUSTUP_TOOLCHAIN");
assert_eq!(name.to_string(), "RUSTUP_TOOLCHAIN");
}
}