use dsntk_common::Jsonify;
use std::fmt;
#[derive(Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Clone)]
pub struct Name(String);
impl From<Vec<String>> for Name {
fn from(value: Vec<String>) -> Self {
Self::new(&value.iter().map(|string| string.as_str()).collect::<Vec<&str>>())
}
}
impl From<Vec<&str>> for Name {
fn from(value: Vec<&str>) -> Self {
Self::new(&value)
}
}
impl From<String> for Name {
fn from(value: String) -> Self {
Self(value.trim().to_string())
}
}
impl From<&String> for Name {
fn from(value: &String) -> Self {
Self(value.trim().to_string())
}
}
impl From<&str> for Name {
fn from(value: &str) -> Self {
Self(value.trim().to_string())
}
}
impl From<Name> for String {
fn from(value: Name) -> Self {
value.0
}
}
impl From<&Name> for String {
fn from(value: &Name) -> Self {
value.0.clone()
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Jsonify for Name {
fn jsonify(&self) -> String {
self.0.clone()
}
}
impl Name {
pub fn new(parts: &[&str]) -> Self {
let mut result = String::with_capacity(80);
let mut current;
let mut prev = false;
for (index, part) in parts.iter().map(|s| s.trim()).enumerate() {
current = matches!(part, "." | "/" | "-" | "'" | "+" | "*");
if index > 0 && !prev && !current && !part.is_empty() {
result.push(' ');
}
result.push_str(part);
prev = current;
}
Self(result)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}