clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Layer 0: The payload profile `clm.spec.v1`: its constants, its refusal type and its depth rule
//! Canonical Path: kernel/clm_rust_core/src/layer0/spec_profile.rs
//!
//! The scanner in `spec_scan` and the encoder, identities and keys in
//! `spec_identity` share these names. `spec_identity` re-exports the public ones.

use std::fmt;

pub const SPEC_DOMAIN_TAG: &str = "clm.spec.v1";
pub const SPEC_HASH_QUALIFIER: &str = crate::layer0::vocabulary::HASH_ALGORITHM_BLAKE3;
pub const MAX_SAFE_INTEGER: i64 = crate::layer0::limits::SPEC_MAX_SAFE_INTEGER;
pub const MIN_SAFE_INTEGER: i64 = -MAX_SAFE_INTEGER;
/// Deepest nesting of arrays and objects that a payload may have. Anything
/// deeper is refused as malformed, so the recursive walks stay inside a small
/// and known share of the call stack.
pub const MAX_NESTING_DEPTH: usize = crate::layer0::limits::SPEC_MAX_NESTING_DEPTH as usize;
/// Deepest nesting of recipes. One recipe level adds three payload levels and
/// the identity adds one, so this is the deepest recipe whose quoted form still
/// fits in a payload. A deeper recipe is refused as malformed by every function.
pub const MAX_RECIPE_DEPTH: usize = crate::layer0::limits::SPEC_MAX_RECIPE_DEPTH as usize;

pub const REFUSED_FLOAT: &str = "float";
pub const REFUSED_DUPLICATE_KEY: &str = "duplicate_key";
pub const REFUSED_NON_ASCII_KEY: &str = "non_ascii_key";
pub const REFUSED_INTEGER_RANGE: &str = "integer_out_of_range";
pub const REFUSED_UNSUPPORTED_TYPE: &str = "unsupported_type";
pub const REFUSED_INVALID_STRING: &str = "invalid_string";
pub const REFUSED_MALFORMED: &str = "malformed";
pub const REFUSED_ACTION_OPERAND: &str = "action_operand";
pub const REFUSED_UNRESOLVED_INPUT: &str = "unresolved_input";
pub const REFUSED_UNKNOWN_COMPONENT: &str = "unknown_component";

pub const OPERAND_REFERENCE: &str = "reference";
pub const OPERAND_DEFERRED: &str = "deferred";
pub const OPERAND_ACTION: &str = "action";

pub const KIND_RECIPE: &str = "recipe";
pub const KIND_DEEP_KEY: &str = "deep_key";
pub const KIND_SHALLOW_KEY: &str = "shallow_key";

/// A payload or recipe falls outside the profile. `reason` is machine-readable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpecProfileError {
    pub reason: &'static str,
    pub detail: String,
}

impl SpecProfileError {
    pub(crate) fn new(reason: &'static str, detail: impl Into<String>) -> Self {
        Self { reason, detail: detail.into() }
    }
}

impl fmt::Display for SpecProfileError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.reason, self.detail)
    }
}

impl std::error::Error for SpecProfileError {}

pub(crate) type Refusable<T> = Result<T, SpecProfileError>;

pub(crate) fn descend(depth: usize, limit: usize, what: &str) -> Refusable<usize> {
    if depth >= limit {
        return Err(SpecProfileError::new(REFUSED_MALFORMED, format!("{what} nests deeper than {limit} levels")));
    }
    Ok(depth + 1)
}

pub(crate) fn is_safe_integer(integer: i64) -> bool {
    (MIN_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&integer)
}