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
use itertools::Itertools;
use std::collections::HashMap;
use std::iter::FromIterator;

#[derive(Clone)]
pub struct Schema {
    pub definitions: Vec<SchemaDefinition>
}

impl Schema {
    pub fn new() -> Self {
        Self {
            definitions: vec![]
        }
    }

    pub fn add_definition(mut self, definition: SchemaDefinition) -> Self {
        self.definitions.push(definition);
        self
    }

    pub fn add_definition_ref(&mut self, definition: SchemaDefinition) {
        self.definitions.push(definition);
    }
}

impl ToString for Schema {
    fn to_string(&self) -> String {
        let mut representation = self.definitions.iter()
            .map(|def| def.to_string())
            .join("\n");
        representation += "\n\n";

        let predicate_map: HashMap<&String, &PredicateDefinition> = HashMap::from_iter(self.definitions.iter()
            .map(|def| &def.predicates)
            .flatten()
            .map(|pred| (&pred.name, pred)));

        representation += &predicate_map.iter().map(|(name, pred)| pred.to_string()).join("\n");

        representation
    }
}

#[derive(Clone)]
pub struct SchemaDefinition {
    name: String,
    predicates: Vec<PredicateDefinition>
}

impl SchemaDefinition {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            predicates: vec![]
        }
    }

    pub fn add_predicate(mut self, predicate: PredicateDefinition) -> Self {
        self.predicates.push(predicate);
        self
    }

    pub fn add_predicate_ref(&mut self, predicate: PredicateDefinition) {
        self.predicates.push(predicate);
    }

}

impl ToString for SchemaDefinition {
    fn to_string(&self) -> String {
        format!(
            "type {type_name} {{\n\
                \t{predicates}\n\
            }}",
        type_name = &self.name,
        predicates = &self.predicates.iter().map(|pred| &pred.name).join("\n\t"))
    }
}

#[derive(Clone)]
pub struct PredicateDefinition {
    name: String,
    predicate_type: PredicateType,
    indexing: Vec<Indexing>,
    upsert: bool
}

impl PredicateDefinition {
    pub fn new(name: &str, predicate_type: PredicateType) -> Self {
        Self {
            name: name.to_string(),
            predicate_type,
            indexing: vec![],
            upsert: false
        }
    }

    pub fn add_index(mut self, index: Indexing) -> Self {
        self.indexing.push(index);
        self
    }

    pub fn add_index_ref(&mut self, index: Indexing) {
        self.indexing.push(index);
    }

    pub fn upsert(mut self) -> Self {
        self.upsert = true;
        self
    }
}

impl ToString for PredicateDefinition {
    fn to_string(&self) -> String {
        let mut index = format!("");

        if !self.indexing.is_empty() {
            index = format!(
                " @index({})",
                &self.indexing.iter().map(|pred| pred.to_string()).join(", ")
            );
        }

        let upsert = if self.upsert {
            format!(" @upsert")
        } else {
            format!("")
        };

        format!(
            "{name}: {ptype}{index}{upsert} .",
            name = &self.name,
            ptype = self.predicate_type.to_string(),
            index = index,
            upsert = upsert
        )
    }
}

#[derive(Clone)]
pub enum PredicateType {
    String,
    StringArray,
    UID,
    UIDArray,
    INT,
    INTArray
}

impl ToString for PredicateType {
    fn to_string(&self) -> String {
        match self {
            PredicateType::String => format!("string"),
            PredicateType::StringArray => format!("[string]"),
            PredicateType::UID => format!("uid"),
            PredicateType::UIDArray => format!("[uid]"),
            PredicateType::INT => format!("int"),
            PredicateType::INTArray => format!("[int]")
        }
    }
}

#[derive(Clone)]
pub enum Indexing {
    TERM,
    TRIGRAM,
    INT,
    EXACT
}

impl ToString for Indexing {
    fn to_string(&self) -> String {
        match self {
            Indexing::TERM => format!("term"),
            Indexing::TRIGRAM => format!("trigram"),
            Indexing::INT => format!("int"),
            Indexing::EXACT => format!("exact")
        }
    }
}