aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorMax Bossing <info@maxbossing.de>2025-07-15 14:41:26 +0200
committerMax Bossing <info@maxbossing.de>2025-07-15 14:41:26 +0200
commit75ed52cb761d90b5e5a96717a4d00ef57003994d (patch)
treed4464a446d3324105274101fe7a72d81b658960e /src/config.rs
init
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..6fd790a
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,44 @@
+use std::env;
+use std::path::PathBuf;
+use serde::Deserialize;
+
+#[derive(Deserialize, Debug)]
+pub struct Config {
+ #[serde(default = "default_dots_dir")]
+ pub dots_dir: PathBuf,
+ #[serde(rename = "dot")]
+ pub dots: Vec<Dot>,
+}
+
+#[derive(Deserialize, Debug)]
+pub struct Dot {
+ #[serde(rename = "src")]
+ pub source: PathBuf,
+ #[serde(rename = "dest")]
+ pub destination: PathBuf,
+}
+
+pub enum ConfigLoadError {
+ IOError(std::io::Error),
+ DeserializationError(toml::de::Error),
+}
+
+impl Config {
+ pub fn load(from: Option<PathBuf>) -> Result<Config, ConfigLoadError> {
+ let path = from.unwrap_or(PathBuf::from("dots.toml"));
+ match std::fs::read_to_string(path).map_err(ConfigLoadError::IOError) {
+ Ok(string) => match toml::from_str(&string) {
+ Ok(config) => Ok(config),
+ Err(err) => Err(ConfigLoadError::DeserializationError(err)),
+ }
+ Err(e) => {
+ Err(e)
+ }
+ }
+ }
+}
+
+fn default_dots_dir() -> PathBuf {
+ env::current_dir().expect("failed to get current dir")
+}
+