tinymist_std/concepts/
typst.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Re-export of the typst crate.
pub(crate) mod well_known {
    pub use typst::foundations::Bytes;

    pub use typst::utils::LazyHash;

    /// Although this is not good to expose this, we make an alias here to
    /// let it as a part of typst-ts.
    pub use typst::syntax::FileId as TypstFileId;

    pub use typst::World as TypstWorld;

    pub use typst::layout::Abs as TypstAbs;

    pub use typst::Document as TypstDocumentTrait;

    pub use typst::layout::PagedDocument as TypstPagedDocument;

    pub use typst::html::HtmlDocument as TypstHtmlDocument;

    pub use typst::text::Font as TypstFont;

    pub use typst::foundations::Dict as TypstDict;

    pub use typst::foundations::Datetime as TypstDatetime;

    pub use typst::{diag, foundations, syntax};
}

/// The enum of all well-known Typst documents.
#[derive(Debug, Clone)]
pub enum TypstDocument {
    /// The document compiled with `paged` target.
    Paged(Arc<well_known::TypstPagedDocument>),
    /// The document compiled with `html` target.
    Html(Arc<well_known::TypstHtmlDocument>),
}

impl TypstDocument {
    /// Gets the number of pages in the document.
    pub fn num_of_pages(&self) -> u32 {
        match self {
            Self::Paged(doc) => doc.pages.len() as u32,
            Self::Html(_doc) => 1u32,
        }
    }

    /// Gets details about the document.
    pub fn info(&self) -> &typst::model::DocumentInfo {
        match self {
            Self::Paged(doc) => &doc.info,
            Self::Html(doc) => &doc.info,
        }
    }

    /// Gets the introspector that can be queried for elements and their
    /// positions.
    pub fn introspector(&self) -> &typst::introspection::Introspector {
        match self {
            Self::Paged(doc) => &doc.introspector,
            Self::Html(doc) => &doc.introspector,
        }
    }
}

impl From<Arc<well_known::TypstPagedDocument>> for TypstDocument {
    fn from(doc: Arc<well_known::TypstPagedDocument>) -> Self {
        Self::Paged(doc)
    }
}

impl From<Arc<well_known::TypstHtmlDocument>> for TypstDocument {
    fn from(doc: Arc<well_known::TypstHtmlDocument>) -> Self {
        Self::Html(doc)
    }
}

impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstPagedDocument> {
    type Error = crate::Error;

    fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
        match doc {
            TypstDocument::Paged(doc) => Ok(doc),
            TypstDocument::Html(_doc) => {
                crate::bail!("The document is compiled with `html` target, not `paged`.")
            }
        }
    }
}

impl<'a> TryFrom<&'a TypstDocument> for &'a Arc<well_known::TypstHtmlDocument> {
    type Error = crate::Error;

    fn try_from(doc: &'a TypstDocument) -> Result<Self, Self::Error> {
        match doc {
            TypstDocument::Paged(_doc) => {
                crate::bail!("The document is compiled with `paged` target, not `html`.")
            }
            TypstDocument::Html(doc) => Ok(doc),
        }
    }
}

use std::sync::Arc;

pub use well_known::*;

/// The prelude of the Typst module.
pub mod prelude {
    pub use comemo::Prehashed;
    pub use ecow::{eco_format, eco_vec, EcoString, EcoVec};
}