summaryrefslogtreecommitdiff
path: root/src/syntax/func/values.rs
blob: a767aef65a343229f29bbab6b969e399c369bb2c (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
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
use std::fmt::{self, Display, Formatter};
use std::marker::PhantomData;
use toddle::query::{FontStyle, FontWeight};

use crate::layout::prelude::*;
use crate::size::{Size, ScaleSize};
use crate::style::Paper;
use super::*;

use self::AlignmentValue::*;


pub trait Value {
    type Output;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error>;
}

impl<V: Value> Value for Spanned<V> {
    type Output = Spanned<V::Output>;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        let span = expr.span;
        V::parse(expr).map(|v| Spanned { v, span })
    }
}

macro_rules! value {
    ($type:ty, $output:ty, $name:expr, $($p:pat => $r:expr),* $(,)?) => {
        impl Value for $type {
            type Output = $output;

            fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
                #[allow(unreachable_patterns)]
                match expr.v {
                    $($p => Ok($r)),*,
                    other => Err(err!("expected {}, found {}",
                                      $name, other.name())),
                }
            }
        }
    };
}

value!(Expr,   Self, "expression", e => e);

value!(Ident,  Self, "identifier", Expr::Ident(i)  => i);
value!(String, Self, "string",     Expr::Str(s)    => s);
value!(f64,    Self, "number",     Expr::Number(n) => n);
value!(bool,   Self, "bool",       Expr::Bool(b)   => b);
value!(Size,   Self, "size",       Expr::Size(s)   => s);
value!(Tuple,  Self, "tuple",      Expr::Tuple(t)  => t);
value!(Object, Self, "object",     Expr::Object(o) => o);

value!(ScaleSize, Self, "number or size",
    Expr::Size(size)    => ScaleSize::Absolute(size),
    Expr::Number(scale) => ScaleSize::Scaled(scale as f32),
);

pub struct StringLike;

value!(StringLike, String, "identifier or string",
    Expr::Ident(Ident(s)) => s,
    Expr::Str(s) => s,
);

pub struct Defaultable<T>(PhantomData<T>);

impl<T: Value> Value for Defaultable<T> {
    type Output = Option<T::Output>;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        match expr.v {
            Expr::Ident(ident) if ident.as_str() == "default" => Ok(None),
            _ => T::parse(expr).map(Some)
        }
    }
}

impl Value for FontStyle {
    type Output = Self;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        FontStyle::from_str(Ident::parse(expr)?.as_str())
            .ok_or_else(|| err!("invalid font style"))
    }
}

impl Value for FontWeight {
    type Output = (Self, bool);

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        match expr.v {
            Expr::Number(weight) => {
                let weight = weight.round();

                if weight >= 100.0 && weight <= 900.0 {
                    Ok((FontWeight(weight as i16), false))
                } else {
                    let clamped = weight.min(900.0).max(100.0) as i16;
                    Ok((FontWeight(clamped), true))
                }
            }
            Expr::Ident(id) => {
                FontWeight::from_str(id.as_str())
                    .ok_or_else(|| err!("invalid font weight"))
                    .map(|weight| (weight, false))
            }
            other => Err(err!("expected identifier or number, \
                               found {}", other.name())),
        }
    }
}

impl Value for Paper {
    type Output = Self;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        Paper::from_str(Ident::parse(expr)?.as_str())
            .ok_or_else(|| err!("invalid paper type"))
    }
}

impl Value for Direction {
    type Output = Self;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        Ok(match Ident::parse(expr)?.as_str() {
            "left-to-right" | "ltr" | "LTR" => LeftToRight,
            "right-to-left" | "rtl" | "RTL" => RightToLeft,
            "top-to-bottom" | "ttb" | "TTB" => TopToBottom,
            "bottom-to-top" | "btt" | "BTT" => BottomToTop,
            _ => return Err(err!("invalid direction"))
        })
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum AlignmentValue {
    Align(Alignment),
    Left,
    Top,
    Right,
    Bottom,
}

impl AlignmentValue {
    /// The generic axis this alignment corresponds to in the given system of
    /// layouting axes. `None` if the alignment is generic.
    pub fn axis(self, axes: LayoutAxes) -> Option<GenericAxis> {
        match self {
            Left | Right => Some(Horizontal.to_generic(axes)),
            Top | Bottom => Some(Vertical.to_generic(axes)),
            Align(_) => None,
        }
    }

    /// The generic version of this alignment in the given system of layouting
    /// axes.
    ///
    /// Returns `None` if the alignment is invalid for the given axis.
    pub fn to_generic(self, axes: LayoutAxes, axis: GenericAxis) -> Option<Alignment> {
        let specific = axis.to_specific(axes);
        let start = match axes.get(axis).is_positive() {
            true => Origin,
            false => End,
        };

        match (self, specific) {
            (Align(alignment), _) => Some(alignment),
            (Left, Horizontal) | (Top, Vertical) => Some(start),
            (Right, Horizontal) | (Bottom, Vertical) => Some(start.inv()),
            _ => None
        }
    }

    /// The specific version of this alignment in the given system of layouting
    /// axes.
    pub fn to_specific(self, axes: LayoutAxes, axis: SpecificAxis) -> AlignmentValue {
        let direction = axes.get_specific(axis);
        if let Align(alignment) = self {
            match (direction, alignment) {
                (LeftToRight, Origin) | (RightToLeft, End) => Left,
                (LeftToRight, End) | (RightToLeft, Origin) => Right,
                (TopToBottom, Origin) | (BottomToTop, End) => Top,
                (TopToBottom, End) | (BottomToTop, Origin) => Bottom,
                (_, Center) => self,
            }
        } else {
            self
        }
    }
}

impl Value for AlignmentValue {
    type Output = Self;

    fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> {
        Ok(match Ident::parse(expr)?.as_str() {
            "origin" => Align(Origin),
            "center" => Align(Center),
            "end"    => Align(End),
            "left"   => Left,
            "top"    => Top,
            "right"  => Right,
            "bottom" => Bottom,
            _ => return Err(err!("invalid alignment"))
        })
    }
}

impl Display for AlignmentValue {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Align(Origin) => write!(f, "origin"),
            Align(Center) => write!(f, "center"),
            Align(End) => write!(f, "end"),
            Left => write!(f, "left"),
            Top => write!(f, "top"),
            Right => write!(f, "right"),
            Bottom => write!(f, "bottom"),
        }
    }
}