tauri_plugin_compmd/frames.rs
1//! The build of the frames the app serves (docs/tauri-plugin.md, decisions 2 and 3): the one the crate embeds, or the
2//! app's own, the folder its `frames.json` names, read once when the app starts.
3
4use std::fs;
5use std::path::Path;
6
7use serde::Deserialize;
8
9use crate::embedded;
10use crate::files::Files;
11
12/// The file beside the builds that names the current one, as `buildHashedFrames` writes it.
13pub const FRAMES_JSON: &str = "frames.json";
14
15/// The prefix of every path the frames are served under.
16pub const PREFIX: &str = "/frames/";
17
18/// What `frames.json` holds.
19#[derive(Deserialize)]
20struct Named {
21 hash: String,
22}
23
24/// The build the app serves, or none when there is none.
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub struct Frames(Option<Build>);
27
28/// A build of the frames: its hash, which its paths name, and its files.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct Build {
31 /// The hash naming it, 16 lowercase hex digits.
32 pub hash: String,
33 /// Its frame documents and their files.
34 pub files: Files,
35}
36
37impl Frames {
38 /// The build the crate embeds; none when it was built without one (`cargo make tauri-frames` writes it).
39 #[must_use]
40 pub fn embedded() -> Self {
41 Self::embedded_as(embedded::HASH, embedded::FILES)
42 }
43
44 /// The build embedded as `hash` and `files`, if there is one.
45 fn embedded_as(hash: Option<&str>, files: &'static [(&'static str, &'static [u8])]) -> Self {
46 Self(hash.map(|hash| Build {
47 hash: hash.to_owned(),
48 files: Files::Embedded(files),
49 }))
50 }
51
52 /// The build `frames.json` in `folder` names, an app's own; none when it is missing, can't be read, or names
53 /// something that isn't a build's hash.
54 #[must_use]
55 pub fn read(folder: &Path) -> Self {
56 let named = fs::read_to_string(folder.join(FRAMES_JSON))
57 .ok()
58 .and_then(|text| serde_json::from_str::<Named>(&text).ok());
59 Self(
60 named
61 .filter(|named| is_hash(&named.hash))
62 .map(|named| Build {
63 files: Files::Folder(folder.join(&named.hash)),
64 hash: named.hash,
65 }),
66 )
67 }
68
69 /// No build: the app serves nothing and gives no `base`.
70 #[must_use]
71 pub const fn none() -> Self {
72 Self(None)
73 }
74
75 /// The build, if there is one.
76 #[must_use]
77 pub const fn build(&self) -> Option<&Build> {
78 self.0.as_ref()
79 }
80
81 /// The frames' `base` on `scheme`, for `@compmd/sandbox`: `<scheme>://localhost/frames/<hash>/`, or the
82 /// `http://<scheme>.localhost/frames/<hash>/` form where Tauri serves custom schemes over http (Windows and
83 /// Android); none without a build.
84 #[must_use]
85 pub fn base(&self, scheme: &str) -> Option<String> {
86 self.base_over(scheme, OVER_HTTP)
87 }
88
89 /// `base`, where custom schemes are served over http (`over_http`) or not.
90 fn base_over(&self, scheme: &str, over_http: bool) -> Option<String> {
91 let build = self.build()?;
92 let origin = if over_http {
93 format!("http://{scheme}.localhost")
94 } else {
95 format!("{scheme}://localhost")
96 };
97 Some(format!("{origin}{PREFIX}{}/", build.hash))
98 }
99}
100
101/// Whether Tauri serves custom schemes over http on this platform: on Windows and Android, as
102/// `http://<scheme>.localhost`.
103const OVER_HTTP: bool = cfg!(any(windows, target_os = "android"));
104
105/// Whether `name` is a build's hash: 16 lowercase hex digits, so it can't name another folder.
106fn is_hash(name: &str) -> bool {
107 name.len() == 16
108 && name
109 .bytes()
110 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
111}
112
113#[cfg(test)]
114pub(crate) mod tests;