use std::fmt;
#[derive(Debug)]
pub enum Error {
PlaceholderCollision { path: String, placeholder: String },
RootKeyCollision { placeholder: String },
IntegerOutOfRange { path: String, value: u64 },
FloatNotRepresentable { path: String, kind: &'static str },
Toml(toml::de::Error),
Io(std::io::Error),
Fmt(fmt::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::PlaceholderCollision { path, placeholder } => {
write!(
f,
"string at {path:?} equals the null placeholder {placeholder:?}; \
pick a different placeholder via TomlJsonOptions"
)
}
Error::RootKeyCollision { placeholder } => {
write!(
f,
"input has a top-level key {placeholder:?} which collides with \
the configured root placeholder; pick a different name via \
TomlJsonOptions::root_placeholder"
)
}
Error::IntegerOutOfRange { path, value } => {
write!(
f,
"integer {value} at {path:?} exceeds TOML's signed 64-bit range \
(i64::MAX = 9223372036854775807)"
)
}
Error::FloatNotRepresentable { path, kind } => {
write!(
f,
"TOML float {kind} at {path:?} cannot be represented in JSON; \
omit the value or use a sentinel string instead"
)
}
Error::Toml(e) => write!(f, "toml parse error: {e}"),
Error::Io(e) => write!(f, "io error: {e}"),
Error::Fmt(e) => write!(f, "format error: {e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Toml(e) => Some(e),
Error::Io(e) => Some(e),
Error::Fmt(e) => Some(e),
_ => None,
}
}
}
impl From<toml::de::Error> for Error {
fn from(e: toml::de::Error) -> Self {
Error::Toml(e)
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Self {
Error::Fmt(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;