tytanic_filter/eval/
value.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
use super::{Error, Func, Set};
use crate::ast::{Num, Str};

/// The value of a test set expression.
#[derive(Debug, Clone)]
pub enum Value<T> {
    /// A test.
    Test(T),

    /// A test set.
    Set(Set<T>),

    /// A function.
    Func(Func<T>),

    /// An unsigned integer.
    Num(Num),

    /// A string.
    Str(Str),
}

impl<T> Value<T> {
    /// The type of this expression.
    pub fn as_type(&self) -> Type {
        match self {
            Value::Test(_) => Type::Test,
            Value::Set(_) => Type::Set,
            Value::Func(_) => Type::Func,
            Value::Num(_) => Type::Num,
            Value::Str(_) => Type::Str,
        }
    }

    /// Convert this value into a `T` or return an error.
    pub fn expect_type<V: TryFromValue<T>>(self) -> Result<V, Error>
    where
        T: Clone,
    {
        V::try_from_value(self)
    }
}

impl<T> From<Set<T>> for Value<T> {
    fn from(value: Set<T>) -> Self {
        Self::Set(value)
    }
}

impl<T> From<Func<T>> for Value<T> {
    fn from(value: Func<T>) -> Self {
        Self::Func(value)
    }
}

impl<T> From<Num> for Value<T> {
    fn from(value: Num) -> Self {
        Self::Num(value)
    }
}

impl<T> From<Str> for Value<T> {
    fn from(value: Str) -> Self {
        Self::Str(value)
    }
}

/// A trait for types which can be unwrapped from a [`Value`].
pub trait TryFromValue<T>: Sized {
    fn try_from_value(value: Value<T>) -> Result<Self, Error>;
}

/// The type of a value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Type {
    /// A test.
    Test,

    /// A test set.
    Set,

    /// A function.
    Func,

    /// An unsigned integer.
    Num,

    /// A string.
    Str,
}

impl Type {
    /// The name of this type.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Test => "test",
            Self::Set => "test set",
            Self::Func => "function",
            Self::Num => "number",
            Self::Str => "string",
        }
    }
}