From 96f63ec38fcdc8194a52c2b34b32f6e88ae64c34 Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 30 Mar 2026 21:28:48 -0400 Subject: Refactor magic strings --- src/config.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/config.rs (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..9ede643 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,42 @@ +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; + +use confy::ConfyError; +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Config { + repositories: HashSet, + passwords: HashMap, String)>, + passphrases: HashMap, +} + +impl Config { + pub fn load() -> Result { + confy::load("git-autosave", "git-autosaved") + } + + pub fn save(&self) -> Result<(), ConfyError> { + confy::store("git-autosave", "git-autosaved", self) + } + + pub fn repositories(&self) -> &HashSet { + &self.repositories + } + + pub fn repositories_mut(&mut self) -> &mut HashSet { + &mut self.repositories + } + + pub fn username_for_url(&self, url: &str) -> Option<&String> { + self.passwords.get(url)?.0.as_ref() + } + + pub fn password_for_url(&self, url: &str) -> Option<&String> { + Some(&self.passwords.get(url)?.1) + } + + pub fn passphrase_for_key(&self, key: &Path) -> Option<&String> { + self.passphrases.get(key) + } +} -- cgit v1.2.3