|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// ABI manifest schema — the Idris2-derived, language-neutral |
| 5 | +// description of a cartridge's ABI surface. The verifier (verify.rs) |
| 6 | +// diffs this against a parsed Zig FFI file to detect drift. |
| 7 | +// |
| 8 | +// Phase 1 of standards#89 sub-issue 3 / standards#92. |
| 9 | +// Phase 1b will emit this from the Idris2 build instead of hand-authoring. |
| 10 | + |
| 11 | +use serde::{Deserialize, Serialize}; |
| 12 | + |
| 13 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 14 | +pub struct AbiManifest { |
| 15 | + pub schema_version: String, |
| 16 | + pub cartridge: String, |
| 17 | + pub source_idris: String, |
| 18 | + pub enums: Vec<EnumDecl>, |
| 19 | + #[serde(default)] |
| 20 | + pub transition_table: Option<TransitionTable>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 24 | +pub struct EnumDecl { |
| 25 | + pub name: String, |
| 26 | + pub variants: Vec<EnumVariant>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 30 | +pub struct EnumVariant { |
| 31 | + pub name: String, |
| 32 | + pub value: i64, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 36 | +pub struct TransitionTable { |
| 37 | + pub state_enum: String, |
| 38 | + pub rows: Vec<TransitionRow>, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 42 | +pub struct TransitionRow { |
| 43 | + pub from: String, |
| 44 | + pub to: String, |
| 45 | + pub allowed: bool, |
| 46 | +} |
| 47 | + |
| 48 | +/// CamelCase → snake_case using the Zig convention used by hyperpolymath |
| 49 | +/// cartridges: underscore is inserted before every uppercase letter whose |
| 50 | +/// previous character is a lowercase letter or digit, then the whole string |
| 51 | +/// is lowercased. Examples (hand-verified against both real cartridges): |
| 52 | +/// `ManifestLoaded` → `manifest_loaded` |
| 53 | +/// `K9Error` → `k9_error` |
| 54 | +/// `SsgError` → `ssg_error` |
| 55 | +/// `ReadyToDeploy` → `ready_to_deploy` |
| 56 | +pub fn to_snake_case(s: &str) -> String { |
| 57 | + let chars: Vec<char> = s.chars().collect(); |
| 58 | + let mut out = String::with_capacity(s.len() + 4); |
| 59 | + for (i, c) in chars.iter().enumerate() { |
| 60 | + if i > 0 && c.is_ascii_uppercase() { |
| 61 | + let prev = chars[i - 1]; |
| 62 | + if prev.is_ascii_lowercase() || prev.is_ascii_digit() { |
| 63 | + out.push('_'); |
| 64 | + } |
| 65 | + } |
| 66 | + out.push(c.to_ascii_lowercase()); |
| 67 | + } |
| 68 | + out |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn snake_case_matches_cartridge_conventions() { |
| 77 | + assert_eq!(to_snake_case("Empty"), "empty"); |
| 78 | + assert_eq!(to_snake_case("ManifestLoaded"), "manifest_loaded"); |
| 79 | + assert_eq!(to_snake_case("ContentLoaded"), "content_loaded"); |
| 80 | + assert_eq!(to_snake_case("K9Error"), "k9_error"); |
| 81 | + assert_eq!(to_snake_case("SsgError"), "ssg_error"); |
| 82 | + assert_eq!(to_snake_case("ReadyToDeploy"), "ready_to_deploy"); |
| 83 | + assert_eq!(to_snake_case("Hugo"), "hugo"); |
| 84 | + } |
| 85 | +} |
0 commit comments