Skip to main content

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    pub js: String,
18    /// Asked for at its current address: what answers it never changes.
19    pub current: bool,
20}
21
22/// The system widget `name` — `Tabs.tsx` — compiled, asked for at
23/// `version`; `Err` with why when it doesn't compile, and `None` for a name
24/// that isn't a system widget.
25pub fn system_module(
26    modules: &Modules,
27    name: &str,
28    version: Option<&str>,
29) -> Option<Result<Compiled, String>> {
30    let stem = name.strip_suffix(".tsx").unwrap_or(name);
31    let compiled = modules.system(stem)?;
32    let current = version.is_some() && version == system_hash(stem);
33    Some(
34        compiled
35            .map(|js| Compiled { js, current })
36            .map_err(|error| format!("{name} does not compile: {error}")),
37    )
38}
39
40/// The project widget or helper at `path` in `folder`'s served folder,
41/// compiled, asked for at `version`. An import without an extension finds
42/// the `.tsx` or `.ts` beside it. It renders by name the widgets of the
43/// document whose widgets folder holds it — the entry document's when no
44/// other names it. `Err` with why when it, or any helper its imports reach,
45/// doesn't compile — the browser would only say an import failed; `None`
46/// when no such file is in the served folder.
47pub fn project_module(
48    folder: &Folder,
49    modules: &Modules,
50    path: &str,
51    version: Option<&str>,
52) -> Option<Result<Compiled, String>> {
53    let served = folder.served();
54    let file = IMPORT_EXTENSIONS
55        .iter()
56        .find_map(|ext| served.file_at(&format!("{path}{ext}")))?;
57    let name = served.name(&file)?;
58    let owner = file
59        .parent()
60        .and_then(|widgets| folder.widgets_owner(widgets))
61        .unwrap_or_else(|| folder.entry());
62    let known = Widgets::of(&owner, modules).names().to_vec();
63    let snapshot = modules.snapshot(served, &file);
64    let current = version == Some(snapshot.hash(&file, &known).as_str());
65    // The closure reaches only helpers inside the served folder, so each
66    // has a name.
67    let broken = |file: &std::path::Path| {
68        let error = snapshot.compile(file, &known).err()?;
69        Some(format!("{} does not compile: {error}", served.name(file)?))
70    };
71    let helper = snapshot
72        .closure(&file)
73        .into_iter()
74        .skip(1)
75        .find_map(|(helper, _)| broken(&helper));
76    if let Some(why) = helper {
77        return Some(Err(why));
78    }
79    Some(
80        snapshot
81            .compile(&file, &known)
82            .map(|js| Compiled { js, current })
83            .map_err(|error| format!("{name} does not compile: {error}")),
84    )
85}