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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
pub use typst_macros::{cast, Cast};
use std::fmt::Write;
use std::ops::Add;
use ecow::EcoString;
use super::Value;
use crate::diag::{At, SourceResult, StrResult};
use crate::syntax::{Span, Spanned};
use crate::util::separated_list;
/// Determine details of a type.
///
/// Type casting works as follows:
/// - [`Reflect for T`](Reflect) describes the possible Typst values for `T`
/// (for documentation and autocomplete).
/// - [`IntoValue for T`](IntoValue) is for conversion from `T -> Value`
/// (infallible)
/// - [`FromValue for T`](FromValue) is for conversion from `Value -> T`
/// (fallible).
///
/// We can't use `TryFrom<Value>` due to conflicting impls. We could use
/// `From<T> for Value`, but that inverses the impl and leads to tons of
/// `.into()` all over the place that become hard to decipher.
pub trait Reflect {
/// Describe the acceptable values for this type.
fn describe() -> CastInfo;
/// Whether the given value can be converted to `T`.
///
/// This exists for performance. The check could also be done through the
/// [`CastInfo`], but it would be much more expensive (heap allocation +
/// dynamic checks instead of optimized machine code for each type).
fn castable(value: &Value) -> bool;
/// Produce an error message for an inacceptable value.
///
/// ```
/// # use typst::eval::{Int, Reflect, Value};
/// assert_eq!(
/// <Int as Reflect>::error(Value::None),
/// "expected integer, found none",
/// );
/// ```
fn error(found: &Value) -> EcoString {
Self::describe().error(found)
}
}
impl Reflect for Value {
fn describe() -> CastInfo {
CastInfo::Any
}
fn castable(_: &Value) -> bool {
true
}
}
impl<T: Reflect> Reflect for Spanned<T> {
fn describe() -> CastInfo {
T::describe()
}
fn castable(value: &Value) -> bool {
T::castable(value)
}
}
impl<T: Reflect> Reflect for StrResult<T> {
fn describe() -> CastInfo {
T::describe()
}
fn castable(value: &Value) -> bool {
T::castable(value)
}
}
impl<T: Reflect> Reflect for SourceResult<T> {
fn describe() -> CastInfo {
T::describe()
}
fn castable(value: &Value) -> bool {
T::castable(value)
}
}
impl<T: Reflect> Reflect for &T {
fn describe() -> CastInfo {
T::describe()
}
fn castable(value: &Value) -> bool {
T::castable(value)
}
}
impl<T: Reflect> Reflect for &mut T {
fn describe() -> CastInfo {
T::describe()
}
fn castable(value: &Value) -> bool {
T::castable(value)
}
}
/// Cast a Rust type into a Typst [`Value`].
///
/// See also: [`Reflect`].
pub trait IntoValue {
/// Cast this type into a value.
fn into_value(self) -> Value;
}
impl IntoValue for Value {
fn into_value(self) -> Value {
self
}
}
impl<T: IntoValue> IntoValue for Spanned<T> {
fn into_value(self) -> Value {
self.v.into_value()
}
}
/// Cast a Rust type or result into a [`SourceResult<Value>`].
///
/// Converts `T`, [`StrResult<T>`], or [`SourceResult<T>`] into
/// [`SourceResult<Value>`] by `Ok`-wrapping or adding span information.
pub trait IntoResult {
/// Cast this type into a value.
fn into_result(self, span: Span) -> SourceResult<Value>;
}
impl<T: IntoValue> IntoResult for T {
fn into_result(self, _: Span) -> SourceResult<Value> {
Ok(self.into_value())
}
}
impl<T: IntoValue> IntoResult for StrResult<T> {
fn into_result(self, span: Span) -> SourceResult<Value> {
self.map(IntoValue::into_value).at(span)
}
}
impl<T: IntoValue> IntoResult for SourceResult<T> {
fn into_result(self, _: Span) -> SourceResult<Value> {
self.map(IntoValue::into_value)
}
}
/// Try to cast a Typst [`Value`] into a Rust type.
///
/// See also: [`Reflect`].
pub trait FromValue<V = Value>: Sized + Reflect {
/// Try to cast the value into an instance of `Self`.
fn from_value(value: V) -> StrResult<Self>;
}
impl FromValue for Value {
fn from_value(value: Value) -> StrResult<Self> {
Ok(value)
}
}
impl<T: FromValue> FromValue<Spanned<Value>> for T {
fn from_value(value: Spanned<Value>) -> StrResult<Self> {
T::from_value(value.v)
}
}
impl<T: FromValue> FromValue<Spanned<Value>> for Spanned<T> {
fn from_value(value: Spanned<Value>) -> StrResult<Self> {
let span = value.span;
T::from_value(value.v).map(|t| Spanned::new(t, span))
}
}
/// Describes a possible value for a cast.
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd)]
pub enum CastInfo {
/// Any value is okay.
Any,
/// A specific value, plus short documentation for that value.
Value(Value, &'static str),
/// Any value of a type.
Type(&'static str),
/// Multiple alternatives.
Union(Vec<Self>),
}
impl CastInfo {
/// Produce an error message describing what was expected and what was
/// found.
pub fn error(&self, found: &Value) -> EcoString {
fn accumulate(
info: &CastInfo,
found: &Value,
parts: &mut Vec<EcoString>,
matching_type: &mut bool,
) {
match info {
CastInfo::Any => parts.push("anything".into()),
CastInfo::Value(value, _) => {
parts.push(value.repr().into());
if value.type_name() == found.type_name() {
*matching_type = true;
}
}
CastInfo::Type(ty) => parts.push((*ty).into()),
CastInfo::Union(options) => {
for option in options {
accumulate(option, found, parts, matching_type);
}
}
}
}
let mut matching_type = false;
let mut parts = vec![];
accumulate(self, found, &mut parts, &mut matching_type);
let mut msg = String::from("expected ");
if parts.is_empty() {
msg.push_str(" nothing");
}
msg.push_str(&separated_list(&parts, "or"));
if !matching_type {
msg.push_str(", found ");
msg.push_str(found.type_name());
}
if_chain::if_chain! {
if let Value::Int(i) = found;
if parts.iter().any(|p| p == "length");
if !matching_type;
then {
write!(msg, ": a length needs a unit - did you mean {i}pt?").unwrap();
}
};
msg.into()
}
}
impl Add for CastInfo {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self::Union(match (self, rhs) {
(Self::Union(mut lhs), Self::Union(rhs)) => {
for cast in rhs {
if !lhs.contains(&cast) {
lhs.push(cast);
}
}
lhs
}
(Self::Union(mut lhs), rhs) => {
if !lhs.contains(&rhs) {
lhs.push(rhs);
}
lhs
}
(lhs, Self::Union(mut rhs)) => {
if !rhs.contains(&lhs) {
rhs.insert(0, lhs);
}
rhs
}
(lhs, rhs) => vec![lhs, rhs],
})
}
}
/// A container for a variadic argument.
pub trait Variadics {
/// The contained type.
type Inner;
}
impl<T> Variadics for Vec<T> {
type Inner = T;
}
/// An uninhabitable type.
pub enum Never {}
impl Reflect for Never {
fn describe() -> CastInfo {
CastInfo::Union(vec![])
}
fn castable(_: &Value) -> bool {
false
}
}
impl IntoValue for Never {
fn into_value(self) -> Value {
match self {}
}
}
impl FromValue for Never {
fn from_value(value: Value) -> StrResult<Self> {
Err(Self::error(&value))
}
}
|