lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

lib.rs (2616B)


      1 #![cfg_attr(coverage_nightly, feature(coverage_attribute))]
      2 #![no_std]
      3 #![forbid(unsafe_code)]
      4 
      5 extern crate alloc;
      6 
      7 use alloc::string::String;
      8 use core::fmt;
      9 use sha2::{Digest, Sha256};
     10 
     11 mod schema_validation;
     12 
     13 pub const RADROOTS_MESH_AGENT_SCHEMA_ID: &str = "0xb83e0c4f71838d9a";
     14 pub const RADROOTS_MESH_AGENT_SCHEMA_NAMESPACE: &str = "radroots::mesh_agent::v1";
     15 pub const RADROOTS_MESH_AGENT_SCHEMA: &str = include_str!("../schema/radroots_mesh_agent_v1.capnp");
     16 
     17 #[derive(Clone, Debug, PartialEq, Eq)]
     18 pub enum RadrootsMeshAgentProtoError {
     19     InvalidSchema,
     20     MissingSchemaId,
     21     MissingNamespace,
     22     MissingRequest,
     23     MissingAction,
     24     MissingResponse,
     25     MissingReceipt,
     26     MissingStatusSurface,
     27     MissingPublishSurface,
     28     MissingError,
     29 }
     30 
     31 impl fmt::Display for RadrootsMeshAgentProtoError {
     32     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     33         match self {
     34             Self::InvalidSchema => f.write_str("mesh agent schema is invalid"),
     35             Self::MissingSchemaId => f.write_str("mesh agent schema id is missing"),
     36             Self::MissingNamespace => f.write_str("mesh agent schema namespace is missing"),
     37             Self::MissingRequest => f.write_str("mesh agent request schema is missing"),
     38             Self::MissingAction => f.write_str("mesh agent action schema is missing"),
     39             Self::MissingResponse => f.write_str("mesh agent response schema is missing"),
     40             Self::MissingReceipt => f.write_str("mesh agent receipt schema is missing"),
     41             Self::MissingStatusSurface => {
     42                 f.write_str("mesh agent status schema surface is missing")
     43             }
     44             Self::MissingPublishSurface => {
     45                 f.write_str("mesh agent publish schema surface is missing")
     46             }
     47             Self::MissingError => f.write_str("mesh agent error schema is missing"),
     48         }
     49     }
     50 }
     51 
     52 pub fn schema_sha256_hex() -> String {
     53     let mut hasher = Sha256::new();
     54     hasher.update(RADROOTS_MESH_AGENT_SCHEMA.as_bytes());
     55     hex_encode(&hasher.finalize())
     56 }
     57 
     58 fn hex_encode(bytes: &[u8]) -> String {
     59     const HEX: &[u8; 16] = b"0123456789abcdef";
     60     let mut out = String::with_capacity(bytes.len() * 2);
     61     for byte in bytes {
     62         out.push(HEX[(byte >> 4) as usize] as char);
     63         out.push(HEX[(byte & 0x0f) as usize] as char);
     64     }
     65     out
     66 }
     67 
     68 pub fn validate_schema() -> Result<(), RadrootsMeshAgentProtoError> {
     69     validate_schema_text(RADROOTS_MESH_AGENT_SCHEMA)
     70 }
     71 
     72 pub fn validate_schema_text(schema: &str) -> Result<(), RadrootsMeshAgentProtoError> {
     73     schema_validation::validate_schema_text(schema)
     74 }