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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

//! Configuration structures and macros

use std::io::Error;
use std::path::PathBuf;
use std::default::Default;
use std::fmt;

use yaml_rust::ScanError;

/// Configuration error
pub enum ConfigError {
    YamlScan(ScanError),
    YamlParse(ConfigMeta),
    NonConfig,
    FileError(PathBuf, Error),
    MultipleExternalFiles(PathBuf, Vec<PathBuf>),
    MissingExternalFile(ConfigMeta),
}

impl ConfigError {
    pub fn to_string(&self) -> String {
        match self {
            &ConfigError::YamlScan(ref e) => format!("Failed to scan YAML: {}", e),
            &ConfigError::YamlParse(ref meta) => {
                let tree = meta.tree();

                let message = if meta.bad_value {
                    "Could not find YAML"
                }
                else {
                    "Failed to parse YAML"
                };

                let path = meta.path.display().to_string() + ": ";

                let basic = format!("{}{}: {}: expected {}", path, message, tree, meta.ty);

                let options = if meta.options.len() > 0 {
                    let mut result = "".to_string();

                    for (index, option) in meta.options.iter().enumerate() {
                        if index != 0 {
                            result = result + ", ";
                        }

                        result = result + option;
                    }

                    format!("\n{}:\t {} {{ {} }}", path, meta.ty, result)
                }
                else {
                    "".to_string()
                };

                format!("{}{}", basic, options)
            },
            &ConfigError::NonConfig => "Attempted usage of a struct function on a field.".to_string(),
            &ConfigError::FileError(ref path, ref e) => {
                format!("{}: Config File Error: {}", path.display().to_string(), e)
            },
            &ConfigError::MultipleExternalFiles(ref path, ref conflicts) => {
                let mut result = "".to_string();

                for (index, conflict) in conflicts.iter().enumerate() {
                    if index != 0 {
                        result = result + ",\n\t";
                    }

                    result = result + &conflict.display().to_string();
                }

                format!("{}: Multiple external files: \n\t{}", path.display().to_string(), result)
            },
            &ConfigError::MissingExternalFile(ref meta) => {
                format!("{}: External YAML file is missing", meta.path.display().to_string())
            },
        }
    }
}

impl fmt::Debug for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

/// Metadata for a configuration structure
#[derive(Clone, Debug)]
pub struct ConfigMeta {
    /// Where the file is located, defaults to "config/config.yml"
    pub path: PathBuf,

    /// List from top-level to bottom-level configs
    pub fields: Vec<String>,

    /// Parent meta
    pub parent: Option<Box<ConfigMeta>>,

    /// String representation of the type
    pub ty: &'static str,

    /// Name of the field
    pub name: &'static str,

    /// Whether key is bad or not
    pub bad_value: bool,

    /// Options to display to user, usually used for enums
    pub options: Vec<String>,
}

impl Default for ConfigMeta {
    fn default() -> Self {
        ConfigMeta {
            path: PathBuf::from("config/config.yml"),
            fields: Vec::new(),
            parent: None,
            ty: "Unknown Type",
            name: "Unknown Name",
            bad_value: false,
            options: Vec::new(),
        }
    }
}

impl ConfigMeta {
    /// Displays the meta's fields in order e.g. Config->nested_config->field
    pub fn tree(&self) -> String {
        let mut tree = "".to_string();

        for (index, element) in self.fields.iter().enumerate() {
            if index != 0 {
                tree = tree + "->";
            }

            tree = tree + element;
        }

        tree
    }

    /// Returns the highest level parent or the root meta of the configuration
    pub fn root(&self) -> ConfigMeta {
        if let Some(ref parent) = self.parent {
            parent.root()
        }
        else {
            self.clone()
        }
    }
}

