#![allow(clippy::len_zero)]
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct NamePath {
path: Vec<String>
}
impl NamePath {
pub fn new<S: Into<String>>(name: S) -> Self {
let name = name.into();
assert!(!name.trim().is_empty(), "path is empty");
NamePath { path: vec![name] }
}
pub fn from_path<S: Into<String>>(path: Vec<S>) -> Self {
assert!(path.len() > 0, "path is invalid");
let path = path.into_iter()
.map(|s| s.into())
.collect::<Vec<String>>();
for s in &path {
assert!(!s.trim().is_empty(), "path is empty");
}
NamePath { path }
}
pub fn name(&self) -> &str {
self.path.last()
.map(|s| s.as_str())
.unwrap()
}
pub fn item_path(&self) -> &[String]{
&self.path[..self.path.len() - 1]
}
pub fn full_path(&self) -> &[String]{
self.path.as_slice()
}
pub fn into_vec(self) -> Vec<String> {
self.path
}
}
impl Display for NamePath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for (i, item) in self.path.iter().enumerate() {
if i > 0 {
write!(f, "::")?;
}
write!(f, "{}", item)?;
}
Ok(())
}
}