use std::{error, fmt};
use bstr::BString;
use super::{Map, OtherFields, tag};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BuildError {
MissingField(&'static str),
InvalidValue(&'static str),
}
impl error::Error for BuildError {}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingField(tag) => write!(f, "missing field: {tag}"),
Self::InvalidValue(tag) => write!(f, "invalid value: {tag}"),
}
}
}
pub trait Inner<I>: Default
where
I: super::Inner,
{
fn build(self) -> Result<I, BuildError>;
}
pub struct Builder<I>
where
I: super::Inner,
{
pub(crate) inner: I::Builder,
other_fields: OtherFields<I::StandardTag>,
}
impl<I> Builder<I>
where
I: super::Inner,
{
pub fn insert<V>(mut self, key: tag::Other<I::StandardTag>, value: V) -> Self
where
V: Into<BString>,
{
self.other_fields.insert(key, value.into());
self
}
pub fn build(self) -> Result<Map<I>, BuildError> {
let inner = self.inner.build()?;
Ok(Map {
inner,
other_fields: self.other_fields,
})
}
}
impl<I> Default for Builder<I>
where
I: super::Inner,
{
fn default() -> Self {
Self {
inner: I::Builder::default(),
other_fields: OtherFields::new(),
}
}
}