summaryrefslogtreecommitdiff
path: root/src/eval/convert.rs
blob: 4c177c5b66b1aa6dc47c11fc02282e83ec859c51 (plain) (blame)
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
//! Conversion from values into other types.

use std::ops::Deref;

use fontdock::{FontStretch, FontStyle, FontWeight};

use super::{Value, ValueDict, ValueFunc};
use crate::diag::Diag;
use crate::geom::Linear;
use crate::layout::{Dir, SpecAlign};
use crate::paper::Paper;
use crate::syntax::{Ident, SpanWith, Spanned, SynTree};

/// Types that values can be converted into.
pub trait Convert: Sized {
    /// Convert a value into `Self`.
    ///
    /// If the conversion works out, this should return `Ok(...)` with an
    /// instance of `Self`. If it doesn't, it should return `Err(...)` giving
    /// back the original value.
    ///
    /// In addition to the result, the method can return an optional diagnostic
    /// to warn even when the conversion suceeded or to explain the problem when
    /// the conversion failed.
    ///
    /// The function takes a `Spanned<Value>` instead of just a `Value` so that
    /// this trait can be blanket implemented for `Spanned<T>` where `T:
    /// Convert`.
    fn convert(value: Spanned<Value>) -> (Result<Self, Value>, Option<Diag>);
}

impl<T: Convert> Convert for Spanned<T> {
    fn convert(value: Spanned<Value>) -> (Result<Self, Value>, Option<Diag>) {
        let span = value.span;
        let (result, diag) = T::convert(value);
        (result.map(|v| v.span_with(span)), diag)
    }
}

/// A value type that matches [length] values.
///
/// [length]: enum.Value.html#variant.Length
pub struct Absolute(pub f64);

impl From<Absolute> for f64 {
    fn from(abs: Absolute) -> f64 {
        abs.0
    }
}

/// A value type that matches [relative] values.
///
/// [relative]: enum.Value.html#variant.Relative
pub struct Relative(pub f64);

impl From<Relative> for f64 {
    fn from(rel: Relative) -> f64 {
        rel.0
    }
}

/// A value type that matches [identifier] and [string] values.
///
/// [identifier]: enum.Value.html#variant.Ident
/// [string]: enum.Value.html#variant.Str
pub struct StringLike(pub String);

impl From<StringLike> for String {
    fn from(like: StringLike) -> String {
        like.0
    }
}

impl Deref for StringLike {
    type Target = str;

    fn deref(&self) -> &str {
        self.0.as_str()
    }
}

macro_rules! impl_match {
    ($type:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => {
        impl Convert for $type {
            fn convert(value: Spanned<Value>) -> (Result<Self, Value>, Option<Diag>) {
                #[allow(unreachable_patterns)]
                match value.v {
                    $($p => (Ok($r), None)),*,
                    v => {
                        let err = error!("expected {}, found {}", $name, v.ty());
                        (Err(v), Some(err))
                    },
                }
            }
        }
    };
}

impl_match!(Value, "value", v => v);
impl_match!(Ident, "identifier", Value::Ident(v) => v);
impl_match!(bool, "bool", Value::Bool(v) => v);
impl_match!(i64, "integer", Value::Int(v) => v);
impl_match!(f64, "float",
    Value::Int(v) => v as f64,
    Value::Float(v) => v,
);
impl_match!(Absolute, "length", Value::Length(v) => Absolute(v));
impl_match!(Relative, "relative", Value::Relative(v) => Relative(v));
impl_match!(Linear, "linear",
    Value::Linear(v) => v,
    Value::Length(v) => Linear::abs(v),
    Value::Relative(v) => Linear::rel(v),
);
impl_match!(String, "string", Value::Str(v) => v);
impl_match!(SynTree, "tree", Value::Content(v) => v);
impl_match!(ValueDict, "dictionary", Value::Dict(v) => v);
impl_match!(ValueFunc, "function", Value::Func(v) => v);
impl_match!(StringLike, "identifier or string",
    Value::Ident(Ident(v)) => StringLike(v),
    Value::Str(v) => StringLike(v),
);

macro_rules! impl_ident {
    ($type:ty, $name:expr, $parse:expr) => {
        impl Convert for $type {
            fn convert(value: Spanned<Value>) -> (Result<Self, Value>, Option<Diag>) {
                match value.v {
                    Value::Ident(id) => {
                        if let Some(thing) = $parse(&id) {
                            (Ok(thing), None)
                        } else {
                            (Err(Value::Ident(id)), Some(error!("invalid {}", $name)))
                        }
                    }
                    v => {
                        let err = error!("expected {}, found {}", $name, v.ty());
                        (Err(v), Some(err))
                    }
                }
            }
        }
    };
}

impl_ident!(Dir, "direction", |v| match v {
    "ltr" => Some(Self::LTR),
    "rtl" => Some(Self::RTL),
    "ttb" => Some(Self::TTB),
    "btt" => Some(Self::BTT),
    _ => None,
});

impl_ident!(SpecAlign, "alignment", |v| match v {
    "left" => Some(Self::Left),
    "right" => Some(Self::Right),
    "top" => Some(Self::Top),
    "bottom" => Some(Self::Bottom),
    "center" => Some(Self::Center),
    _ => None,
});

impl_ident!(FontStyle, "font style", Self::from_str);
impl_ident!(FontStretch, "font stretch", Self::from_str);
impl_ident!(Paper, "paper", Self::from_name);

impl Convert for FontWeight {
    fn convert(value: Spanned<Value>) -> (Result<Self, Value>, Option<Diag>) {
        match value.v {
            Value::Int(number) => {
                let [min, max] = [100, 900];
                let warning = if number < min {
                    Some(warning!("the minimum font weight is {}", min))
                } else if number > max {
                    Some(warning!("the maximum font weight is {}", max))
                } else {
                    None
                };
                let weight = Self::from_number(number.min(max).max(min) as u16);
                (Ok(weight), warning)
            }
            Value::Ident(id) => {
                if let Some(thing) = FontWeight::from_str(&id) {
                    (Ok(thing), None)
                } else {
                    (Err(Value::Ident(id)), Some(error!("invalid font weight")))
                }
            }
            v => {
                let err =
                    error!("expected font weight (name or number), found {}", v.ty());
                (Err(v), Some(err))
            }
        }
    }
}