Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

plugin/host/src/named.rs

//! **holger's frozen codes on guest-host's typed refusals.**
//!
//! `guest-host` (edda) refuses with typed errors and a plain-language
//! `Display`; it carries no application error registry, which is what let it
//! leave this crate without taking `holger-errcode` along. This file is the
//! seam back: every refusal a loaded module can produce is given the
//! `holger-plugin-<n>` code it always had, so an operator's log, a test's
//! assertion and `holger plugin describe` all read as before.

use holger_errcode as ec;

use crate::verify::TrustError;

/// The code a trust refusal is raised under.
pub fn trust_code(e: &TrustError) -> &'static ec::ErrCode {
    match e {
        TrustError::RootsUnreadable { .. } => &ec::PLUGIN_TRUST_ROOTS_UNREADABLE,
        TrustError::Unsigned { .. } => &ec::PLUGIN_MODULE_UNSIGNED,
        TrustError::SignatureInvalid { .. } => &ec::PLUGIN_SIGNATURE_INVALID,
        TrustError::VerifierUnavailable { .. } => &ec::PLUGIN_VERIFICATION_UNAVAILABLE,
    }
}

/// `[holger-plugin-n] <refusal>` for a trust refusal.
pub fn trust_refusal(e: &TrustError) -> String {
    trust_code(e).tag(e)
}

/// `[holger-plugin-5] <refusal>` for a zero limit.
pub fn limit_refusal(e: &guest_host::LimitZero) -> String {
    ec::PLUGIN_LIMIT_MISCONFIGURED.tag(e)
}

#[cfg(feature = "wasm-plugins")]
pub use runtime::*;

#[cfg(feature = "wasm-plugins")]
mod runtime {
    use super::*;
    use guest_host::{Bound, CallError, LoadError};

    /// The frozen code this bound is raised under.
    pub fn bound_code(b: Bound) -> &'static ec::ErrCode {
        match b {
            Bound::Fuel => &ec::PLUGIN_FUEL_EXHAUSTED,
            Bound::Deadline => &ec::PLUGIN_DEADLINE_EXCEEDED,
            Bound::Memory => &ec::PLUGIN_MEMORY_LIMIT,
            Bound::Table => &ec::PLUGIN_TABLE_LIMIT,
        }
    }

    /// A load refusal as the string every holger caller reads, with its code
    /// where it has one (trust, limits) and the plain message otherwise.
    pub fn load_refusal(e: LoadError) -> String {
        match e {
            LoadError::Trust(t) => trust_refusal(&t),
            LoadError::Limits(z) => limit_refusal(&z),
            other => other.to_string(),
        }
    }

    /// A call refusal as a string, with the bound's code where a bound fired.
    pub fn call_refusal(e: CallError) -> String {
        match e {
            CallError::Bound { bound, refusal } => bound_code(bound).tag(refusal),
            other => other.to_string(),
        }
    }
}