use std::path::PathBuf;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, ConferError>;
#[derive(Debug, Error)]
pub enum ConferError {
#[error("I/O error (path: {path:?}): {source}")]
Io {
path: Option<PathBuf>,
#[source]
source: std::io::Error,
},
#[error("failed to parse TOML: {0}")]
Parse(#[from] toml::de::Error),
#[error("failed to serialize TOML: {0}")]
Serialize(#[from] toml::ser::Error),
#[error("missing key {section}.{key}")]
MissingKey { section: String, key: String },
#[error("expected {expected} at {section}.{key} but found {found}")]
TypeMismatch {
section: String,
key: String,
expected: &'static str,
found: &'static str,
},
#[error("invalid value at {section}.{key}: {message}")]
ValueParse {
section: String,
key: String,
message: String,
},
}
impl ConferError {
pub(crate) fn io_error(path: Option<PathBuf>, source: std::io::Error) -> Self {
Self::Io { path, source }
}
pub fn missing_key(section: impl Into<String>, key: impl Into<String>) -> Self {
Self::MissingKey {
section: section.into(),
key: key.into(),
}
}
pub fn type_mismatch(
section: impl Into<String>,
key: impl Into<String>,
expected: &'static str,
found: &'static str,
) -> Self {
Self::TypeMismatch {
section: section.into(),
key: key.into(),
expected,
found,
}
}
pub fn value_parse(
section: impl Into<String>,
key: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::value_parse_owned(section, key, message.into())
}
pub fn value_parse_owned(
section: impl Into<String>,
key: impl Into<String>,
message: String,
) -> Self {
Self::ValueParse {
section: section.into(),
key: key.into(),
message,
}
}
}
impl From<std::io::Error> for ConferError {
fn from(source: std::io::Error) -> Self {
Self::Io {
path: None,
source,
}
}
}