use thiserror::Error;
use crate::block::Strtab;
use crate::block::{AttributeGroups, Attributes};
#[derive(Debug, Error)]
pub enum MapCtxError {
#[error("mapping context requires a version for disambiguation, but none is available")]
NoVersion,
#[error("mapping context requires a string table, but none is available")]
NoStrtab,
#[error("mapping context requires attribute groups, but none are available")]
NoAttributeGroups,
}
#[non_exhaustive]
#[derive(Debug, Default)]
pub struct MapCtx {
pub(crate) version: Option<u64>,
pub(crate) strtab: Option<Strtab>,
pub(crate) attribute_groups: Option<AttributeGroups>,
pub(crate) attributes: Option<Attributes>,
}
impl MapCtx {
pub fn version(&self) -> Result<u64, MapCtxError> {
self.version.ok_or(MapCtxError::NoVersion)
}
pub fn use_strtab(&self) -> Result<bool, MapCtxError> {
self.version().map(|v| v >= 2)
}
pub fn use_relative_ids(&self) -> Result<bool, MapCtxError> {
self.version().map(|v| v >= 1)
}
pub fn strtab(&self) -> Result<&Strtab, MapCtxError> {
self.strtab.as_ref().ok_or(MapCtxError::NoStrtab)
}
pub fn attribute_groups(&self) -> Result<&AttributeGroups, MapCtxError> {
self.attribute_groups
.as_ref()
.ok_or(MapCtxError::NoAttributeGroups)
}
}
pub(crate) trait Mappable<T>: Sized {
type Error;
fn try_map(raw: &T, ctx: &mut MapCtx) -> Result<Self, Self::Error>;
}