mod display;
pub use display::*;
use std::ops::{Deref, DerefMut};
const FMT_PADDING: &str = "\t";
#[repr(transparent)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Vmf<S> {
pub inner: Block<S>,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Block<S> {
pub name: S,
pub props: Vec<Property<S, S>>,
pub blocks: Vec<Block<S>>,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Property<K, V> {
pub key: K,
pub value: V,
}
impl<S> Vmf<S> {
pub const ROOT_NAME: &str = "root";
pub fn root(&self) -> &Block<S> {
&self.inner
}
pub fn root_mut(&mut self) -> &mut Block<S> {
&mut self.inner
}
}
impl<'a, S: From<&'a str>> Vmf<S> {
pub fn new(blocks: Vec<Block<S>>) -> Self {
Self { inner: Block::new(Self::ROOT_NAME, vec![], blocks) }
}
}
impl<S> Block<S> {
pub fn new<T: Into<S>>(name: T, props: Vec<Property<S, S>>, blocks: Vec<Block<S>>) -> Self {
Self { name: name.into(), props, blocks }
}
pub fn iter_children(&self) -> impl Iterator<Item = &Self> {
self.blocks.iter()
}
}
impl<S, V> Property<S, V> {
pub fn new<T: Into<S>, U: Into<V>>(key: T, value: U) -> Self {
Self { key: key.into(), value: value.into() }
}
}
impl<S: AsRef<str>, V> Property<S, V> {
pub fn is_id(&self) -> bool {
self.key.as_ref() == "id"
}
}
impl<'a, S: From<&'a str>> Default for Vmf<S> {
fn default() -> Self {
Self { inner: Block::new(Self::ROOT_NAME, vec![], vec![]) }
}
}
impl<S> AsRef<Block<S>> for Vmf<S> {
fn as_ref(&self) -> &Block<S> {
&self.inner
}
}
impl<S> AsMut<Block<S>> for Vmf<S> {
fn as_mut(&mut self) -> &mut Block<S> {
&mut self.inner
}
}
impl<S> Deref for Vmf<S> {
type Target = Block<S>;
fn deref(&self) -> &Block<S> {
&self.inner
}
}
impl<S> DerefMut for Vmf<S> {
fn deref_mut(&mut self) -> &mut Block<S> {
&mut self.inner
}
}
impl<S> From<Vmf<S>> for Block<S> {
fn from(vmf: Vmf<S>) -> Self {
vmf.inner
}
}