use crate::cross_cutting::context::CLMContext;
use crate::layer0::runtime_contract::{ClmExitCode, RUNTIME_CONTRACT_VERSION};
use crate::layer0::vocabulary::HASH_PREFIX_BLAKE3;
pub const CLASSIFIED_CLM_TRIAD: &str = "classified:clm_triad";
pub const CLASSIFIED_UNKNOWN: &str = "classified:unknown";
pub const CLM_DOCUMENT_ROOT: &str = "clm:";
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DispatchError {
NotFound(String),
Unbound(String),
Store(String),
}
impl DispatchError {
pub fn exit_code(&self) -> i32 {
match self {
Self::NotFound(_) | Self::Unbound(_) => ClmExitCode::ContentMissing.code(),
Self::Store(_) => ClmExitCode::GeneralError.code(),
}
}
}
impl std::fmt::Display for DispatchError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFound(route) => write!(f, "OperationNotFoundError: {route}"),
Self::Unbound(op_id) => write!(f, "UnboundDispatchError: {op_id}"),
Self::Store(reason) => f.write_str(reason),
}
}
}
impl std::error::Error for DispatchError {}
impl From<String> for DispatchError {
fn from(reason: String) -> Self {
Self::Store(reason)
}
}
pub fn memorize_content(_ctx: &CLMContext, payload: &str) -> String {
format!("{HASH_PREFIX_BLAKE3}{}", blake3::hash(payload.as_bytes()).to_hex())
}
pub fn classify_yaml(_ctx: &CLMContext, payload: &str) -> String {
let class = if payload.trim_start().starts_with(CLM_DOCUMENT_ROOT) { CLASSIFIED_CLM_TRIAD } else { CLASSIFIED_UNKNOWN };
class.to_string()
}
pub fn probe_version(_ctx: &CLMContext, _payload: &str) -> String {
format!("clm-core:{RUNTIME_CONTRACT_VERSION}")
}