qualia_cooperative_core/qapp_package/
manifest.rs1use serde::{Deserialize, Serialize};
15
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19#[serde(rename_all = "snake_case")]
20pub enum QappKind {
21 Cooperative,
22 Health,
23 Journal,
24 Directory,
25 Custom(String),
26}
27
28impl QappKind {
29 pub fn as_str(&self) -> &str {
32 match self {
33 QappKind::Cooperative => "cooperative",
34 QappKind::Health => "health",
35 QappKind::Journal => "journal",
36 QappKind::Directory => "directory",
37 QappKind::Custom(id) => id.as_str(),
38 }
39 }
40}
41
42impl Default for QappKind {
43 fn default() -> Self {
44 QappKind::Cooperative
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51#[serde(rename_all = "snake_case")]
52pub enum Capability {
53 ReadRecords,
54 WriteRecords,
55 Sync,
56 BlobStore,
57 Notifications,
58 Camera,
59 Custom(String),
60}
61
62impl Capability {
63 pub fn as_str(&self) -> &str {
65 match self {
66 Capability::ReadRecords => "read_records",
67 Capability::WriteRecords => "write_records",
68 Capability::Sync => "sync",
69 Capability::BlobStore => "blob_store",
70 Capability::Notifications => "notifications",
71 Capability::Camera => "camera",
72 Capability::Custom(id) => id.as_str(),
73 }
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct WasmRef {
82 pub path: String,
85 pub sha256_hex: String,
87 pub size_bytes: u64,
89}
90
91impl Default for WasmRef {
92 fn default() -> Self {
93 Self {
94 path: "./qapp.wasm".to_string(),
95 sha256_hex: String::new(),
96 size_bytes: 0,
97 }
98 }
99}
100
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub struct IconRef {
104 pub src: String,
106 pub sizes: String,
108 pub purpose: String,
110}
111
112#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
115pub struct QappManifest {
116 pub id: String,
118 pub name: String,
120 pub short_name: String,
122 pub version: String,
124 pub description: String,
126 pub kind: QappKind,
128 pub capabilities: Vec<Capability>,
130 pub entry_wasm: WasmRef,
132 pub icons: Vec<IconRef>,
134 pub theme_color: String,
136 pub background_color: String,
138 pub display: String,
140 pub offline: bool,
142}
143
144impl QappManifest {
145 pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
148 let name = name.into();
149 Self {
150 id: id.into(),
151 short_name: name.clone(),
152 name,
153 version: "0.1.0".to_string(),
154 description: String::new(),
155 kind: QappKind::default(),
156 capabilities: Vec::new(),
157 entry_wasm: WasmRef::default(),
158 icons: Vec::new(),
159 theme_color: "#101418".to_string(),
160 background_color: "#0b0d10".to_string(),
161 display: "standalone".to_string(),
162 offline: true,
163 }
164 }
165
166 pub fn with_kind(mut self, kind: QappKind) -> Self {
168 self.kind = kind;
169 self
170 }
171
172 pub fn with_capability(mut self, capability: Capability) -> Self {
174 self.capabilities.push(capability);
175 self
176 }
177
178 pub fn with_entry(mut self, entry_wasm: WasmRef) -> Self {
180 self.entry_wasm = entry_wasm;
181 self
182 }
183
184 pub fn with_icon(mut self, icon: IconRef) -> Self {
186 self.icons.push(icon);
187 self
188 }
189
190 pub fn with_description(mut self, description: impl Into<String>) -> Self {
192 self.description = description.into();
193 self
194 }
195
196 pub fn with_version(mut self, version: impl Into<String>) -> Self {
198 self.version = version.into();
199 self
200 }
201
202 pub fn validate(&self) -> Result<(), Vec<String>> {
210 let mut problems = Vec::new();
211
212 if self.id.trim().is_empty() {
213 problems.push("id must not be empty".to_string());
214 } else if !self.id.contains('.') {
215 problems.push(format!(
216 "id \"{}\" must be reverse-DNS-ish (contain at least one '.')",
217 self.id
218 ));
219 }
220
221 if self.name.trim().is_empty() {
222 problems.push("name must not be empty".to_string());
223 }
224
225 if self.version.trim().is_empty() {
226 problems.push("version must not be empty".to_string());
227 }
228
229 if self.icons.is_empty() {
230 problems
231 .push("at least one icon is required for a usable home-screen install".to_string());
232 }
233
234 if problems.is_empty() {
235 Ok(())
236 } else {
237 Err(problems)
238 }
239 }
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245
246 #[test]
247 fn new_has_sane_defaults() {
248 let m = QappManifest::new("coop.qualia.journal", "Qualia Journal");
249 assert_eq!(m.id, "coop.qualia.journal");
250 assert_eq!(m.name, "Qualia Journal");
251 assert_eq!(m.short_name, "Qualia Journal");
252 assert_eq!(m.version, "0.1.0");
253 assert_eq!(m.display, "standalone");
254 assert_eq!(m.theme_color, "#101418");
255 assert_eq!(m.background_color, "#0b0d10");
256 assert!(m.offline);
257 assert!(
258 m.capabilities.is_empty(),
259 "least privilege = empty by default"
260 );
261 assert_eq!(m.kind, QappKind::Cooperative);
262 }
263
264 #[test]
265 fn builders_compose() {
266 let m = QappManifest::new("coop.qualia.journal", "Qualia Journal")
267 .with_kind(QappKind::Journal)
268 .with_version("1.2.3")
269 .with_description("A private journal")
270 .with_capability(Capability::ReadRecords)
271 .with_capability(Capability::WriteRecords)
272 .with_entry(WasmRef {
273 path: "./journal.wasm".to_string(),
274 sha256_hex: "deadbeef".to_string(),
275 size_bytes: 4096,
276 })
277 .with_icon(IconRef {
278 src: "./icons/icon-192.png".to_string(),
279 sizes: "192x192".to_string(),
280 purpose: "any".to_string(),
281 });
282 assert_eq!(m.kind, QappKind::Journal);
283 assert_eq!(m.version, "1.2.3");
284 assert_eq!(m.description, "A private journal");
285 assert_eq!(m.capabilities.len(), 2);
286 assert_eq!(m.entry_wasm.path, "./journal.wasm");
287 assert_eq!(m.icons.len(), 1);
288 }
289
290 #[test]
291 fn manifest_serde_round_trips() {
292 let m = QappManifest::new("coop.qualia.directory", "Directory")
293 .with_kind(QappKind::Directory)
294 .with_capability(Capability::Sync)
295 .with_icon(IconRef {
296 src: "./i.png".to_string(),
297 sizes: "any".to_string(),
298 purpose: "maskable".to_string(),
299 });
300 let json = serde_json::to_string(&m).expect("serialize");
301 let back: QappManifest = serde_json::from_str(&json).expect("deserialize");
302 assert_eq!(m, back);
303 }
304
305 #[test]
306 fn well_known_kind_serializes_snake_case() {
307 let json = serde_json::to_string(&QappKind::Cooperative).unwrap();
308 assert_eq!(json, "\"cooperative\"");
309 let json = serde_json::to_string(&QappKind::Health).unwrap();
310 assert_eq!(json, "\"health\"");
311 }
312
313 #[test]
314 fn custom_kind_serializes_to_its_string() {
315 let json =
316 serde_json::to_string(&QappKind::Custom("coop.custom.kind".to_string())).unwrap();
317 assert_eq!(json, "{\"custom\":\"coop.custom.kind\"}");
318 assert_eq!(QappKind::Custom("x".to_string()).as_str(), "x");
319 }
320
321 #[test]
322 fn custom_capability_serializes_to_its_string() {
323 let json = serde_json::to_string(&Capability::Custom("geolocation".to_string())).unwrap();
324 assert_eq!(json, "{\"custom\":\"geolocation\"}");
325 assert_eq!(
326 Capability::Custom("geolocation".to_string()).as_str(),
327 "geolocation"
328 );
329 assert_eq!(
331 serde_json::to_string(&Capability::ReadRecords).unwrap(),
332 "\"read_records\""
333 );
334 }
335
336 #[test]
337 fn validate_ok_for_complete_manifest() {
338 let m = QappManifest::new("coop.qualia.journal", "Journal").with_icon(IconRef {
339 src: "./i.png".to_string(),
340 sizes: "192x192".to_string(),
341 purpose: "any".to_string(),
342 });
343 assert_eq!(m.validate(), Ok(()));
344 }
345
346 #[test]
347 fn validate_catches_empty_id() {
348 let m = QappManifest::new("", "Journal").with_icon(IconRef {
349 src: "./i.png".to_string(),
350 sizes: "any".to_string(),
351 purpose: "any".to_string(),
352 });
353 let problems = m.validate().unwrap_err();
354 assert!(problems.iter().any(|p| p.contains("id must not be empty")));
355 }
356
357 #[test]
358 fn validate_catches_missing_dot() {
359 let m = QappManifest::new("journal", "Journal").with_icon(IconRef {
360 src: "./i.png".to_string(),
361 sizes: "any".to_string(),
362 purpose: "any".to_string(),
363 });
364 let problems = m.validate().unwrap_err();
365 assert!(problems.iter().any(|p| p.contains("reverse-DNS-ish")));
366 }
367
368 #[test]
369 fn validate_catches_empty_name() {
370 let m = QappManifest::new("coop.qualia.journal", "").with_icon(IconRef {
371 src: "./i.png".to_string(),
372 sizes: "any".to_string(),
373 purpose: "any".to_string(),
374 });
375 let problems = m.validate().unwrap_err();
376 assert!(problems
377 .iter()
378 .any(|p| p.contains("name must not be empty")));
379 }
380
381 #[test]
382 fn validate_reports_missing_icon() {
383 let m = QappManifest::new("coop.qualia.journal", "Journal");
384 let problems = m.validate().unwrap_err();
385 assert!(problems.iter().any(|p| p.contains("icon")));
386 }
387
388 #[test]
389 fn validate_accumulates_all_problems() {
390 let mut m = QappManifest::new("", "");
392 m.version = String::new();
393 let problems = m.validate().unwrap_err();
394 assert_eq!(problems.len(), 4, "problems: {problems:?}");
395 }
396}