devpage_core/widgets/request.rs
1//! A request for a widget's module, answered: the module its address names,
2//! compiled from one reading of its files, with whether it was asked for at
3//! its current address — or why it can't be had.
4//!
5//! An address names a module by its path in the served folder and its
6//! version (`?v=`). The version asked about and the bytes sent come from one
7//! [`Snapshot`](super::Snapshot), so a save between the two can't be served
8//! under the version before it.
9
10use super::modules::IMPORT_EXTENSIONS;
11use super::{Modules, Widgets, system_hash};
12use crate::folder::Folder;
13
14/// A widget module, compiled, as a request for it is answered.
15#[derive(Debug, PartialEq)]
16pub struct Compiled {
17 /// The module compiled to the JavaScript the browser imports.
18 pub js: String,
19 /// Asked for at its current address: what answers it never changes.
20 pub current: bool,
21}
22
23/// The system module in `file` — a system widget's, `Tabs.tsx`, or one
24/// they share, `live.ts` — compiled, asked for at `version`; `Err` with why
25/// when it doesn't compile, and `None` for a file that isn't a system
26/// module.
27pub fn system_module(
28 modules: &Modules,
29 file: &str,
30 version: Option<&str>,
31) -> Option<Result<Compiled, String>> {
32 let compiled = modules.system(file)?;
33 let current = version.is_some() && version == system_hash(file);
34 Some(
35 compiled
36 .map(|js| Compiled { js, current })
37 .map_err(|error| format!("{file} does not compile: {error}")),
38 )
39}
40
41/// The project widget or helper at `path` in `folder`'s served folder,
42/// compiled, asked for at `version`. An import without an extension finds
43/// the `.tsx` or `.ts` beside it. It renders by name the widgets of the
44/// document whose widgets folder holds it — the entry document's when no
45/// other names it. `Err` with why when it, or any helper its imports reach,
46/// doesn't compile — the browser would only say an import failed; `None`
47/// when no such file is in the served folder.
48pub fn project_module(
49 folder: &Folder,
50 modules: &Modules,
51 path: &str,
52 version: Option<&str>,
53) -> Option<Result<Compiled, String>> {
54 let served = folder.served();
55 let file = IMPORT_EXTENSIONS
56 .iter()
57 .find_map(|ext| served.file_at(&format!("{path}{ext}")))?;
58 let name = served.name(&file)?;
59 let owner = file
60 .parent()
61 .and_then(|widgets| folder.widgets_owner(widgets))
62 .unwrap_or_else(|| folder.entry());
63 let known = Widgets::of(&owner, modules).names().to_vec();
64 let snapshot = modules.snapshot(served, &file);
65 let current = version == Some(snapshot.hash(&file, &known).as_str());
66 // The closure reaches only helpers inside the served folder, so each
67 // has a name.
68 let broken = |file: &std::path::Path| {
69 let error = snapshot.compile(file, &known).err()?;
70 Some(format!("{} does not compile: {error}", served.name(file)?))
71 };
72 let helper = snapshot
73 .closure(&file)
74 .into_iter()
75 .skip(1)
76 .find_map(|(helper, _)| broken(&helper));
77 if let Some(why) = helper {
78 return Some(Err(why));
79 }
80 Some(
81 snapshot
82 .compile(&file, &known)
83 .map(|js| Compiled { js, current })
84 .map_err(|error| format!("{name} does not compile: {error}")),
85 )
86}