cairo_annotations/annotations/
traits.rsuse cairo_lang_sierra::debug_info::DebugInfo;
use serde::de::DeserializeOwned;
use thiserror::Error;
pub trait TryFromDebugInfo: Sized {
type Error: std::error::Error;
fn try_from_debug_info(sierra_debug_info: &DebugInfo) -> Result<Self, Self::Error>;
}
#[derive(Debug, Error)]
pub enum AnnotationsError {
#[error("Missing namespace: {0}")]
MissingNamespace(String),
#[error("Deserialization error: {0}")]
DeserializationError(#[from] serde_json::Error),
}
pub(crate) trait Namespace {
const NAMESPACE: &'static str;
}
impl<T> TryFromDebugInfo for T
where
T: Namespace + DeserializeOwned,
{
type Error = AnnotationsError;
fn try_from_debug_info(sierra_debug_info: &DebugInfo) -> Result<Self, Self::Error> {
let value = sierra_debug_info
.annotations
.get(Self::NAMESPACE)
.ok_or_else(|| AnnotationsError::MissingNamespace(Self::NAMESPACE.into()))?;
serde_json::from_value(value.clone()).map_err(AnnotationsError::DeserializationError)
}
}