1#![doc = include_str!("../README.md")]
2#![no_std]
3
4extern crate alloc;
5
6use alloc::borrow::ToOwned;
7use alloc::collections::BTreeMap;
8use alloc::string::String;
9use alloc::vec;
10use alloc::vec::Vec;
11
12use schemars::{JsonSchema, schema_for};
13use serde::{Deserialize, Serialize};
14use serde_json::Value;
15
16mod compiled;
17pub use compiled::{CompiledCatalog, SchemaMatch};
18
19pub const DEFAULT_SCHEMA_URL: &str =
21 "https://catalog.lintel.tools/schemas/lintel/catalog/latest.json";
22
23#[derive(Debug, Clone, Default, Deserialize, JsonSchema)]
31#[schemars(title = "Schema Catalog")]
32pub struct Catalog {
33 #[serde(default, rename = "$schema")]
35 pub schema: Option<String>,
36 #[serde(default = "default_version")]
38 pub version: u32,
39 #[serde(default)]
41 pub title: Option<String>,
42 pub schemas: Vec<SchemaEntry>,
44 #[serde(default)]
48 pub groups: Vec<CatalogGroup>,
49}
50
51impl Serialize for Catalog {
52 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
53 use serde::ser::SerializeMap;
54
55 let mut len = 3; if self.title.is_some() {
57 len += 1;
58 }
59 if !self.groups.is_empty() {
60 len += 1;
61 }
62
63 let mut map = serializer.serialize_map(Some(len))?;
64 map.serialize_entry(
65 "$schema",
66 self.schema.as_deref().unwrap_or(DEFAULT_SCHEMA_URL),
67 )?;
68 map.serialize_entry("version", &self.version)?;
69 if let Some(ref title) = self.title {
70 map.serialize_entry("title", title)?;
71 }
72 map.serialize_entry("schemas", &self.schemas)?;
73 if !self.groups.is_empty() {
74 map.serialize_entry("groups", &self.groups)?;
75 }
76 map.end()
77 }
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
85#[schemars(title = "Schema Group")]
86pub struct CatalogGroup {
87 pub name: String,
89 pub description: String,
91 pub schemas: Vec<String>,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
99#[schemars(title = "Schema Entry")]
100pub struct SchemaEntry {
101 #[schemars(example = example_schema_name())]
103 pub name: String,
104 #[serde(default)]
106 pub description: String,
107 #[schemars(example = example_schema_url())]
109 pub url: String,
110 #[serde(default, rename = "sourceUrl", skip_serializing_if = "Option::is_none")]
113 pub source_url: Option<String>,
114 #[serde(default, rename = "fileMatch", skip_serializing_if = "Vec::is_empty")]
119 #[schemars(title = "File Match")]
120 #[schemars(example = example_file_match())]
121 pub file_match: Vec<String>,
122 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
125 pub versions: BTreeMap<String, String>,
126}
127
128fn default_version() -> u32 {
129 1
130}
131
132fn example_schema_name() -> String {
133 "My Config".to_owned()
134}
135
136fn example_schema_url() -> String {
137 "https://example.com/schemas/my-config.json".to_owned()
138}
139
140fn example_file_match() -> Vec<String> {
141 vec!["*.config.json".to_owned(), "**/.config.json".to_owned()]
142}
143
144pub fn schema() -> Value {
150 serde_json::to_value(schema_for!(Catalog)).expect("schema serialization cannot fail")
151}
152
153pub fn parse_catalog(json: &str) -> Result<Catalog, serde_json::Error> {
159 serde_json::from_str(json)
160}
161
162pub fn parse_catalog_value(value: serde_json::Value) -> Result<Catalog, serde_json::Error> {
168 serde_json::from_value(value)
169}
170
171#[cfg(test)]
172mod tests {
173 use alloc::string::ToString;
174
175 use super::*;
176
177 #[test]
178 fn round_trip_catalog() {
179 let catalog = Catalog {
180 version: 1,
181 schemas: vec![SchemaEntry {
182 name: "Test Schema".into(),
183 description: "A test schema".into(),
184 url: "https://example.com/test.json".into(),
185 source_url: None,
186 file_match: vec!["*.test.json".into()],
187 versions: BTreeMap::new(),
188 }],
189 ..Catalog::default()
190 };
191 let json = serde_json::to_string_pretty(&catalog).expect("serialize");
192 let parsed: Catalog = serde_json::from_str(&json).expect("deserialize");
193 assert_eq!(parsed.version, 1);
194 assert_eq!(parsed.schemas.len(), 1);
195 assert_eq!(parsed.schemas[0].name, "Test Schema");
196 assert_eq!(parsed.schemas[0].file_match, vec!["*.test.json"]);
197 }
198
199 #[test]
200 fn parse_catalog_from_json_string() {
201 let json = r#"{"version":1,"schemas":[{"name":"test","description":"desc","url":"https://example.com/s.json","fileMatch":["*.json"]}]}"#;
202 let catalog = parse_catalog(json).expect("parse");
203 assert_eq!(catalog.schemas.len(), 1);
204 assert_eq!(catalog.schemas[0].name, "test");
205 assert_eq!(catalog.schemas[0].file_match, vec!["*.json"]);
206 }
207
208 #[test]
209 fn empty_file_match_omitted_in_serialization() {
210 let entry = SchemaEntry {
211 name: "No Match".into(),
212 description: "desc".into(),
213 url: "https://example.com/no.json".into(),
214 source_url: None,
215 file_match: vec![],
216 versions: BTreeMap::new(),
217 };
218 let json = serde_json::to_string(&entry).expect("serialize");
219 assert!(!json.contains("fileMatch"));
220 assert!(!json.contains("sourceUrl"));
221 assert!(!json.contains("versions"));
222 }
223
224 #[test]
225 fn source_url_serialized_as_camel_case() {
226 let entry = SchemaEntry {
227 name: "Test".into(),
228 description: "desc".into(),
229 url: "https://catalog.example.com/test.json".into(),
230 source_url: Some("https://upstream.example.com/test.json".into()),
231 file_match: vec![],
232 versions: BTreeMap::new(),
233 };
234 let json = serde_json::to_string(&entry).expect("serialize");
235 assert!(json.contains("\"sourceUrl\""));
236 assert!(json.contains("https://upstream.example.com/test.json"));
237
238 let parsed: SchemaEntry = serde_json::from_str(&json).expect("deserialize");
240 assert_eq!(
241 parsed.source_url.as_deref(),
242 Some("https://upstream.example.com/test.json")
243 );
244 }
245
246 #[test]
247 fn deserialize_with_versions() {
248 let json = r#"{
249 "version": 1,
250 "schemas": [{
251 "name": "test",
252 "description": "desc",
253 "url": "https://example.com/s.json",
254 "versions": {"draft-07": "https://example.com/draft07.json"}
255 }]
256 }"#;
257 let catalog = parse_catalog(json).expect("parse");
258 assert_eq!(
259 catalog.schemas[0].versions.get("draft-07"),
260 Some(&"https://example.com/draft07.json".to_string())
261 );
262 }
263
264 #[test]
265 fn schema_has_camel_case_properties() {
266 let schema = schema();
267 let schema_str = serde_json::to_string(&schema).expect("serialize");
268 assert!(
269 schema_str.contains("fileMatch"),
270 "schema should contain fileMatch"
271 );
272 assert!(
273 schema_str.contains("sourceUrl"),
274 "schema should contain sourceUrl"
275 );
276 }
277}