aster/foundation/
project.rs1use std::path::{Path, PathBuf};
2
3use anyhow::{Context, Result, bail, ensure};
4use typst::syntax::VirtualPath;
5
6use crate::foundation::config::AsterConfig;
7
8#[derive(Clone)]
10pub struct Project {
11 root: PathBuf,
12}
13
14impl Project {
15 pub fn find(dir: &Path) -> Option<Self> {
17 let dir = std::path::absolute(dir).ok()?;
18 let mut current = Some(dir.as_path());
19 while let Some(path) = current {
20 if path.join("aster.toml").is_file() {
21 return Some(Self {
22 root: path.to_owned(),
23 });
24 }
25 current = path.parent();
26 }
27 None
28 }
29
30 pub fn open(root: impl Into<PathBuf>) -> Result<Self> {
32 let root =
33 std::path::absolute(root.into()).context("failed to make project root absolute")?;
34 if !root.join("aster.toml").is_file() {
35 bail!("no aster.toml found in {}", root.display());
36 }
37 Ok(Self { root })
38 }
39
40 pub fn root(&self) -> &Path {
42 &self.root
43 }
44
45 pub fn config_file(&self) -> PathBuf {
47 self.root.join("aster.toml")
48 }
49
50 pub fn config_path(&self) -> VirtualPath {
52 VirtualPath::new("aster.toml").expect("the manifest path is a valid virtual path")
53 }
54
55 pub fn realize(&self, path: &VirtualPath) -> PathBuf {
57 path.realize(&self.root)
58 .expect("validated project path must realize within the project root")
59 }
60}
61
62#[derive(Clone)]
64pub struct ProjectLayout {
65 pages: VirtualPath,
66 generate: VirtualPath,
67 content: VirtualPath,
68 public: VirtualPath,
69 output: VirtualPath,
70 generated_assets: VirtualPath,
71 fonts: Vec<VirtualPath>,
72 watch_paths: Vec<VirtualPath>,
73}
74
75impl ProjectLayout {
76 pub fn new(config: &AsterConfig) -> Result<Self> {
78 let pages = project_directory(&config.paths.pages, "pages")?;
79 let generate = project_directory(&config.paths.generate, "generate")?;
80 let content = project_directory(&config.paths.content, "content")?;
81 let public = project_directory(&config.paths.public, "public")?;
82 let output = project_directory(&config.paths.output, "output")?;
83 ensure_disjoint(&pages, "pages", &content, "content")?;
84 ensure_disjoint(&pages, "pages", &public, "public")?;
85 ensure_disjoint(&pages, "pages", &output, "output")?;
86 ensure_disjoint(&generate, "generate", &pages, "pages")?;
87 ensure_disjoint(&generate, "generate", &content, "content")?;
88 ensure_disjoint(&generate, "generate", &public, "public")?;
89 ensure_disjoint(&generate, "generate", &output, "output")?;
90 ensure_disjoint(&content, "content", &public, "public")?;
91 ensure_disjoint(&content, "content", &output, "output")?;
92 ensure_disjoint(&public, "public", &output, "output")?;
93
94 let generated_assets = project_directory(&config.output.assets, "generated assets")?;
95 let fonts = config
96 .typst
97 .fonts
98 .paths
99 .iter()
100 .map(|path| project_path(path, "font"))
101 .collect::<Result<_>>()?;
102 for font in &fonts {
103 ensure_disjoint(font, "font", &output, "output")?;
104 }
105 let watch_paths = config
106 .watch
107 .paths
108 .iter()
109 .chain(
110 config
111 .postprocess
112 .iter()
113 .flat_map(|processor| processor.watch.iter()),
114 )
115 .map(|value| {
116 let path = project_path(value, "watch")?;
117 ensure!(!path.is_root(), "watch path cannot be the project root");
118 ensure!(
119 !overlaps(&path, &output),
120 "watch path `{}` must not overlap the output directory",
121 path.get_without_slash()
122 );
123 Ok(path)
124 })
125 .collect::<Result<_>>()?;
126
127 Ok(Self {
128 pages,
129 generate,
130 content,
131 public,
132 output,
133 generated_assets,
134 fonts,
135 watch_paths,
136 })
137 }
138
139 pub fn pages(&self) -> &VirtualPath {
141 &self.pages
142 }
143
144 pub fn generate(&self) -> &VirtualPath {
146 &self.generate
147 }
148
149 pub fn content(&self) -> &VirtualPath {
151 &self.content
152 }
153
154 pub fn public(&self) -> &VirtualPath {
156 &self.public
157 }
158
159 pub fn generated_assets(&self) -> &VirtualPath {
161 &self.generated_assets
162 }
163
164 pub fn output(&self) -> &VirtualPath {
166 &self.output
167 }
168
169 pub fn font_dirs(&self) -> impl Iterator<Item = &VirtualPath> {
171 self.fonts.iter()
172 }
173
174 pub fn watch_paths(&self) -> impl Iterator<Item = &VirtualPath> {
176 self.watch_paths.iter()
177 }
178}
179
180fn project_directory(value: &str, name: &str) -> Result<VirtualPath> {
181 let path = project_path(value, name)?;
182 ensure!(
183 !path.is_root(),
184 "{name} directory cannot be the project root"
185 );
186 Ok(path)
187}
188
189fn project_path(value: &str, name: &str) -> Result<VirtualPath> {
190 ensure!(
191 !Path::new(value).is_absolute(),
192 "{name} path must be relative"
193 );
194 VirtualPath::new(value).with_context(|| format!("invalid {name} path `{value}`"))
195}
196
197fn ensure_disjoint(
198 left: &VirtualPath,
199 left_name: &str,
200 right: &VirtualPath,
201 right_name: &str,
202) -> Result<()> {
203 ensure!(
204 !overlaps(left, right),
205 "{left_name} and {right_name} directories must not overlap"
206 );
207 Ok(())
208}
209
210fn overlaps(left: &VirtualPath, right: &VirtualPath) -> bool {
211 let left = Path::new(left.get_without_slash());
212 let right = Path::new(right.get_without_slash());
213 left.starts_with(right) || right.starts_with(left)
214}