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
use utils::{apperance_keys_contains, layout_keys_contains};
use types::{Case, PropertyError};
use inflector::Inflector;

/// Middleware enum with property type of current style field
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyParseType {
    /// Calculator expression parser. For properties that use calc in runtime. Key started from "~"
    Expression,
    /// Default parser middleware. Key without prefix for standart properties
    Default,
    /// Custom parser middleware. Key started from "@"
    Custom,
}

/// Middleware enum with property type of current stylesheet field
#[derive(Clone, Debug, PartialEq)]
pub enum FieldParseType {
    /// Stylesheet global variable started from "$"
    Variable,
    /// Default parser middleware. Key without prefix for standart style name
    Default,
    /// Custom parser middleware. Key started from "@"
    Custom,
}

/// Expanded info about property key - needed middleware, case, etc..
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyKeyInfo {
    // @todo: next adding args type for property
    pub key_type: PropertyParseType,
    pub source: String,
    pub name: String,
    pub case: Case,
}

impl PropertyKeyInfo {
    /// Create a new info instanse, this method return error if key isnt valid
    pub fn new(key: &str) -> Result<PropertyKeyInfo, PropertyError> {
        let prefix = match &key[..1] {
            "~" => Ok(PropertyParseType::Expression),
            "@" => Ok(PropertyParseType::Custom),
            _ => {
                let snake_key = key.to_snake_case();

                if apperance_keys_contains(snake_key.as_str()) || layout_keys_contains(snake_key.as_str()) {
                    Ok(PropertyParseType::Default)
                } else {
                    Err(PropertyError::InvalidKey {
                        key: key.to_string(),
                    })
                }
            }
        }?;

        let name = if prefix != PropertyParseType::Default {
            key[1..].to_string()
        } else {
            key.to_string()
        };

        Ok(PropertyKeyInfo {
            case: Case::new(name.as_str()),
            source: key.to_string(),
            key_type: prefix,
            name,
        })
    }
}

pub struct StylesheetFieldInfo {
    pub key_type: FieldParseType,
    pub source: String,
    pub name: String,
    pub case: Case,
}

impl StylesheetFieldInfo {
    pub fn new(key: &str) -> StylesheetFieldInfo {
        let prefix = match &key[..1] {
            "$" => FieldParseType::Variable,
            "@" => FieldParseType::Custom,
            _ => FieldParseType::Default,
        };

        let name = if prefix != FieldParseType::Default {
            key[1..].to_string()
        } else {
            key.to_string()
        };

        StylesheetFieldInfo {
            case: Case::new(name.as_str()),
            source: key.to_string(),
            key_type: prefix,
            name,
        }
    } 
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_key_info() {
        let default = extract!(Ok(_), PropertyKeyInfo::new("background"));
        let custom = extract!(Ok(_), PropertyKeyInfo::new("@custom"));
        let expr = extract!(Ok(_), PropertyKeyInfo::new("~expr_value"));

        let exp_default = Some(PropertyKeyInfo {
            key_type: PropertyParseType::Default,
            source: "background".to_string(),
            name: "background".to_string(),
            case: Case::Snake,
        });
        let exp_custom = Some(PropertyKeyInfo {
            key_type: PropertyParseType::Custom,
            source: "@custom".to_string(),
            name: "custom".to_string(),
            case: Case::Snake,
        });
        let exp_expr = Some(PropertyKeyInfo {
            key_type: PropertyParseType::Expression,
            source: "~expr_value".to_string(),
            name: "expr_value".to_string(),
            case: Case::Snake,
        });

        assert_eq!(default, exp_default);
        assert_eq!(custom, exp_custom);
        assert_eq!(expr, exp_expr);
    }
}