vnv/parsing/
config.rs

1use std::fs;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Options {
8    pub src: String,
9    pub cloak: bool,
10    pub build: Build,
11}
12
13#[derive(Debug, Serialize, Deserialize, Clone)]
14pub struct Build {
15    pub output: String,
16    pub minify: bool
17}
18
19impl Options {
20    /// Returns the default values for config
21    pub fn new() -> Self {
22        Options {
23            src: String::from(".vnv"),
24            cloak: false,
25            build: Build {
26                output: String::from(".env"),
27                minify: false
28            }
29        }
30    }
31}
32
33pub fn parse(path: &str) -> Options {
34    let defaults: Options = Options::new();
35
36    let content = fs::read(path);
37
38    if let Ok(content) = content {
39        let json = String::from_utf8(content).unwrap();
40
41        let object: Result<Value, _> = serde_json::from_str(&json);
42
43        if let Ok(object) = object {
44            // map the values to the options object
45            return Options {
46                src: object["src"].as_str().unwrap_or(&defaults.src).to_string(),
47                cloak: object["cloak"].to_string().parse().unwrap_or(false),
48                build: Build {
49                    output: object["build"]["output"].as_str().unwrap_or(&defaults.build.output).to_string(),
50                    minify: object["build"]["minify"].to_string().parse().unwrap_or(false)
51                }
52            };
53        } else {
54            return defaults;
55        }
56    } else {
57
58
59        return defaults;
60    }
61}