tinymist_task/
primitives.rs

1pub use tinymist_world::args::{ExportTarget, OutputFormat, PdfStandard, TaskWhen};
2
3use core::fmt;
4use std::hash::{Hash, Hasher};
5use std::num::NonZeroUsize;
6use std::ops::RangeInclusive;
7use std::path::PathBuf;
8use std::{path::Path, str::FromStr};
9
10use serde::{Deserialize, Serialize};
11use tinymist_std::ImmutPath;
12use tinymist_std::error::prelude::*;
13use tinymist_std::path::{PathClean, unix_slash};
14use tinymist_world::vfs::WorkspaceResolver;
15use tinymist_world::{CompilerFeat, CompilerWorld, EntryReader, EntryState};
16use typst::diag::EcoString;
17use typst::layout::PageRanges;
18use typst::syntax::FileId;
19
20/// A scalar that is not NaN.
21#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
22pub struct Scalar(f32);
23
24impl TryFrom<f32> for Scalar {
25    type Error = &'static str;
26
27    fn try_from(value: f32) -> Result<Self, Self::Error> {
28        if value.is_nan() {
29            Err("NaN is not a valid scalar value")
30        } else {
31            Ok(Scalar(value))
32        }
33    }
34}
35
36impl Scalar {
37    /// Converts the scalar to an f32.
38    pub fn to_f32(self) -> f32 {
39        self.0
40    }
41}
42
43impl PartialEq for Scalar {
44    fn eq(&self, other: &Self) -> bool {
45        self.0 == other.0
46    }
47}
48
49impl Eq for Scalar {}
50
51impl Hash for Scalar {
52    fn hash<H: Hasher>(&self, state: &mut H) {
53        self.0.to_bits().hash(state);
54    }
55}
56
57impl PartialOrd for Scalar {
58    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
59        Some(self.cmp(other))
60    }
61}
62
63impl Ord for Scalar {
64    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
65        self.0.partial_cmp(&other.0).unwrap()
66    }
67}
68
69/// A project ID.
70#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
71#[serde(rename_all = "kebab-case")]
72pub struct Id(String);
73
74impl Id {
75    /// Creates a new project Id.
76    pub fn new(s: String) -> Self {
77        Id(s)
78    }
79
80    /// Creates a new project Id from a world.
81    pub fn from_world<F: CompilerFeat>(world: &CompilerWorld<F>, ctx: CtxPath) -> Option<Self> {
82        let entry = world.entry_state();
83        let id = unix_slash(entry.main()?.vpath().as_rootless_path());
84
85        // todo: entry root may not be set, so we should use the cwd
86        let path = &ResourcePath::from_user_sys(Path::new(&id), ctx);
87        Some(path.into())
88    }
89}
90
91impl From<&ResourcePath> for Id {
92    fn from(value: &ResourcePath) -> Self {
93        Id::new(value.to_string())
94    }
95}
96
97impl fmt::Display for Id {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        f.write_str(&self.0)
100    }
101}
102
103/// The path pattern that could be substituted.
104///
105/// # Examples
106/// - `$root` is the root of the project.
107/// - `$root/$dir` is the parent directory of the input (main) file.
108/// - `$root/main` will help store pdf file to `$root/main.pdf` constantly.
109/// - (default) `$root/$dir/$name` will help store pdf file along with the input
110///   file.
111#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
112pub struct PathPattern(pub EcoString);
113
114impl fmt::Display for PathPattern {
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        f.write_str(&self.0)
117    }
118}
119
120impl PathPattern {
121    /// Creates a new path pattern.
122    pub fn new(pattern: &str) -> Self {
123        Self(pattern.into())
124    }
125
126    /// Creates a new path pattern from a string.
127    pub fn relative_to(self, base: &Path) -> Self {
128        if self.0.is_empty() {
129            return self;
130        }
131
132        let path = Path::new(self.0.as_str());
133        if path.is_absolute() {
134            let rel_path = tinymist_std::path::diff(path, base);
135
136            match rel_path {
137                Some(rel) => PathPattern(unix_slash(&rel).into()),
138                None => self,
139            }
140        } else {
141            self
142        }
143    }
144
145    /// Substitutes the path pattern with `$root`, and `$dir/$name`.
146    pub fn substitute(&self, entry: &EntryState) -> Option<ImmutPath> {
147        self.substitute_impl(entry.root(), entry.main())
148    }
149
150    #[comemo::memoize]
151    fn substitute_impl(&self, root: Option<ImmutPath>, main: Option<FileId>) -> Option<ImmutPath> {
152        log::debug!("Check path {main:?} and root {root:?} with output directory {self:?}");
153
154        let (root, main) = root.zip(main)?;
155
156        // Files in packages are not exported
157        if WorkspaceResolver::is_package_file(main) {
158            return None;
159        }
160        // Files without a path are not exported
161        let path = main.vpath().resolve(&root)?;
162
163        // todo: handle untitled path
164        if let Ok(path) = path.strip_prefix("/untitled") {
165            let tmp = std::env::temp_dir();
166            let path = tmp.join("typst").join(path);
167            return Some(path.as_path().into());
168        }
169
170        if self.0.is_empty() {
171            return Some(path.to_path_buf().clean().into());
172        }
173
174        let path = path.strip_prefix(&root).ok()?;
175        let dir = path.parent();
176        let file_name = path.file_name().unwrap_or_default();
177
178        let w = root.to_string_lossy();
179        let f = file_name.to_string_lossy();
180
181        // replace all $root
182        let mut path = self.0.replace("$root", &w);
183        if let Some(dir) = dir {
184            let d = dir.to_string_lossy();
185            path = path.replace("$dir", &d);
186        }
187        path = path.replace("$name", &f);
188
189        Some(Path::new(path.as_str()).clean().into())
190    }
191}
192
193/// Implements parsing of page ranges (`1-3`, `4`, `5-`, `-2`), used by the
194/// `CompileCommand.pages` argument, through the `FromStr` trait instead of a
195/// value parser, in order to generate better errors.
196///
197/// See also: <https://github.com/clap-rs/clap/issues/5065>
198#[derive(Debug, Clone, PartialEq, Eq, Hash)]
199pub struct Pages(pub RangeInclusive<Option<NonZeroUsize>>);
200
201impl Pages {
202    /// Selects the first page.
203    pub const FIRST: Pages = Pages(NonZeroUsize::new(1)..=NonZeroUsize::new(1));
204}
205
206impl FromStr for Pages {
207    type Err = &'static str;
208
209    fn from_str(value: &str) -> Result<Self, Self::Err> {
210        match value
211            .split('-')
212            .map(str::trim)
213            .collect::<Vec<_>>()
214            .as_slice()
215        {
216            [] | [""] => Err("page export range must not be empty"),
217            [single_page] => {
218                let page_number = parse_page_number(single_page)?;
219                Ok(Pages(Some(page_number)..=Some(page_number)))
220            }
221            ["", ""] => Err("page export range must have start or end"),
222            [start, ""] => Ok(Pages(Some(parse_page_number(start)?)..=None)),
223            ["", end] => Ok(Pages(None..=Some(parse_page_number(end)?))),
224            [start, end] => {
225                let start = parse_page_number(start)?;
226                let end = parse_page_number(end)?;
227                if start > end {
228                    Err("page export range must end at a page after the start")
229                } else {
230                    Ok(Pages(Some(start)..=Some(end)))
231                }
232            }
233            [_, _, _, ..] => Err("page export range must have a single hyphen"),
234        }
235    }
236}
237
238/// The ranges of the pages to be exported as specified by the user.
239pub fn exported_page_ranges(pages: &[Pages]) -> PageRanges {
240    PageRanges::new(pages.iter().map(|p| p.0.clone()).collect())
241}
242
243impl fmt::Display for Pages {
244    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245        let start = match self.0.start() {
246            Some(start) => start.to_string(),
247            None => String::from(""),
248        };
249        let end = match self.0.end() {
250            Some(end) => end.to_string(),
251            None => String::from(""),
252        };
253        write!(f, "{start}-{end}")
254    }
255}
256
257impl serde::Serialize for Pages {
258    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
259    where
260        S: serde::Serializer,
261    {
262        serializer.serialize_str(&self.to_string())
263    }
264}
265
266impl<'de> serde::Deserialize<'de> for Pages {
267    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
268    where
269        D: serde::Deserializer<'de>,
270    {
271        let value = String::deserialize(deserializer)?;
272        value.parse().map_err(serde::de::Error::custom)
273    }
274}
275
276/// Parses a single page number.
277fn parse_page_number(value: &str) -> Result<NonZeroUsize, &'static str> {
278    if value == "0" {
279        Err("page numbers start at one")
280    } else {
281        NonZeroUsize::from_str(value).map_err(|_| "not a valid page number")
282    }
283}
284
285/// A resource path.
286#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
287pub struct ResourcePath(EcoString, String);
288
289impl fmt::Display for ResourcePath {
290    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291        write!(f, "{}:{}", self.0, self.1)
292    }
293}
294
295impl FromStr for ResourcePath {
296    type Err = &'static str;
297
298    fn from_str(value: &str) -> Result<Self, Self::Err> {
299        let mut parts = value.split(':');
300        let scheme = parts.next().ok_or("missing scheme")?;
301        let path = parts.next().ok_or("missing path")?;
302        if parts.next().is_some() {
303            Err("too many colons")
304        } else {
305            Ok(ResourcePath(scheme.into(), path.to_string()))
306        }
307    }
308}
309
310impl serde::Serialize for ResourcePath {
311    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
312    where
313        S: serde::Serializer,
314    {
315        serializer.serialize_str(&self.to_string())
316    }
317}
318
319impl<'de> serde::Deserialize<'de> for ResourcePath {
320    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
321    where
322        D: serde::Deserializer<'de>,
323    {
324        let value = String::deserialize(deserializer)?;
325        value.parse().map_err(serde::de::Error::custom)
326    }
327}
328
329/// The path context.
330// todo: The ctx path looks not quite maintainable. But we only target to make
331// things correct, then back to make code good.
332pub type CtxPath<'a, 'b> = (/* cwd */ &'a Path, /* lock_dir */ &'b Path);
333
334impl ResourcePath {
335    /// Creates a new resource path from a user passing system path.
336    pub fn from_user_sys(inp: &Path, (cwd, lock_dir): CtxPath) -> Self {
337        let abs = if inp.is_absolute() {
338            inp.to_path_buf()
339        } else {
340            cwd.join(inp)
341        };
342        let resource_path = if let Some(rel) = tinymist_std::path::diff(&abs, lock_dir) {
343            rel
344        } else {
345            abs
346        };
347        // todo: clean is not posix compatible,
348        // for example /symlink/../file is not equivalent to /file
349        let rel = unix_slash(&resource_path.clean());
350        ResourcePath("file".into(), rel.to_string())
351    }
352
353    /// Creates a new resource path from a file id.
354    pub fn from_file_id(id: FileId) -> Self {
355        let package = id.package();
356        match package {
357            Some(package) => ResourcePath(
358                "file_id".into(),
359                format!("{package}{}", unix_slash(id.vpath().as_rooted_path())),
360            ),
361            None => ResourcePath(
362                "file_id".into(),
363                format!("$root{}", unix_slash(id.vpath().as_rooted_path())),
364            ),
365        }
366    }
367
368    /// Converts the resource path to a path relative to the `base` (usually the
369    /// directory storing the lockfile).
370    pub fn relative_to(&self, base: &Path) -> Option<Self> {
371        if self.0 == "file" {
372            let path = Path::new(&self.1);
373            if path.is_absolute() {
374                let rel_path = tinymist_std::path::diff(path, base)?;
375                Some(ResourcePath(self.0.clone(), unix_slash(&rel_path)))
376            } else {
377                Some(ResourcePath(self.0.clone(), self.1.clone()))
378            }
379        } else {
380            Some(self.clone())
381        }
382    }
383
384    /// Converts the resource path to a path relative to the `base` (usually the
385    /// directory storing the lockfile).
386    pub fn to_rel_path(&self, base: &Path) -> Option<PathBuf> {
387        if self.0 == "file" {
388            let path = Path::new(&self.1);
389            if path.is_absolute() {
390                Some(tinymist_std::path::diff(path, base).unwrap_or_else(|| path.to_owned()))
391            } else {
392                Some(path.to_owned())
393            }
394        } else {
395            None
396        }
397    }
398
399    /// Converts the resource path to an absolute file system path.
400    pub fn to_abs_path(&self, base: &Path) -> Option<PathBuf> {
401        if self.0 == "file" {
402            let path = Path::new(&self.1);
403            if path.is_absolute() {
404                Some(path.to_owned())
405            } else {
406                Some(base.join(path))
407            }
408        } else {
409            None
410        }
411    }
412}
413
414/// Utilities for output template processing.
415/// Copied from typst-cli.
416pub mod output_template {
417    const INDEXABLE: [&str; 3] = ["{p}", "{0p}", "{n}"];
418
419    /// Checks if the output template has indexable templates.
420    pub fn has_indexable_template(output: &str) -> bool {
421        INDEXABLE.iter().any(|template| output.contains(template))
422    }
423
424    /// Formats the output template with the given page number and total pages.
425    /// Note: `this_page` is 1-based.
426    pub fn format(output: &str, this_page: usize, total_pages: usize) -> String {
427        // Find the base 10 width of number `i`
428        fn width(i: usize) -> usize {
429            1 + i.checked_ilog10().unwrap_or(0) as usize
430        }
431
432        let other_templates = ["{t}"];
433        INDEXABLE
434            .iter()
435            .chain(other_templates.iter())
436            .fold(output.to_string(), |out, template| {
437                let replacement = match *template {
438                    "{p}" => format!("{this_page}"),
439                    "{0p}" | "{n}" => format!("{:01$}", this_page, width(total_pages)),
440                    "{t}" => format!("{total_pages}"),
441                    _ => unreachable!("unhandled template placeholder {template}"),
442                };
443                out.replace(template, replacement.as_str())
444            })
445    }
446}
447
448#[cfg(test)]
449mod tests {
450    use super::*;
451    use typst::syntax::VirtualPath;
452
453    #[test]
454    fn test_substitute_path() {
455        let root = Path::new("/dummy-root");
456        let entry =
457            EntryState::new_rooted(root.into(), Some(VirtualPath::new("/dir1/dir2/file.txt")));
458
459        assert_eq!(
460            PathPattern::new("/substitute/$dir/$name").substitute(&entry),
461            Some(PathBuf::from("/substitute/dir1/dir2/file.txt").into())
462        );
463        assert_eq!(
464            PathPattern::new("/substitute/$dir/../$name").substitute(&entry),
465            Some(PathBuf::from("/substitute/dir1/file.txt").into())
466        );
467        assert_eq!(
468            PathPattern::new("/substitute/$name").substitute(&entry),
469            Some(PathBuf::from("/substitute/file.txt").into())
470        );
471        assert_eq!(
472            PathPattern::new("/substitute/target/$dir/$name").substitute(&entry),
473            Some(PathBuf::from("/substitute/target/dir1/dir2/file.txt").into())
474        );
475    }
476}