summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 9ede6434b069c804946a3fa807d5e92b68c2f243 (plain)
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<PathBuf>,
	passwords: HashMap<String, (Option<String>, String)>,
	passphrases: HashMap<PathBuf, String>,
}

impl Config {
	pub fn load() -> Result<Config, ConfyError> {
		confy::load("git-autosave", "git-autosaved")
	}

	pub fn save(&self) -> Result<(), ConfyError> {
		confy::store("git-autosave", "git-autosaved", self)
	}

	pub fn repositories(&self) -> &HashSet<PathBuf> {
		&self.repositories
	}

	pub fn repositories_mut(&mut self) -> &mut HashSet<PathBuf> {
		&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)
	}
}