use std::{str::FromStr, fmt::Display};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SlopValue {
String(String),
List(Vec<String>),
}
impl SlopValue {
#[inline(always)]
pub fn is_string(&self) -> bool {
if let Self::String(_) = self { true } else { false }
}
#[inline(always)]
pub fn is_list(&self) -> bool {
if let Self::List(_) = self { true } else { false }
}
#[inline(always)]
pub fn string(&self) -> Option<&String> {
if let Self::String(s) = self { Some(&s) } else { None }
}
#[inline(always)]
pub fn list(&self) -> Option<&Vec<String>> {
if let Self::List(l) = self { Some(&l) } else { None }
}
#[inline]
pub fn parse_into<T>(&self) -> Option<Result<T, T::Err>> where T: FromStr {
if let Some(s) = self.string() {
Some(s.parse())
} else {
None
}
}
#[inline]
pub fn to_string_pretty(&self) -> String {
match self {
Self::String(s) => format!("={s}"),
Self::List(l) => format!("{{\n {}\n}}", l.join("\n ")),
}
}
}
impl Display for SlopValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(s) => write!(f, "={s}"),
Self::List(l) => write!(f, "{{\n{}\n}}", l.join("\n")),
}
}
}
impl From<String> for SlopValue {
#[inline(always)]
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&str> for SlopValue {
#[inline(always)]
fn from(value: &str) -> Self {
Self::String(value.to_string())
}
}
impl From<Vec<String>> for SlopValue {
#[inline(always)]
fn from(value: Vec<String>) -> Self {
Self::List(value)
}
}
impl From<Vec<&str>> for SlopValue {
#[inline(always)]
fn from(value: Vec<&str>) -> Self {
Self::List(value.into_iter().map(|s| s.to_string()).collect())
}
}
impl From<&[String]> for SlopValue {
#[inline(always)]
fn from(value: &[String]) -> Self {
Self::List(value.to_owned())
}
}
impl From<&[&str]> for SlopValue {
#[inline(always)]
fn from(value: &[&str]) -> Self {
Self::List(value.into_iter().map(|s| s.to_string()).collect())
}
}