use std::path::PathBuf;
use thiserror::Error;
use super::schema::CONFIG_VERSION;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("I/O error on {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("error parsing {path}: {source}")]
Parse {
path: PathBuf,
#[source]
source: toml::de::Error,
},
#[error(
"config file {path} uses version {version} format.\n\
Run `lx --upgrade-config` to migrate it to version {CONFIG_VERSION}."
)]
NeedsUpgrade { path: PathBuf, version: String },
#[error("personality inheritance cycle: {chain}")]
InheritanceCycle { chain: String },
#[error("personality '{child}' inherits from '{parent}', which does not exist")]
MissingParent { child: String, parent: String },
#[error("{path} is already at version {CONFIG_VERSION}; no upgrade needed")]
AlreadyCurrent { path: PathBuf },
#[error("no config file found to upgrade")]
NothingToUpgrade,
#[error("unknown {kind} '{name}'\nKnown {kind_plural}: {candidates}")]
NotFound {
kind: &'static str,
kind_plural: &'static str,
name: String,
candidates: String,
},
}
pub(super) trait IoResultExt<T> {
fn with_path(self, path: impl Into<PathBuf>) -> Result<T, ConfigError>;
}
impl<T> IoResultExt<T> for std::io::Result<T> {
fn with_path(self, path: impl Into<PathBuf>) -> Result<T, ConfigError> {
self.map_err(|source| ConfigError::Io {
path: path.into(),
source,
})
}
}