about summary refs log tree commit diff
path: root/src/util/static_cfg.rs
blob: b20456fb3bd4cc5789d2b2118f3b4c2bae194da0 (plain) (blame)
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
use include_dir::Dir;
use serde::de;

macro_rules! __static_cfg_include {
    (toml_file, $filename:expr) => {
        include_str!($filename)
    };
    (toml_dir, $filename:expr) => {
        include_dir!($filename)
    };
    (json_file, $filename:expr) => {
        include_str!($filename)
    };
    (json_dir, $filename:expr) => {
        include_dir!($filename)
    };
    (cfg_dir, $filename:expr) => {
        include_dir!($filename)
    };
}

macro_rules! __static_cfg_type {
    (toml_file) => (&'static str);
    (json_file) => (&'static str);
    (toml_dir) => (include_dir::Dir<'static>);
    (json_dir) => (include_dir::Dir<'static>);
    (cfg_dir) => (include_dir::Dir<'static>);
}

macro_rules! __static_cfg_parse {
    (toml_file, $e:expr) => {
        toml::from_str($e).unwrap()
    };

    (json_file, $e:expr) => {
        serde_json::from_str($e).unwrap()
    };

    (toml_dir, $e:expr) => {
        crate::util::static_cfg::parse_toml_dir($e)
    };

    (json_dir, $e:expr) => {
        crate::util::static_cfg::parse_json_dir($e)
    };

    (cfg_dir, $e:expr) => {
        crate::util::static_cfg::parse_cfg_dir($e);
    };
}

macro_rules! __static_cfg_inner {
    ($(#[$attr:meta])* ($($vis:tt)*) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => {
        // static RAW: &'static str = __static_cfg_include!($kind, $filename);
        static RAW: __static_cfg_type!($kind) = __static_cfg_include!($kind, $filename);
        lazy_static! {
            $(#[$attr])* static ref $N: $T = __static_cfg_parse!($kind, RAW);
        }

        static_cfg!($($t)*);
    }
}

#[macro_export]
macro_rules! static_cfg {
    ($(#[$attr:meta])* static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => {
        __static_cfg_inner!($(#[$attr])* () static ref $N : $T = $kind($filename); $($t)*);
    };

    ($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => {
        __static_cfg_inner!($(#[$attr])* (pub) static ref $N : $T = $kind($filename); $($t)*);
    };

    ($(#[$attr:meta])* pub ($($vis:tt)+) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => {
        __static_cfg_inner!($(#[$attr])* (pub ($($vis)+)) static ref $N : $T = $kind($filename); $($t)*);
    };

    () => ()
}

pub fn parse_cfg_dir<'a, T>(d: Dir<'a>) -> Vec<T>
where
    T: de::Deserialize<'a>,
{
    d.files()
        .iter()
        .filter_map(|f| {
            let path = f.path();
            let contents = f.contents_utf8().unwrap();
            match path.extension().and_then(|e| e.to_str()) {
                Some("toml") => {
                    Some(toml::from_str(contents).unwrap_or_else(|e| {
                        panic!(
                            "Error parsing TOML file {}: {}",
                            path.display(),
                            e
                        )
                    }))
                }
                Some("json") => {
                    Some(serde_json::from_str(contents).unwrap_or_else(|e| {
                        panic!(
                            "Error parsing JSON file {}: {}",
                            path.display(),
                            e
                        )
                    }))
                }
                // > YAML currently does not support zero-copy deserialization
                // Some("yaml") => {
                //     Some(serde_yaml::from_str(contents).unwrap_or_else(|e| {
                //         panic!(
                //             "Error parsing YAML file {}: {}",
                //             path.display(),
                //             e
                //         )
                //     }))
                // }
                _ => None,
            }
        })
        .collect()
}

pub fn parse_toml_dir<'a, T>(d: Dir<'a>) -> Vec<T>
where
    T: de::Deserialize<'a>,
{
    d.files()
        .iter()
        .map(|f| {
            toml::from_str(f.contents_utf8().unwrap()).unwrap_or_else(|e| {
                panic!("Error parsing TOML file {}: {}", f.path, e)
            })
        })
        .collect()
}

pub fn parse_json_dir<'a, T>(d: Dir<'a>) -> Vec<T>
where
    T: de::Deserialize<'a>,
{
    d.files()
        .iter()
        .map(|f| serde_json::from_str(f.contents_utf8().unwrap()).unwrap())
        .collect()
}