use std::fmt;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Invalid cell coordinates ({0})")]
CellCoordinates(String),
#[error("IllegalCharacterError: data contains characters that cannot be stored in Excel: {0}")]
IllegalCharacter(char),
#[error("ColumnStringIndexException: {0}")]
ColumnStringIndex(String),
#[error("DataTypeException: {0}")]
DataType(String),
#[error("NamedRangeException: {0}")]
NamedRange(String),
#[error("SheetTitleException: {0}")]
SheetTitle(String),
#[error("InsufficientCoordinatesException: {0}")]
InsufficientCoordinates(String),
#[error("OpenModeError: {0}")]
OpenMode(String),
#[error("InvalidFileException: {0}")]
InvalidFile(String),
#[error("ReadOnlyWorkbookException: {0}")]
ReadOnlyWorkbook(String),
#[error("MissingNumberFormat: {0}")]
MissingNumberFormat(String),
#[error("WorkbookAlreadySaved: {0}")]
WorkbookAlreadySaved(String),
#[error("ValueError: {0}")]
Value(String),
#[error("TypeError: {0}")]
Type(String),
#[error("KeyError: {0}")]
Key(String),
#[error("AttributeError: {0}")]
Attribute(String),
#[error("BadZipFile: {0}")]
BadZipFile(String),
#[error("XMLParseError: {0}")]
Xml(String),
#[error("IOError: {0}")]
Io(String),
#[error("NotImplementedError: {0}")]
NotImplemented(String),
}
impl Error {
pub fn value(msg: impl Into<String>) -> Self {
Error::Value(msg.into())
}
pub fn type_error(msg: impl Into<String>) -> Self {
Error::Type(msg.into())
}
pub fn key(msg: impl Into<String>) -> Self {
Error::Key(msg.into())
}
pub fn attribute(msg: impl Into<String>) -> Self {
Error::Attribute(msg.into())
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Error::Io(value.to_string())
}
}
impl From<quick_xml::Error> for Error {
fn from(value: quick_xml::Error) -> Self {
Error::Xml(value.to_string())
}
}
impl From<quick_xml::events::attributes::AttrError> for Error {
fn from(value: quick_xml::events::attributes::AttrError) -> Self {
Error::Xml(value.to_string())
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub trait OrDefault<T: Default> {
fn or_default(self) -> T;
}
impl<T: Default> OrDefault<T> for Result<T> {
fn or_default(self) -> T {
self.unwrap_or_default()
}
}
pub fn safe_string<T: fmt::Display>(value: T) -> String {
value.to_string()
}