Skip to main content

aster/build/
mod.rs

1//! Build orchestration and output publication.
2//!
3//! This layer combines the foundation and engine layers behind [`BuildSession`].
4
5use std::fmt;
6
7use comemo::{Track, Tracked};
8use typst::ecow::EcoString;
9use typst_kit::datetime::Time;
10use typst_kit::fonts::FontStore;
11
12use crate::foundation::{FilesystemDependency, FontConfig, Project};
13
14mod content;
15mod downloader;
16mod files;
17mod output;
18mod pipeline;
19mod postprocess;
20mod transform;
21mod world;
22
23use self::files::ProjectFiles;
24
25pub use pipeline::BuildOutcome;
26
27/// A reusable build session bound to one Aster project.
28///
29/// A session retains compiler resources and observed filesystem state across
30/// builds while reloading the project manifest for every build attempt.
31pub struct BuildSession {
32    project: Project,
33    font_config: Option<FontConfig>,
34    fonts: FontStore,
35    files: ProjectFiles,
36    now: Time,
37    dev: bool,
38}
39
40impl BuildSession {
41    /// Create a reusable session bound to a validated project.
42    ///
43    /// `dev` is exposed to Typst code as `sys.inputs._aster.dev`.
44    pub fn new(project: Project, dev: bool) -> Self {
45        let files = ProjectFiles::new(&project);
46        Self {
47            project,
48            font_config: None,
49            fonts: FontStore::new(),
50            files,
51            now: Time::system(),
52            dev,
53        }
54    }
55
56    /// Return the project bound to this session.
57    pub fn project(&self) -> &Project {
58        &self.project
59    }
60
61    /// Return the inputs observed by the latest build attempt.
62    pub fn dependencies(&mut self) -> Vec<FilesystemDependency> {
63        self.files.dependencies()
64    }
65
66    fn reset(&mut self) {
67        self.files.reset();
68        self.now.reset();
69    }
70
71    fn project_files(&self) -> Tracked<'_, ProjectFiles> {
72        self.files.track()
73    }
74}
75
76/// A non-fatal diagnostic produced while building a project.
77#[derive(Clone, Debug, Eq, Hash, PartialEq)]
78pub struct BuildWarning(EcoString);
79
80impl BuildWarning {
81    fn new(message: impl Into<EcoString>) -> Self {
82        Self(message.into())
83    }
84
85    /// Return the warning message without presentation-specific formatting.
86    pub fn as_str(&self) -> &str {
87        self.0.as_str()
88    }
89}
90
91impl fmt::Display for BuildWarning {
92    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
93        formatter.write_str(&self.0)
94    }
95}
96
97impl AsRef<str> for BuildWarning {
98    fn as_ref(&self) -> &str {
99        self.as_str()
100    }
101}