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
use crate::prelude::{
    Style,
    Value,
};

/// Values of an attribute can be in these variants
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
    /// an argument value, to be called as parameter, the function is called to the element
    FunctionCall(Value),
    /// a simple value, wrapper of primitive types
    Simple(Value),
    /// style values
    Style(Vec<Style>),
    /// no value
    Empty,
}

impl AttributeValue {
    /// create an attribute from Vec<Style>
    pub fn from_styles(styles: Vec<Style>) -> Self {
        AttributeValue::Style(styles)
    }

    /// create an attribute value from simple value
    pub fn from_value(value: Value) -> Self {
        AttributeValue::Simple(value)
    }

    /// create an attribute from a function `name` with arguments `value`
    pub fn function_call(value: Value) -> Self {
        AttributeValue::FunctionCall(value)
    }

    /// return the value if it is a Simple variant
    pub fn get_simple(&self) -> Option<&Value> {
        match self {
            AttributeValue::Simple(v) => Some(&v),
            _ => None,
        }
    }

    /// return the function call argument value if it is a FunctionCall variant
    pub fn get_function_call_value(&self) -> Option<&Value> {
        match self {
            AttributeValue::FunctionCall(v) => Some(&v),
            _ => None,
        }
    }

    /// returns true if this attribute value is a style
    pub fn is_style(&self) -> bool {
        match self {
            AttributeValue::Style(_) => true,
            _ => false,
        }
    }

    /// return the styles if the attribute value is a style
    pub fn as_style(&self) -> Option<&Vec<Style>> {
        match self {
            AttributeValue::Style(styles) => Some(styles),
            _ => None,
        }
    }

    /// return true if this is a function call
    pub fn is_function_call(&self) -> bool {
        match self {
            AttributeValue::FunctionCall(_) => true,
            _ => false,
        }
    }

    /// returns true if this attribute value is empty
    pub fn is_empty(&self) -> bool {
        match self {
            AttributeValue::Empty => true,
            _ => false,
        }
    }
}