use std::{error, fmt};
use indexmap::IndexMap;
use super::Value;
use crate::header::record::value::{map, Map};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Collection {
Unstructured(Vec<String>),
Structured(IndexMap<String, Map<map::Other>>),
}
impl Collection {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
match self {
Self::Unstructured(list) => list.len(),
Self::Structured(map) => map.len(),
}
}
pub(crate) fn add(&mut self, value: Value) -> Result<(), AddError> {
match (self, value) {
(Self::Unstructured(list), Value::String(s)) => {
list.push(s);
Ok(())
}
(Self::Unstructured(_), Value::Map(..)) => Err(AddError::TypeMismatch {
actual: "structured",
expected: "unstructured",
}),
(Self::Structured(map), Value::Map(id, m)) => {
map.insert(id, m);
Ok(())
}
(Self::Structured(_), Value::String(_)) => Err(AddError::TypeMismatch {
actual: "unstructured",
expected: "structured",
}),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AddError {
TypeMismatch {
actual: &'static str,
expected: &'static str,
},
}
impl error::Error for AddError {}
impl fmt::Display for AddError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TypeMismatch { actual, expected } => {
write!(f, "type mismatch: expected {expected}, got {actual}")
}
}
}
}