use super::*;
use sapio_base::simp::SIMPError;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug, PartialEq, Eq)]
pub struct InputMetadata {
#[serde(flatten)]
pub extra: BTreeMap<String, serde_json::Value>,
pub simp: BTreeMap<i64, serde_json::Value>,
}
impl InputMetadata {
pub fn is_empty(&self) -> bool {
*self == Default::default()
}
pub fn add_simp_inplace<S: SIMPAttachableAt<TemplateInputLT>>(
&mut self,
s: S,
) -> Result<(), SIMPError> {
let old = self.simp.insert(s.get_protocol_number(), s.to_json()?);
if let Some(old) = old {
Err(SIMPError::AlreadyDefined(old))
} else {
Ok(())
}
}
pub fn add_simp<S: SIMPAttachableAt<TemplateInputLT>>(
mut self,
s: S,
) -> Result<Self, SIMPError> {
let old = self.simp.insert(s.get_protocol_number(), s.to_json()?);
if let Some(old) = old {
Err(SIMPError::AlreadyDefined(old))
} else {
Ok(self)
}
}
}
impl Default for InputMetadata {
fn default() -> Self {
InputMetadata {
extra: Default::default(),
simp: Default::default(),
}
}
}
impl<const N: usize> From<[(&str, serde_json::Value); N]> for InputMetadata {
fn from(v: [(&str, serde_json::Value); N]) -> InputMetadata {
InputMetadata {
extra: IntoIterator::into_iter(v)
.map(|(a, b)| (a.into(), b))
.collect(),
simp: Default::default(),
}
}
}