2021-12-16 08:13:59 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::env;
|
2021-12-17 00:48:47 +00:00
|
|
|
use std::error::Error;
|
2021-12-16 08:13:59 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-12-17 00:48:47 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-12-16 08:13:59 +00:00
|
|
|
/// The internal representation of the configuration file.
|
|
|
|
pub struct Config {
|
2021-12-17 01:50:30 +00:00
|
|
|
#[serde(skip)] // Skip serializing the configration file location
|
|
|
|
config_location: String,
|
2021-12-16 08:13:59 +00:00
|
|
|
pub source_dir: String,
|
2021-12-17 01:50:30 +00:00
|
|
|
pub repositories: Vec<Repository>,
|
2021-12-17 00:48:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:12:30 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2021-12-17 01:50:30 +00:00
|
|
|
/// The internal representation of a single repository
|
|
|
|
pub struct Repository {
|
2021-12-17 02:43:50 +00:00
|
|
|
pub name: String,
|
|
|
|
pub url: String,
|
2021-12-22 01:35:40 +00:00
|
|
|
pub category: Option<String>,
|
2021-12-16 08:13:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 01:50:30 +00:00
|
|
|
impl Repository {
|
2021-12-22 01:35:40 +00:00
|
|
|
pub fn new(name: String, url: String, category: Option<String>) -> Repository {
|
2021-12-17 01:50:30 +00:00
|
|
|
Repository {
|
2021-12-19 04:13:02 +00:00
|
|
|
name,
|
2021-12-17 01:50:30 +00:00
|
|
|
url,
|
2021-12-22 01:35:40 +00:00
|
|
|
category,
|
2021-12-17 01:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:13:59 +00:00
|
|
|
impl Config {
|
|
|
|
/// Create new Config struct with default values
|
|
|
|
///
|
|
|
|
/// The `source_dir` field defaults to `$HOME/sources`
|
|
|
|
pub fn new() -> Config {
|
|
|
|
Config {
|
|
|
|
source_dir: format!("{}/sources", env::var("HOME").unwrap()),
|
2021-12-17 00:48:47 +00:00
|
|
|
repositories: vec![],
|
2021-12-17 01:50:30 +00:00
|
|
|
config_location: String::new(),
|
2021-12-16 08:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read TOML file and load values into a Config struct
|
|
|
|
///
|
2021-12-17 01:50:30 +00:00
|
|
|
/// If the filename doesn't exist, `read_config()` will write the current struct to the given
|
2021-12-16 08:13:59 +00:00
|
|
|
/// config file.
|
|
|
|
pub fn read_config(&mut self, filename: &str) -> Result<(), Box<dyn Error>> {
|
|
|
|
// Load raw yaml file. If the file doesn't exist, create it
|
|
|
|
if std::path::Path::new(filename).exists() {
|
|
|
|
let raw = fs::read_to_string(filename)?;
|
|
|
|
// Convert string to our Config struct
|
|
|
|
let toml: Config = toml::from_str(&raw)?;
|
|
|
|
*self = toml;
|
|
|
|
} else {
|
|
|
|
let config: String = toml::to_string(&self)?;
|
|
|
|
// Get the path to the config file
|
2021-12-17 00:48:47 +00:00
|
|
|
let path = match Path::new(filename).parent() {
|
2021-12-16 08:13:59 +00:00
|
|
|
Some(x) => x,
|
2021-12-17 00:48:47 +00:00
|
|
|
_ => Path::new(filename),
|
2021-12-16 08:13:59 +00:00
|
|
|
};
|
|
|
|
// Make sure the path exists
|
|
|
|
fs::create_dir_all(path).unwrap();
|
|
|
|
// Write default Config struct to file
|
|
|
|
fs::write(filename, config).expect("Couldn't write file");
|
|
|
|
}
|
2021-12-17 01:50:30 +00:00
|
|
|
self.config_location = filename.to_string();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save the current configuration to a file.
|
|
|
|
///
|
|
|
|
/// This has to be called after `read_config()`.
|
|
|
|
pub fn save_config(&self) -> Result<(), Box<dyn Error>> {
|
|
|
|
fs::write(&self.config_location, toml::to_string(&self)?)?;
|
2021-12-16 08:13:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a string of the current loaded config.
|
|
|
|
///
|
|
|
|
/// Useful for dumping the config
|
|
|
|
pub fn to_string(&self) -> Result<String, toml::ser::Error> {
|
|
|
|
toml::to_string_pretty(self)
|
|
|
|
}
|
|
|
|
}
|