From 14ad60a6d77f40fdd0b23e60e11ace8c52449c01 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Wed, 26 Mar 2025 18:52:05 -0600 Subject: rough config setup --- src/data/config.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/data/mod.rs | 3 +++ src/data/pages.rs | 0 src/data/users.rs | 0 4 files changed, 48 insertions(+) create mode 100644 src/data/config.rs create mode 100644 src/data/mod.rs create mode 100644 src/data/pages.rs create mode 100644 src/data/users.rs (limited to 'src/data') diff --git a/src/data/config.rs b/src/data/config.rs new file mode 100644 index 0000000..bce5c94 --- /dev/null +++ b/src/data/config.rs @@ -0,0 +1,45 @@ +use std::fs; +use std::path::{Path, PathBuf}; + +use serde::Deserialize; + +#[derive(Deserialize)] +struct ConfigFile { + site_title: Option, + data_dir: Option, + external_root: Option, + listen_port: Option, + footer_copyright: Option, +} + +pub struct Config { + site_title: String, + data_dir: PathBuf, + external_root: String, + listen_port: u16, + footer_copyright: Option +} + +impl Config { + pub fn read_from_file>(path: P) -> Result { + let config_contents = fs::read_to_string(&path) + .map_err(|_| "Unable to open config file".to_string())?; + + let file : ConfigFile = toml::from_str(&config_contents) + .map_err(|err| err.to_string())?; + + let port = file.listen_port.unwrap_or(3000); + + Ok(Self { + site_title: file.site_title + .unwrap_or("Untitled StormScribe Site".to_string()), + data_dir: file.data_dir.unwrap_or(path.as_ref() + .canonicalize().map_err(|_| "Cannot resolve config file location".to_string())? + .parent().ok_or("Cannot resolve data dir".to_string())?.to_path_buf()), + external_root: file.external_root + .unwrap_or(format!("http://localhost:{port}/")), + listen_port: port, + footer_copyright: file.footer_copyright, + }) + } +} diff --git a/src/data/mod.rs b/src/data/mod.rs new file mode 100644 index 0000000..bfe8b48 --- /dev/null +++ b/src/data/mod.rs @@ -0,0 +1,3 @@ +pub mod config; + +struct DirState diff --git a/src/data/pages.rs b/src/data/pages.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/data/users.rs b/src/data/users.rs new file mode 100644 index 0000000..e69de29 -- cgit 1.4.1