mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-08 18:45:13 +00:00
add missing deserialized config errors
This commit is contained in:
parent
c0e002ea1b
commit
1e4dc0cb5a
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{collections::HashMap, env, fs, io, path::PathBuf, result};
|
use std::{collections::HashMap, env, fs, io, path::PathBuf};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use toml;
|
use toml;
|
||||||
|
|
||||||
|
@ -14,15 +14,13 @@ use super::*;
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum DeserializeConfigError {
|
pub enum DeserializeConfigError {
|
||||||
#[error("cannot read config file")]
|
#[error("cannot read config file")]
|
||||||
ReadConfigFile(#[from] io::Error),
|
ReadConfigFile(#[source] io::Error),
|
||||||
#[error("cannot parse config file")]
|
#[error("cannot parse config file")]
|
||||||
ParseConfigFile(#[from] toml::de::Error),
|
ParseConfigFile(#[source] toml::de::Error),
|
||||||
#[error("cannot read environment variable")]
|
#[error("cannot read environment variable {1}")]
|
||||||
ReadEnvVar(#[from] env::VarError),
|
ReadEnvVar(#[source] env::VarError, &'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result<T> = result::Result<T, DeserializeConfigError>;
|
|
||||||
|
|
||||||
/// Represents the user config file.
|
/// Represents the user config file.
|
||||||
#[derive(Debug, Default, Clone, Deserialize)]
|
#[derive(Debug, Default, Clone, Deserialize)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
@ -51,13 +49,13 @@ pub struct DeserializedConfig {
|
||||||
|
|
||||||
impl DeserializedConfig {
|
impl DeserializedConfig {
|
||||||
/// Tries to create a config from an optional path.
|
/// Tries to create a config from an optional path.
|
||||||
pub fn from_opt_path(path: Option<&str>) -> Result<Self> {
|
pub fn from_opt_path(path: Option<&str>) -> Result<Self, DeserializeConfigError> {
|
||||||
trace!(">> parse config from path");
|
trace!(">> parse config from path");
|
||||||
debug!("path: {:?}", path);
|
debug!("path: {:?}", path);
|
||||||
|
|
||||||
let path = path.map(|s| s.into()).unwrap_or(Self::path()?);
|
let path = path.map(|s| s.into()).unwrap_or(Self::path()?);
|
||||||
let content = fs::read_to_string(path)?;
|
let content = fs::read_to_string(path).map_err(DeserializeConfigError::ReadConfigFile)?;
|
||||||
let config = toml::from_str(&content)?;
|
let config = toml::from_str(&content).map_err(DeserializeConfigError::ParseConfigFile)?;
|
||||||
|
|
||||||
trace!("config: {:?}", config);
|
trace!("config: {:?}", config);
|
||||||
trace!("<< parse config from path");
|
trace!("<< parse config from path");
|
||||||
|
@ -66,21 +64,23 @@ impl DeserializedConfig {
|
||||||
|
|
||||||
/// Tries to get the XDG config file path from XDG_CONFIG_HOME
|
/// Tries to get the XDG config file path from XDG_CONFIG_HOME
|
||||||
/// environment variable.
|
/// environment variable.
|
||||||
fn path_from_xdg() -> Result<PathBuf> {
|
fn path_from_xdg() -> Result<PathBuf, DeserializeConfigError> {
|
||||||
let path = env::var("XDG_CONFIG_HOME")?;
|
let path = env::var("XDG_CONFIG_HOME")
|
||||||
|
.map_err(|err| DeserializeConfigError::ReadEnvVar(err, "XDG_CONFIG_HOME"))?;
|
||||||
let path = PathBuf::from(path).join("himalaya").join("config.toml");
|
let path = PathBuf::from(path).join("himalaya").join("config.toml");
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to get the XDG config file path from HOME environment
|
/// Tries to get the XDG config file path from HOME environment
|
||||||
/// variable.
|
/// variable.
|
||||||
fn path_from_xdg_alt() -> Result<PathBuf> {
|
fn path_from_xdg_alt() -> Result<PathBuf, DeserializeConfigError> {
|
||||||
let home_var = if cfg!(target_family = "windows") {
|
let home_var = if cfg!(target_family = "windows") {
|
||||||
"USERPROFILE"
|
"USERPROFILE"
|
||||||
} else {
|
} else {
|
||||||
"HOME"
|
"HOME"
|
||||||
};
|
};
|
||||||
let path = env::var(home_var)?;
|
let path =
|
||||||
|
env::var(home_var).map_err(|err| DeserializeConfigError::ReadEnvVar(err, home_var))?;
|
||||||
let path = PathBuf::from(path)
|
let path = PathBuf::from(path)
|
||||||
.join(".config")
|
.join(".config")
|
||||||
.join("himalaya")
|
.join("himalaya")
|
||||||
|
@ -90,19 +90,20 @@ impl DeserializedConfig {
|
||||||
|
|
||||||
/// Tries to get the .himalayarc config file path from HOME
|
/// Tries to get the .himalayarc config file path from HOME
|
||||||
/// environment variable.
|
/// environment variable.
|
||||||
fn path_from_home() -> Result<PathBuf> {
|
fn path_from_home() -> Result<PathBuf, DeserializeConfigError> {
|
||||||
let home_var = if cfg!(target_family = "windows") {
|
let home_var = if cfg!(target_family = "windows") {
|
||||||
"USERPROFILE"
|
"USERPROFILE"
|
||||||
} else {
|
} else {
|
||||||
"HOME"
|
"HOME"
|
||||||
};
|
};
|
||||||
let path = env::var(home_var)?;
|
let path =
|
||||||
|
env::var(home_var).map_err(|err| DeserializeConfigError::ReadEnvVar(err, home_var))?;
|
||||||
let path = PathBuf::from(path).join(".himalayarc");
|
let path = PathBuf::from(path).join(".himalayarc");
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to get the config file path.
|
/// Tries to get the config file path.
|
||||||
pub fn path() -> Result<PathBuf> {
|
pub fn path() -> Result<PathBuf, DeserializeConfigError> {
|
||||||
Self::path_from_xdg()
|
Self::path_from_xdg()
|
||||||
.or_else(|_| Self::path_from_xdg_alt())
|
.or_else(|_| Self::path_from_xdg_alt())
|
||||||
.or_else(|_| Self::path_from_home())
|
.or_else(|_| Self::path_from_home())
|
||||||
|
|
Loading…
Reference in a new issue