use std::path::Path;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("cannot evaluate: {0}")]
Calculator(String),
#[error("invalid variable name: '{0}'")]
InvalidVariableName(String),
#[error("no previous answer to save")]
NoPreviousAnswer,
#[error("error in config {path}: {message}")]
Config { path: String, message: String },
#[error("error in state {path}: {message}")]
State { path: String, message: String },
#[error("{context}: {source}")]
Io {
context: String,
source: std::io::Error,
},
}
impl Error {
pub fn config(path: &Path, message: impl Into<String>) -> Self {
Error::Config {
path: path.display().to_string(),
message: message.into(),
}
}
pub fn state(path: &Path, message: impl Into<String>) -> Self {
Error::State {
path: path.display().to_string(),
message: message.into(),
}
}
pub fn io(context: impl Into<String>, source: std::io::Error) -> Self {
Error::Io {
context: context.into(),
source,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;