tinymist_std/concepts/
typst.rs

1//! Re-export of the typst crate.
2pub(crate) mod well_known {
3    pub use typst::foundations::Bytes;
4
5    pub use typst::utils::LazyHash;
6
7    /// Although this is not good to expose this, we make an alias here to
8    /// let it as a part of typst-ts.
9    pub use typst::syntax::FileId as TypstFileId;
10
11    pub use typst::World as TypstWorld;
12
13    pub use typst::layout::Abs as TypstAbs;
14
15    pub use typst::Document as TypstDocumentTrait;
16
17    pub use typst::layout::PagedDocument as TypstPagedDocument;
18
19    pub use typst::html::HtmlDocument as TypstHtmlDocument;
20
21    pub use typst::text::Font as TypstFont;
22
23    pub use typst::foundations::Dict as TypstDict;
24
25    pub use typst::foundations::Datetime as TypstDatetime;
26
27    pub use typst::{diag, foundations, syntax};
28}
29
30/// The enum of all well-known Typst documents.
31#[derive(Debug, Clone)]
32pub enum TypstDocument {
33    /// The document compiled with `paged` target.
34    Paged(Arc<well_known::TypstPagedDocument>),
35    /// The document compiled with `html` target.
36    Html(Arc<well_known::TypstHtmlDocument>),
37}
38
39impl TypstDocument {
40    /// Gets the number of pages in the document.
41    pub fn num_of_pages(&self) -> u32 {
42        match self {
43            Self::Paged(doc) => doc.pages.len() as u32,
44            Self::Html(_doc) => 1u32,
45        }
46    }
47
48    /// Gets details about the document.
49    pub fn info(&self) -> &typst::model::DocumentInfo {
50        match self {
51            Self::Paged(doc) => &doc.info,
52            Self::Html(doc) => &doc.info,
53        }
54    }
55
56    /// Gets the introspector that can be queried for elements and their
57    /// positions.
58    pub fn introspector(&self) -> &typst::introspection::Introspector {
59        match self {
60            Self::Paged(doc) => &doc.introspector,
61            Self::Html(doc) => &doc.introspector,
62        }
63    }
64}
65
66impl From<Arc<well_known::TypstPagedDocument>> for TypstDocument {
67    fn from(doc: Arc<well_known::TypstPagedDocument>) -> Self {
68        Self::Paged(doc)
69    }
70}
71
72impl From<Arc<well_known::TypstHtmlDocument>> for TypstDocument {
73    fn from(doc: Arc<well_known::TypstHtmlDocument>) -> Self {
74        Self::Html(doc)
75    }
76}
77
78impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstPagedDocument> {
79    type Error = crate::Error;
80
81    fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
82        match doc {
83            TypstDocument::Paged(doc) => Ok(doc),
84            TypstDocument::Html(_doc) => {
85                crate::bail!("The document is compiled with `html` target, not `paged`.")
86            }
87        }
88    }
89}
90
91impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstHtmlDocument> {
92    type Error = crate::Error;
93
94    fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
95        match doc {
96            TypstDocument::Paged(_doc) => {
97                crate::bail!("The document is compiled with `paged` target, not `html`.")
98            }
99            TypstDocument::Html(doc) => Ok(doc),
100        }
101    }
102}
103
104use std::sync::Arc;
105
106pub use well_known::*;
107
108/// The prelude of the Typst module.
109pub mod prelude {
110    pub use comemo::Prehashed;
111    pub use ecow::{eco_format, eco_vec, EcoString, EcoVec};
112}