aboutsummaryrefslogtreecommitdiff
path: root/backend/src/config.rs
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2024-07-24 23:40:11 -0400
committerGravatar Chris Xiong <chirs241097@gmail.com> 2024-07-24 23:40:11 -0400
commitb736068ee7b82e05c2ede8bc48ace7ffa4709e29 (patch)
treeeaa3cce9fdd9973043a7a55613584f90f6598a20 /backend/src/config.rs
downloadnotekins-b736068ee7b82e05c2ede8bc48ace7ffa4709e29.tar.xz
Initial commit.
Diffstat (limited to 'backend/src/config.rs')
-rw-r--r--backend/src/config.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/backend/src/config.rs b/backend/src/config.rs
new file mode 100644
index 0000000..a1085ba
--- /dev/null
+++ b/backend/src/config.rs
@@ -0,0 +1,82 @@
+use std::collections::hash_map::HashMap;
+use std::fs::File;
+use std::io::Read;
+use std::fmt::{Display, Formatter, Error};
+
+#[derive(Clone)]
+pub enum ConfigValue {
+ Str(String),
+ UInt(usize)
+}
+
+impl Display for ConfigValue {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ match self {
+ ConfigValue::Str(s) => write!(f, "{}", s),
+ ConfigValue::UInt(u) => write!(f, "{}", u)
+ }
+ }
+}
+
+struct ConfigItem {
+ default_value: ConfigValue,
+ parse_func: Box<dyn Fn(String) -> Option<ConfigValue>>,
+}
+
+pub struct Config {
+ v: HashMap<String, ConfigValue>,
+}
+
+fn stripped_str(s: String) -> Option<ConfigValue> {
+ Some(ConfigValue::Str(String::from(s.trim())))
+}
+
+fn parse_usize(s: String) -> Option<ConfigValue> {
+ if let Ok(r) = usize::from_str_radix(&s, 10) {
+ Some(ConfigValue::UInt(r))
+ } else { None }
+}
+
+pub fn split_at_first<'a, 'b>(s: &'a str, sp: &'b str) -> (&'a str, &'a str) {
+ if let Some(p) = s.find(sp) {
+ (&s[.. p], &s[p + sp.len()..])
+ } else { (s, "") }
+}
+
+impl Config {
+ fn items() -> HashMap<&'static str, ConfigItem> {
+ HashMap::from([
+ ("SERVED_DATA_ROOT", ConfigItem{default_value: ConfigValue::Str(String::from("")), parse_func: Box::new(stripped_str)}),
+ ("POSTS_PER_PAGE", ConfigItem{default_value: ConfigValue::UInt(20), parse_func: Box::new(parse_usize)}),
+ ("DISPLAY_TIMEZONE", ConfigItem{default_value: ConfigValue::Str(String::from("UTC")), parse_func: Box::new(stripped_str)}),
+ ("VERSION_STRING", ConfigItem{default_value: ConfigValue::Str(String::from("1.0")), parse_func: Box::new(stripped_str)}),
+ ])
+ }
+ pub fn parse_config(filename: &str) -> Config {
+ let mut v = HashMap::from_iter(
+ Config::items().iter()
+ .map(|(k, v)| {(String::from(*k), v.default_value.clone())})
+ );
+ let mut f = File::open(filename).unwrap();
+ let mut s = String::new();
+ if let Ok(_) = f.read_to_string(&mut s) {
+ for l in s.split("\n") {
+ let (sname, svalue) = split_at_first(l, "=");
+ if let Some(ConfigItem{default_value: _, parse_func}) = Config::items().get(sname) {
+ if let Some(value) = parse_func(String::from(svalue)) {
+ v.insert(String::from(sname), value);
+ }
+ }
+ }
+ }
+ Config { v }
+ }
+ pub fn get(&self, key: &str) -> ConfigValue {
+ self.v.get(&String::from(key)).unwrap().clone()
+ }
+ pub fn get_str(&self, key: &str) -> String {
+ if let ConfigValue::Str(s) = self.get(key) {
+ s
+ } else { String::from("") }
+ }
+}