rair_env/
metadata.rs

1/*
2 * metadata.rs: Data types handling handling for renv.
3 * Copyright (C) 2019  Oddcoder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17use super::environment::Environment;
18
19pub type StrFn<T> = fn(&str, &str, &Environment<T>, &mut T) -> bool;
20pub type U64Fn<T> = fn(&str, u64, &Environment<T>, &mut T) -> bool;
21pub type I64Fn<T> = fn(&str, i64, &Environment<T>, &mut T) -> bool;
22pub type BoolFn<T> = fn(&str, bool, &Environment<T>, &mut T) -> bool;
23pub type ColorFn<T> = fn(&str, (u8, u8, u8), &Environment<T>, &mut T) -> bool;
24
25pub(crate) struct EnvStr<T> {
26    pub(crate) data: String,
27    pub(crate) default: String,
28    pub(crate) help: String,
29    pub(crate) cb: Option<StrFn<T>>,
30}
31
32pub(crate) struct EnvU64<T> {
33    pub(crate) data: u64,
34    pub(crate) default: u64,
35    pub(crate) help: String,
36    pub(crate) cb: Option<U64Fn<T>>,
37}
38
39pub(crate) struct EnvI64<T> {
40    pub(crate) data: i64,
41    pub(crate) default: i64,
42    pub(crate) help: String,
43    pub(crate) cb: Option<I64Fn<T>>,
44}
45
46pub(crate) struct EnvBool<T> {
47    pub(crate) data: bool,
48    pub(crate) default: bool,
49    pub(crate) help: String,
50    pub(crate) cb: Option<BoolFn<T>>,
51}
52
53pub(crate) struct EnvColor<T> {
54    pub(crate) data: (u8, u8, u8),
55    pub(crate) default: (u8, u8, u8),
56    pub(crate) help: String,
57    pub(crate) cb: Option<ColorFn<T>>,
58}
59
60pub(crate) enum EnvMetaData<T> {
61    Str(EnvStr<T>),
62    U64(EnvU64<T>),
63    I64(EnvI64<T>),
64    Bool(EnvBool<T>),
65    Color(EnvColor<T>),
66}
67
68impl<T> EnvMetaData<T> {
69    pub(crate) fn as_str(&self) -> Option<&EnvStr<T>> {
70        if let EnvMetaData::Str(s) = self {
71            return Some(s);
72        }
73        return None;
74    }
75    pub(crate) fn as_u64(&self) -> Option<&EnvU64<T>> {
76        if let EnvMetaData::U64(u) = self {
77            return Some(u);
78        }
79        return None;
80    }
81    pub(crate) fn as_i64(&self) -> Option<&EnvI64<T>> {
82        if let EnvMetaData::I64(i) = self {
83            return Some(i);
84        }
85        return None;
86    }
87    pub(crate) fn as_bool(&self) -> Option<&EnvBool<T>> {
88        if let EnvMetaData::Bool(b) = self {
89            return Some(b);
90        }
91        return None;
92    }
93    pub(crate) fn as_color(&self) -> Option<&EnvColor<T>> {
94        if let EnvMetaData::Color(c) = self {
95            return Some(c);
96        }
97        return None;
98    }
99    pub(crate) fn mut_str(&mut self) -> Option<&mut EnvStr<T>> {
100        if let EnvMetaData::Str(s) = self {
101            return Some(s);
102        }
103        return None;
104    }
105    pub(crate) fn mut_u64(&mut self) -> Option<&mut EnvU64<T>> {
106        if let EnvMetaData::U64(u) = self {
107            return Some(u);
108        }
109        return None;
110    }
111    pub(crate) fn mut_i64(&mut self) -> Option<&mut EnvI64<T>> {
112        if let EnvMetaData::I64(i) = self {
113            return Some(i);
114        }
115        return None;
116    }
117    pub(crate) fn mut_bool(&mut self) -> Option<&mut EnvBool<T>> {
118        if let EnvMetaData::Bool(b) = self {
119            return Some(b);
120        }
121        return None;
122    }
123    pub(crate) fn mut_color(&mut self) -> Option<&mut EnvColor<T>> {
124        if let EnvMetaData::Color(c) = self {
125            return Some(c);
126        }
127        return None;
128    }
129}