/// Automatically generates a struct/enums for loading in yaml files.
#[macro_export]
macro_rules! config {
    // Struct
    (
        $(#[$root_meta:meta])*
        struct $root:ident {
            $( $(#[$field_meta:meta])* pub $field:ident: $ty:ty = $name:expr, )*
        }
    ) => {
        #[derive(Clone, Debug)]
        $(#[$root_meta])*
        pub struct $root {
            _meta: $crate::ConfigMeta,
            $(
                $(#[$field_meta])*
                pub $field: $ty,
            )*
        }

        impl $root {
            pub fn to_string(&self) -> String {
                $crate::to_string(&self.to_yaml(&self._meta.path.clone().as_path()))
            }
        }

        impl Default for $root {
            fn default() -> Self {
                $root {
                    _meta: $crate::ConfigMeta::default(),
                    $( $field: $name, )*
                }
            }
        }

        impl $crate::Element for $root {
            fn from_yaml(meta: &$crate::ConfigMeta, config: &$crate::Yaml) -> Result<Self, $crate::ConfigError> {
                use std::collections::HashSet;
                use std::path::PathBuf;

                let mut default = $root::default();

                let mut next_meta = meta.clone();
                next_meta.ty = stringify!($root);

                // Appends top-level
                if meta.fields.len() == 0 {
                    next_meta.fields.push(stringify!($root).to_string());
                }

                default._meta = next_meta.clone();

                // Warns of keys that are in the Yaml Hash, but not in the structure
                let mut unexpected = HashSet::new();

                if let &$crate::Yaml::Hash(ref hash) = config {
                    for (key, _) in hash {
                        if let &$crate::Yaml::String(ref key_str) = key {
                            unexpected.insert(key_str.clone());
                        }
                    }

                    $(
                        unexpected.remove(&stringify!($field).to_string());
                    )*

                    for unexpected_key in unexpected {
                        println!("{}: Unexpected key: {}",
                            default._meta.path.display().to_string(),
                            default._meta.tree() + "->" + &unexpected_key);
                    }
                }

                Ok($root {
                    _meta: default._meta,
                    $(
                        $field: {
                            let key = &config[stringify!($field)];

                            // set up current meta
                            let mut field_meta = next_meta.clone();

                            field_meta.fields.push(stringify!($field).to_string());
                            field_meta.ty = stringify!($ty);
                            field_meta.name = stringify!($field);
                            field_meta.bad_value = key.is_badvalue();
                            field_meta.parent = Some(Box::new(next_meta.clone()));

                            let val = if key.as_str() == Some("extern") { // external file
                                let mut path = PathBuf::from("");

                                for (index, child) in field_meta.fields.iter().enumerate() {
                                    if index != 0 {
                                        path.push(child);
                                    }
                                }

                                <$ty>::from_file_raw(&field_meta, &path.as_path())
                            }
                            else { // current file
                                <$ty>::from_yaml(&field_meta, key)
                            };

                            match val {
                                Ok(found) => found,
                                Err(e) => {
                                    // output error and fall-through the default values
                                    println!("{}", e);

                                    default.$field.set_meta(&field_meta);

                                    default.$field
                                },
                            }
                        },
                    )*
                })
            }

            fn to_yaml(&self, path: &Path) -> $crate::Yaml {
                use std::collections::BTreeMap;

                let mut map: BTreeMap<$crate::Yaml, $crate::Yaml> = BTreeMap::new();

                $(
                    map.insert(
                        $crate::Yaml::String(stringify!($field).to_string()),
                        self.$field.to_yaml(path),
                    );

                    if let Some(field_meta) = self.$field.meta() {
                        if field_meta.path != path {
                            map.insert(
                                $crate::Yaml::String(stringify!($field).to_string()),
                                $crate::Yaml::String("extern".to_string()),
                            );
                        }
                    }
                )*

                $crate::Yaml::Hash(map)
            }

            fn set_meta(&mut self, meta: &$crate::ConfigMeta) {
                self._meta = meta.clone();
            }

            fn meta(&self) -> Option<$crate::ConfigMeta> {
                Some(self._meta.clone())
            }

            fn write_file(&self) -> Result<(), $crate::ConfigError> {
                use std::fs::{DirBuilder, File};
                use std::io::{Write};

                let path = self._meta.path.clone();
                let readable = $crate::to_string(&self.to_yaml(&path.as_path()));

                // Recursively create in the case of new project or deleted folders
                try!(DirBuilder::new().recursive(true).create(&path.parent().unwrap())
                    .map_err(|e| $crate::ConfigError::FileError(path.clone(), e)));

                let mut file = try!(File::create(&path)
                    .map_err(|e| $crate::ConfigError::FileError(path.clone(), e)));
                try!(file.write_all(readable.as_bytes())
                    .map_err(|e| $crate::ConfigError::FileError(path.clone(), e)));

                $(
                    if let Some(ref field_meta) = self.$field.meta() {
                        if field_meta.path != path {
                            if let Err(e) = self.$field.write_file() {
                                return Err(e);
                            }
                        }
                    }
                )*

                Ok(())
            }
        }
    };

    // Enum
    (
        $(#[$root_meta:meta])*
        enum $root:ident {
            $( $field:ident, )*
        }
    ) => {
        #[derive(Clone, Debug, PartialEq)]
        $(#[$root_meta])*
        pub enum $root {
            $($field,)*
        }

        impl $crate::Element for $root {
            fn from_yaml(meta: &$crate::ConfigMeta, config: &$crate::Yaml) -> Result<Self, $crate::ConfigError> {
                let mut next_meta = meta.clone();
                next_meta.options = vec![$( stringify!($field).to_string(), )*];

                if let &$crate::Yaml::String(ref string) = config {
                    let s: &str = string;

                    match s {
                        $(
                            stringify!($field) => Ok($root::$field),
                        )*
                        _ => Err($crate::ConfigError::YamlParse(next_meta.clone()))
                    }
                }
                else {
                    Err($crate::ConfigError::YamlParse(next_meta.clone()))
                }
            }

            fn to_yaml(&self, _: &Path) -> $crate::Yaml {
                match self {
                    $(
                        &$root::$field => $crate::Yaml::String(stringify!($field).to_string()),
                    )*
                }
            }
        }
    };
}