1include!(concat!(env!("OUT_DIR"), "/embedded.rs"));
32
33pub fn schema(rel_path: &str) -> Option<&'static str> {
37 FILES
38 .binary_search_by(|(path, _)| (*path).cmp(rel_path))
39 .ok()
40 .map(|index| FILES[index].1)
41}
42
43pub fn schema_names() -> &'static [&'static str] {
45 NAMES
46}
47
48pub fn fixture(rel_path: &str) -> Option<&'static str> {
53 let mut key = String::with_capacity("fixtures/".len() + rel_path.len());
54 key.push_str("fixtures/");
55 key.push_str(rel_path);
56 schema(&key)
57}
58
59pub fn schema_by_id(id: &str) -> Option<&'static str> {
62 IDS.iter()
63 .find(|(schema_id, _)| *schema_id == id)
64 .map(|(_, path)| *path)
65}
66
67pub fn schema_ids() -> &'static [(&'static str, &'static str)] {
69 IDS
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use std::collections::BTreeMap;
76 use std::path::{Path, PathBuf};
77
78 fn disk_root() -> PathBuf {
81 let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
82 let repo_root = manifest.join("../../schemas");
83 if repo_root.is_dir() {
84 repo_root
85 } else {
86 manifest.join("schemas")
87 }
88 }
89
90 fn walk(root: &Path, dir: &Path, out: &mut BTreeMap<String, Vec<u8>>) {
91 for entry in std::fs::read_dir(dir).expect("read schemas dir") {
92 let path = entry.expect("dir entry").path();
93 if path.is_dir() {
94 walk(root, &path, out);
95 } else {
96 let rel = path
97 .strip_prefix(root)
98 .expect("path under schemas root")
99 .to_string_lossy()
100 .replace('\\', "/");
101 out.insert(rel, std::fs::read(&path).expect("read schema file"));
102 }
103 }
104 }
105
106 #[test]
107 fn embedded_corpus_matches_disk() {
108 let root = disk_root();
109 let mut on_disk = BTreeMap::new();
110 walk(&root, &root, &mut on_disk);
111
112 assert!(
113 on_disk.len() >= 25,
114 "expected the frozen corpus (>= 25 files), found {} under {}",
115 on_disk.len(),
116 root.display()
117 );
118 assert!(
119 on_disk.contains_key("fixtures/integrity/chain.json"),
120 "the ADR-016 integrity fixtures must be part of the corpus"
121 );
122
123 let mut seen = std::collections::HashSet::new();
124 for (path, _) in FILES {
125 assert!(seen.insert(*path), "duplicate embedded path `{path}`");
126 }
127 assert_eq!(schema_names().len(), FILES.len());
128
129 for (rel, bytes) in &on_disk {
131 let embedded =
132 schema(rel).unwrap_or_else(|| panic!("`{rel}` is not embedded in the crate"));
133 assert_eq!(
134 embedded.as_bytes(),
135 bytes.as_slice(),
136 "embedded `{rel}` diverges from the on-disk contract"
137 );
138 }
139 for (rel, _) in FILES {
141 assert!(
142 on_disk.contains_key(*rel),
143 "embedded `{rel}` is not present on disk"
144 );
145 }
146 }
147
148 #[test]
149 fn exposes_schemas_fixtures_and_ids() {
150 assert!(schema("envelope.json").is_some());
151 assert!(schema("api/catalog.json").is_some());
152 assert!(schema("fixtures/integrity/chain.json").is_some());
153
154 assert!(fixture("integrity/chain.json").is_some());
156 assert!(fixture("envelope/envelope-rich-signed.json").is_some());
157 assert!(fixture("does/not/exist.json").is_none());
158
159 assert_eq!(
160 schema_by_id("https://rigorix.eu/schemas/envelope-v2.json"),
161 Some("envelope.json")
162 );
163 assert_eq!(
164 schema_by_id("https://rigorix.eu/schemas/claims.json"),
165 Some("claims.json")
166 );
167 assert!(schema_ids()
168 .iter()
169 .any(|(id, path)| id.ends_with("/envelope-v2.json") && *path == "envelope.json"));
170 }
171}