compmd_host/manifest.rs
1//! `manifest.json`, the bundle's one description ([hosting], decision 3), which `@compmd/vite` writes and every host
2//! reads, in Rust here and in JavaScript.
3//!
4//! [hosting]: https://github.com/comp-markdown/compmd/blob/main/docs/hosting.md
5
6use std::collections::BTreeMap;
7
8use compmd_pack::{PackName, ReleaseId};
9use serde::{Deserialize, Serialize};
10
11use crate::bundled_pack::BundledPack;
12use crate::fetched_by::FetchedBy;
13use crate::frames::Frames;
14use crate::library::Library;
15use crate::manifest_error::ManifestError;
16
17/// The manifest's format this crate reads; a bundle of another is refused, with what to upgrade.
18pub const FORMAT: u32 = 1;
19
20/// A bundle's manifest.
21#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
22pub struct Manifest {
23 /// The format it is written in, [`FORMAT`].
24 pub format: u32,
25 /// The frames' build.
26 pub frames: Frames,
27 /// Where the frames' policies say they are served, when that is a custom scheme (`compmd://localhost/`), whose
28 /// `'self'` `WebKit` reads as nothing in a sandboxed frame; none for http and https origins.
29 #[serde(default, skip_serializing_if = "Option::is_none")]
30 pub source: Option<String>,
31 /// The packs the bundle holds, under `packs/<name>@<version>/`.
32 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 pub packs: Vec<BundledPack>,
34 /// The libraries the frames offer component code, besides the runtimes.
35 #[serde(default, skip_serializing_if = "Vec::is_empty")]
36 pub libraries: Vec<Library>,
37 /// The processor's policy the configuration made (`policyOf`), as JSON: what a host that processes in Rust gives
38 /// the processor.
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub policy: Option<serde_json::Value>,
41 /// The packs every document gets without importing them.
42 #[serde(default, skip_serializing_if = "Vec::is_empty")]
43 pub ambient: Vec<PackName>,
44 /// The registries packs come from, by scope (`@kit`, `*` for every other), each a URL ending in `/`; none when
45 /// the configuration names none.
46 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
47 pub registries: BTreeMap<String, String>,
48 /// Who fetches from them: with [`FetchedBy::Host`], the host serves `registry/` as their proxy.
49 #[serde(default)]
50 pub fetch: FetchedBy,
51 /// In bytes: the largest pack the host takes from a registry; the registry rules' default when absent.
52 #[serde(
53 default,
54 rename = "maxPackSize",
55 skip_serializing_if = "Option::is_none"
56 )]
57 pub max_pack_size: Option<u64>,
58}
59
60impl Manifest {
61 /// The manifest in `text`.
62 ///
63 /// # Errors
64 ///
65 /// [`ManifestError`] when it isn't a manifest, is of another format, or names a frames' hash, a pack, a path or
66 /// an integrity that isn't one.
67 pub fn parse(text: &str) -> Result<Self, ManifestError> {
68 #[derive(Deserialize)]
69 struct Format {
70 format: u32,
71 }
72 let json = |error: serde_json::Error| ManifestError::Json(error.to_string());
73 let format: Format = serde_json::from_str(text).map_err(json)?;
74 if format.format != FORMAT {
75 return Err(ManifestError::UnknownFormat {
76 found: format.format,
77 known: FORMAT,
78 });
79 }
80 serde_json::from_str(text).map_err(json)
81 }
82
83 /// The custom scheme the bundle was built for, from its `source` (`compmd://localhost/` is `compmd`); none when
84 /// it was built for an http or https origin.
85 #[must_use]
86 pub fn scheme(&self) -> Option<&str> {
87 let scheme = self.source.as_deref()?.split_once("://")?.0;
88 (scheme != "http" && scheme != "https").then_some(scheme)
89 }
90
91 /// The pack at `release`, when the bundle holds it.
92 #[must_use]
93 pub fn pack(&self, release: &ReleaseId) -> Option<&BundledPack> {
94 self.packs.iter().find(|pack| pack.release == *release)
95 }
96}
97
98#[cfg(test)]
99mod tests;