noodles_vcf/header/record/value/
collection.rsuse 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)) => try_insert(map, id, m),
(Self::Structured(_), Value::String(_)) => Err(AddError::TypeMismatch {
actual: "unstructured",
expected: "structured",
}),
}
}
}
fn try_insert(
map: &mut IndexMap<String, Map<map::Other>>,
id: String,
m: Map<map::Other>,
) -> Result<(), AddError> {
use indexmap::map::Entry;
match map.entry(id) {
Entry::Vacant(entry) => {
entry.insert(m);
Ok(())
}
Entry::Occupied(entry) => {
let (id, _) = entry.swap_remove_entry();
Err(AddError::DuplicateId(id))
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AddError {
TypeMismatch {
actual: &'static str,
expected: &'static str,
},
DuplicateId(String),
}
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}")
}
Self::DuplicateId(id) => write!(f, "duplicate ID: {id}"),
}
}
}