diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-01-13 23:19:22 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-06-13 12:16:39 +0200 |
| commit | eee62e0892b1247ce321cd30747ab90d58f49732 (patch) | |
| tree | 9145e96680d3fac42dc67387c460b307ce4156af /mumlib/src/config.rs | |
| parent | 54153914d22180777b25a1d4f5089ed2ae2839df (diff) | |
| download | mum-eee62e0892b1247ce321cd30747ab90d58f49732.tar.gz | |
document mumlib
Diffstat (limited to 'mumlib/src/config.rs')
| -rw-r--r-- | mumlib/src/config.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 3edef37..b5bcdb1 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -23,6 +23,8 @@ struct TOMLConfig { servers: Option<Array>, } +/// Our representation of the mumdrc config file. +// Deserialized via [TOMLConfig]. #[derive(Clone, Debug, Default)] pub struct Config { pub audio: AudioConfig, @@ -34,6 +36,15 @@ pub struct Config { } impl Config { + /// Writes this config to the specified path. + /// + /// Pass create = true if you want the file to be created if it doesn't already exist. + /// + /// # Errors + /// + /// - [ConfigError::WontCreateFile] if the file doesn't exist and create = false was passed. + /// - Any [ConfigError::TOMLErrorSer] encountered when serializing the config. + /// - Any [ConfigError::IOError] encountered when writing the file. pub fn write(&self, path: &Path, create: bool) -> Result<(), ConfigError> { // Possible race here. It's fine since it shows when: // 1) the file doesn't exist when checked and is then created @@ -90,6 +101,10 @@ impl ServerConfig { } } +/// Finds the default path of the configuration file. +/// +/// The user config dir is looked for first (cross-platform friendly) and +/// `/etc/mumdrc` is used as a fallback. pub fn default_cfg_path() -> PathBuf { match dirs::config_dir() { Some(mut p) => { @@ -143,6 +158,14 @@ impl From<Config> for TOMLConfig { } } +/// Reads the config at the specified path. +/// +/// If the file isn't found, returns a default config. +/// +/// # Errors +/// +/// - Any [ConfigError::TOMLErrorDe] encountered when deserializing the config. +/// - Any [ConfigError::IOError] encountered when reading the file. pub fn read_cfg(path: &Path) -> Result<Config, ConfigError> { match fs::read_to_string(path) { Ok(s) => { |
