typst_as_lib/
conversions.rs1use typst::{
2 foundations::Bytes,
3 syntax::{package::PackageSpec, FileId, Source, VirtualPath},
4 text::Font,
5};
6
7pub trait IntoFileId {
8 fn into_file_id(self) -> FileId
9 where
10 Self: std::marker::Sized;
11}
12
13impl IntoFileId for FileId {
14 fn into_file_id(self) -> FileId {
15 self
16 }
17}
18
19impl IntoFileId for &str {
20 fn into_file_id(self) -> FileId {
21 FileId::new(None, VirtualPath::new(self))
22 }
23}
24
25impl IntoFileId for (PackageSpec, &str) {
26 fn into_file_id(self) -> FileId {
27 let (p, id) = self;
28 FileId::new(Some(p), VirtualPath::new(id))
29 }
30}
31
32pub trait IntoSource {
33 fn into_source(self) -> Source
34 where
35 Self: std::marker::Sized;
36}
37
38impl IntoSource for Source {
39 fn into_source(self) -> Source {
40 self
41 }
42}
43
44impl IntoSource for (&str, String) {
45 fn into_source(self) -> Source {
46 let (path, source) = self;
47 let id = FileId::new(None, VirtualPath::new(path));
48 Source::new(id, source)
49 }
50}
51
52impl IntoSource for (&str, &str) {
53 fn into_source(self) -> Source {
54 let (path, source) = self;
55 (path, source.to_owned()).into_source()
56 }
57}
58
59impl IntoSource for (FileId, String) {
60 fn into_source(self) -> Source {
61 let (id, source) = self;
62 Source::new(id, source)
63 }
64}
65
66impl IntoSource for (FileId, &str) {
67 fn into_source(self) -> Source {
68 let (id, source) = self;
69 Source::new(id, source.to_owned())
70 }
71}
72
73impl IntoSource for String {
74 fn into_source(self) -> Source {
75 Source::detached(self)
76 }
77}
78
79impl IntoSource for &str {
80 fn into_source(self) -> Source {
81 Source::detached(self.to_owned())
82 }
83}
84
85pub trait IntoBytes {
86 fn into_bytes(self) -> Bytes
87 where
88 Self: std::marker::Sized;
89}
90
91impl IntoBytes for &[u8] {
92 fn into_bytes(self) -> Bytes {
93 Bytes::new(self.to_vec())
94 }
95}
96
97impl IntoBytes for Vec<u8> {
98 fn into_bytes(self) -> Bytes {
99 Bytes::new(self)
100 }
101}
102
103impl IntoBytes for Bytes {
104 fn into_bytes(self) -> Bytes {
105 self
106 }
107}
108
109pub trait IntoFonts
110where
111 Self: std::marker::Sized,
112{
113 fn into_fonts(self) -> Box<dyn Iterator<Item = Font>>;
114}
115
116impl IntoFonts for &[u8] {
117 fn into_fonts(self) -> Box<dyn Iterator<Item = Font>> {
118 Box::new(Font::iter(Bytes::new(self.to_vec())))
119 }
120}
121
122impl IntoFonts for Vec<u8> {
123 fn into_fonts(self) -> Box<dyn Iterator<Item = Font>> {
124 Box::new(Font::iter(Bytes::new(self)))
125 }
126}
127
128impl IntoFonts for Font {
129 fn into_fonts(self) -> Box<dyn Iterator<Item = Font>> {
130 Box::new(std::iter::once(self))
131 }
132}
133
134impl IntoFonts for Bytes {
135 fn into_fonts(self) -> Box<dyn Iterator<Item = Font>> {
136 Box::new(Font::iter(self))
137 }
138}