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
use yoga::{FlexStyle, StyleUnit, Layout as Dimension};
use ordered_float::OrderedFloat;
use std::collections::HashMap;
use serde_json::Value;

use types::{
    PropertiesExpressions,
    PropertiesLayout,
    ProcessingError,
    Properties,
    Variable,
    pair_to_flex
};

use traits::{
    TStyleContext,
    TStyleCollect,
    TStyleStates,
};

/// Style dimensions context
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DimensionsContext {
    pub current: Option<Dimension>,
    pub parent: Option<Dimension>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DimensionType {
    Current,
    Parent,
}

/// Context with other needed info - for parse and prepares,
/// aka dimensions screen, element measures, variables, and other.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Context {
    // Variables for preset before configurations
    pub variables: HashMap<String, Variable>,
    // Layout props this container
    pub dimensions: DimensionsContext,
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct CollectedStyle {
    layout: Vec<FlexStyle>,
}

/// Style element, with all element status, and context`s,
/// with implementations of traits for parse unions of one element
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Style {
    // States of properties as :hover, :active, etc..
    pub states: HashMap<String, Properties>,

    // Context
    pub context: Context,

    // Collected styles, after compilation by enables states
    pub collected: CollectedStyle,

    // Enabled states of current style
    pub enabled_states: Vec<String>,
}

/// Default context representation for Parsing Trait
/// Trait `TParseStyleMiddleware` implemented in jss_derive as proc-macro
#[derive(Debug, Clone)]
pub struct ParseStyleMiddleware {}

/* _______________________________________________________________________ */
fn set_dimension_variable(context: &mut Context, name: String, dimension: &Option<Dimension>) {
    extract!(Some(_), dimension)
        .and_then(|dimension| {
            let self_variable = hashmap!{
                "bottom" => dimension.bottom(),
                "right" => dimension.right(),
                "left" => dimension.left(),
                "top" => dimension.top(),
                "height" => dimension.height(),
                "width" => dimension.width(),
            }
            .into_iter()
            .map(|(k, v)| (k.to_string(), v))
            .collect::<HashMap<String, f32>>();

            context.set_variable(name, Variable::Map(self_variable));
            Some(())
        })
        .is_some();
}

impl TStyleContext for Context {
    fn set_dimension(&mut self, entry_type: DimensionType, dimension: Option<Dimension>) {
        match entry_type {
            DimensionType::Current => {
                set_dimension_variable(self, "$self".to_string(), &dimension);
                self.dimensions.current = dimension;
            }

            DimensionType::Parent => {
                set_dimension_variable(self, "$parent".to_string(), &dimension);
                self.dimensions.parent = dimension;
            }
        }
    }

    fn set_variable(&mut self, name: String, value: Variable) {
        self.variables.insert(name, value).is_some();
    }

    fn set_variables<T>(&mut self, variables: T)
    where
        T: IntoIterator<Item = (String, Variable)>,
    {
        for (name, value) in variables {
            self.variables.insert(name, value).is_some();
        }
    }
}

impl TStyleStates for Style {
    // @todo: adding check for state exists
    fn enable_states(&mut self, states: Vec<String>) {
        self.enabled_states = states;
    }
}

impl TStyleCollect for Style {
    fn collect_layout_style(&self) -> (Vec<FlexStyle>, Vec<ProcessingError>) {
        use self::ProcessingError::*;

        let mut layout_styles = vec![];
        let mut eval_errors = vec![];

        let mut expressions = PropertiesExpressions::default();
        let mut layout = PropertiesLayout::default();

        for state in self.enabled_states.iter() {
            let state_expressions = self.states[state].expressions.0.clone();
            let state_layout = self.states[state].layout.0.clone();

            expressions.0.extend(state_expressions);
            layout.0.extend(state_layout);
        }

        // Run expressions
        for (property, expr) in expressions.0 {
            let mut expression = expr.clone();

            // Set variables into runtime expression
            for (name, value) in self.context.variables.iter() {
                match value {
                    Variable::Number(num) => {
                        expression = expression.value(name.clone(), num);
                    }
                    Variable::Map(map) => {
                        expression = expression.value(name.clone(), map);
                    }
                }
            }

            match expression.exec() {
                Err(error) => eval_errors.push(ExecFailed {
                    property,
                    error,
                }),

                Ok(value) => {
                    let number_msg = "expected float or integer";

                    let make_type_error = |msg: &str| InvalidType {
                        expected: msg.to_string(),
                        property: property.clone(),
                    };

                    extract!(Value::Number(_), value)
                        .ok_or(make_type_error(number_msg))
                        .and_then(|n| n.as_f64().ok_or(make_type_error(number_msg)))
                        .and_then(|number| {
                            let number: OrderedFloat<f32> = (number as f32).into();

                            pair_to_flex(property.clone(), StyleUnit::Point(number))
                                .map_err(|_| make_type_error("valid unit by key"))
                        })
                        .and_then(|flex_style| {
                            layout_styles.push(flex_style);
                            Ok(())
                        })
                        .is_ok();
                }
            }
        }

        // Setter static property
        for (_property, value) in layout.0 {
            layout_styles.push(value);
        }

        (layout_styles, eval_errors)
    }
}