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
use std::collections::HashSet;

#[derive(PartialEq, Clone, Debug)]
pub enum NodeType {
    Text(String),
    Binding(String),
    Group,
    Button(ButtonData),
    LineInput(LineInputData),
    ProgressBar(ProgressBarData),
    Template(TemplateData),
    Repeat(RepeatData),
    // Special Root Nodes
    RootView,
    RootTemplate
}

#[derive(Clone, Debug)]
pub struct Node {
    pub children: Vec<Node>,
    classes: Option<String>,
    pub node_type: NodeType,
}

impl Node {
    pub fn tree_size(&self) -> usize {
        let mut count = 1;

        for kid in &self.children {
            count += kid.tree_size();
        }

        count
    }
}

pub type Template = Node;
pub type View = Node;

pub fn new_template(classes: Option<String>) -> Template {
    Node {
        children: Vec::new(),
        node_type: NodeType::RootTemplate,
        classes: classes
    }
}

pub fn new_view(classes: Option<String>) -> View {
    Node {
        children: Vec::new(),
        node_type: NodeType::RootView,
        classes: classes,
    }
}

impl Node {

    pub fn new(classes: Option<String>, nt: NodeType) -> Node {
        Node {
            children: Vec::new(),
            node_type: nt,
            classes: classes,
        }
    }

    pub fn from_template(other: &Template, nt: NodeType) -> Node {
        Node {
            children: other.children.clone(),
            node_type: nt,
            classes: None
        }
    }

    pub fn classes(&self) -> HashSet<&str> {
        match self.classes {
            Some(ref classlist) => classlist.split(' ').collect(),
            None => HashSet::new()
        }
    }
}

// ------------------------------------------------- Button tag
#[derive(PartialEq, Clone, Debug)]
pub struct ButtonData {
    pub gotoview: Option<String>,
    pub action: Option<String>,
    pub key: Option<String>,
}

// ------------------------------------------------- Line input tag
#[derive(PartialEq, Clone, Debug)]
pub struct LineInputData {
    pub value: Option<String>,
    pub key: Option<String>,
}

// ------------------------------------------------- Progress bar tag
#[derive(PartialEq, Clone, Debug)]
pub struct ProgressBarData {
    pub value: Option<String>
}

// ------------------------------------------------- Template tag
#[derive(PartialEq, Clone, Debug)]
pub struct TemplateData {
    pub path: String,
}

// ------------------------------------------------- Repeat tag
#[derive(PartialEq, Clone, Debug)]
pub struct RepeatData {
    pub template_name: String,
    pub iter: String,
}