use crate::Name;
use std::fmt;
use std::ops::Deref;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct QualifiedName(Vec<Name>);
impl QualifiedName {
pub fn new(names: &[&Name]) -> Self {
Self(names.iter().map(|&v| v.clone()).collect::<Vec<Name>>())
}
}
impl fmt::Display for QualifiedName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|v| v.to_string()).collect::<Vec<String>>().join("."))
}
}
impl Deref for QualifiedName {
type Target = Vec<Name>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl QualifiedName {
pub fn push(&mut self, name: Name) {
self.0.push(name);
}
pub fn insert(&mut self, index: usize, name: Name) {
self.0.insert(index, name);
}
pub fn pop(&mut self) -> Option<Name> {
self.0.pop()
}
}
impl From<Name> for QualifiedName {
fn from(value: Name) -> Self {
Self(value.to_string().split('.').map(Name::from).collect())
}
}
impl From<Vec<Name>> for QualifiedName {
fn from(value: Vec<Name>) -> Self {
let mut names = vec![];
for name in value {
let mut sub_names = name.to_string().split('.').map(Name::from).collect::<Vec<Name>>();
names.append(&mut sub_names);
}
Self(names)
}
}