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 compmd_pack::{PackName, ReleaseId};
7use serde::{Deserialize, Serialize};
8
9use crate::bundled_pack::BundledPack;
10use crate::frames::Frames;
11use crate::library::Library;
12use crate::manifest_error::ManifestError;
13
14/// The manifest's format this crate reads; a bundle of another is refused, with what to upgrade.
15pub const FORMAT: u32 = 1;
16
17/// A bundle's manifest.
18#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
19pub struct Manifest {
20 /// The format it is written in, [`FORMAT`].
21 pub format: u32,
22 /// The frames' build.
23 pub frames: Frames,
24 /// Where the frames' policies say they are served, when that is a custom scheme (`compmd://localhost/`), whose
25 /// `'self'` `WebKit` reads as nothing in a sandboxed frame; none for http and https origins.
26 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub source: Option<String>,
28 /// The packs the bundle holds, under `packs/<name>@<version>/`.
29 #[serde(default, skip_serializing_if = "Vec::is_empty")]
30 pub packs: Vec<BundledPack>,
31 /// The libraries the frames offer component code, besides the runtimes.
32 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 pub libraries: Vec<Library>,
34 /// The processor's policy the configuration made (`policyOf`), as JSON: what a host that processes in Rust gives
35 /// the processor.
36 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub policy: Option<serde_json::Value>,
38 /// The packs every document gets without importing them.
39 #[serde(default, skip_serializing_if = "Vec::is_empty")]
40 pub ambient: Vec<PackName>,
41}
42
43impl Manifest {
44 /// The manifest in `text`.
45 ///
46 /// # Errors
47 ///
48 /// [`ManifestError`] when it isn't a manifest, is of another format, or names a frames' hash, a pack, a path or
49 /// an integrity that isn't one.
50 pub fn parse(text: &str) -> Result<Self, ManifestError> {
51 #[derive(Deserialize)]
52 struct Format {
53 format: u32,
54 }
55 let json = |error: serde_json::Error| ManifestError::Json(error.to_string());
56 let format: Format = serde_json::from_str(text).map_err(json)?;
57 if format.format != FORMAT {
58 return Err(ManifestError::UnknownFormat {
59 found: format.format,
60 known: FORMAT,
61 });
62 }
63 serde_json::from_str(text).map_err(json)
64 }
65
66 /// The custom scheme the bundle was built for, from its `source` (`compmd://localhost/` is `compmd`); none when
67 /// it was built for an http or https origin.
68 #[must_use]
69 pub fn scheme(&self) -> Option<&str> {
70 let scheme = self.source.as_deref()?.split_once("://")?.0;
71 (scheme != "http" && scheme != "https").then_some(scheme)
72 }
73
74 /// The pack at `release`, when the bundle holds it.
75 #[must_use]
76 pub fn pack(&self, release: &ReleaseId) -> Option<&BundledPack> {
77 self.packs.iter().find(|pack| pack.release == *release)
78 }
79}
80
81#[cfg(test)]
82mod tests;