Skip to main content

openakb_validate/
catalog.rs

1//! Stable code catalog.
2
3use std::fmt;
4
5use serde::ser::{Serialize, Serializer};
6
7/// Maximum allowed depth for parent chains.
8pub const PARENT_DEPTH_MAX: usize = 64;
9
10/// Maximum structural nesting depth a descriptor may reach before
11/// [`crate::validate`] declines the recursive schema walk and reports `AKB011`
12/// instead of risking a stack-overflow abort.
13///
14/// [`serde_json::from_str`] caps deserialization nesting at 128, so every
15/// descriptor obtained by parsing text has a structural depth of at most 128.
16/// This cap sits an order of magnitude above that, so no parsed descriptor can
17/// ever reach it (zero false positives on real input), while staying far below
18/// the empirically observed ~150,000-level depth at which the recursive
19/// `jsonschema` structural walk overflows the stack and aborts. The wide margin
20/// on both sides keeps validation total without rejecting any realistic input.
21pub const STRUCTURAL_DEPTH_MAX: usize = 1024;
22
23/// Maximum length of an inline `[cite:]` id token (spec §4.4).
24pub const CITE_ID_MAX_LENGTH: usize = 64;
25
26/// Inline `[cite:]` id-token characters (spec §4.4): a case-insensitive superset
27/// of the typed source-id form. Hyphen last so this doubles as a regex class body.
28pub const CITE_ID_CHARSET: &str = "A-Za-z0-9_-";
29
30/// Stable OpenAKB diagnostic code.
31#[allow(missing_docs)] // Variant meanings are exposed through Code::name and fixed by the catalog.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
33pub enum Code {
34    Akb001,
35    Akb002,
36    Akb003,
37    Akb004,
38    Akb005,
39    Akb006,
40    Akb007,
41    Akb008,
42    Akb009,
43    Akb010,
44    Akb011,
45    Akb012,
46}
47
48impl Code {
49    /// All stable diagnostic codes in ascending order.
50    pub const ALL: [Self; 12] = [
51        Self::Akb001,
52        Self::Akb002,
53        Self::Akb003,
54        Self::Akb004,
55        Self::Akb005,
56        Self::Akb006,
57        Self::Akb007,
58        Self::Akb008,
59        Self::Akb009,
60        Self::Akb010,
61        Self::Akb011,
62        Self::Akb012,
63    ];
64
65    /// Returns the wire spelling of this diagnostic code.
66    #[must_use]
67    pub const fn as_str(self) -> &'static str {
68        match self {
69            Self::Akb001 => "AKB001",
70            Self::Akb002 => "AKB002",
71            Self::Akb003 => "AKB003",
72            Self::Akb004 => "AKB004",
73            Self::Akb005 => "AKB005",
74            Self::Akb006 => "AKB006",
75            Self::Akb007 => "AKB007",
76            Self::Akb008 => "AKB008",
77            Self::Akb009 => "AKB009",
78            Self::Akb010 => "AKB010",
79            Self::Akb011 => "AKB011",
80            Self::Akb012 => "AKB012",
81        }
82    }
83
84    /// Returns the stable human-readable name for this diagnostic code.
85    #[must_use]
86    pub const fn name(self) -> &'static str {
87        match self {
88            Self::Akb001 => "id-not-unique",
89            Self::Akb002 => "empty-section",
90            Self::Akb003 => "missing-source-cite",
91            Self::Akb004 => "parent-cycle",
92            Self::Akb005 => "cap-exceeded",
93            Self::Akb006 => "unknown-core-property",
94            Self::Akb007 => "unresolved-reference",
95            Self::Akb008 => "unknown-rel",
96            Self::Akb009 => "missing-required-field",
97            Self::Akb010 => "invalid-reference-kind",
98            Self::Akb011 => "malformed-value",
99            Self::Akb012 => "link-missing-target",
100        }
101    }
102}
103
104impl fmt::Display for Code {
105    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
106        formatter.write_str(self.as_str())
107    }
108}
109
110impl Serialize for Code {
111    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
112    where
113        S: Serializer,
114    {
115        serializer.serialize_str(self.as_str())
116    }
117}