use crate::coordinate::{MAX_COL, MAX_ROW};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("invalid cell reference: {0:?}")]
InvalidCellRef(String),
#[error("invalid range: {0:?}")]
InvalidRange(String),
#[error("row {0} is outside the range 1..={MAX_ROW}")]
RowOutOfRange(u64),
#[error("column {0} is outside the range 1..={MAX_COL}")]
ColOutOfRange(u64),
#[error("date is outside the range Excel can represent")]
DateOutOfRange,
#[error("a sheet named {0:?} already exists")]
DuplicateSheetName(String),
#[error("no sheet at index {0}")]
SheetIndexOutOfRange(usize),
#[error("invalid sheet name {0:?}")]
InvalidSheetName(String),
#[error("invalid formula: {0}")]
InvalidFormula(String),
#[error("xlsx: {0}")]
Xlsx(String),
#[error("xlsb: {0}")]
Xlsb(String),
#[error("csv: {0}")]
Csv(String),
#[error("html: {0}")]
Html(String),
#[error("ods: {0}")]
Ods(String),
#[error("xls: {0}")]
Xls(String),
#[error("slk: {0}")]
Slk(String),
#[error("gnumeric: {0}")]
Gnumeric(String),
#[error("io: {0}")]
Io(String),
#[error("xml: {0}")]
Xml2003(String),
}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CellError {
Null,
Div0,
Value,
Ref,
Name,
Num,
Na,
Calc,
}
impl CellError {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Null => "#NULL!",
Self::Div0 => "#DIV/0!",
Self::Value => "#VALUE!",
Self::Ref => "#REF!",
Self::Name => "#NAME?",
Self::Num => "#NUM!",
Self::Na => "#N/A",
Self::Calc => "#CALC!",
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
Some(match s {
"#NULL!" => Self::Null,
"#DIV/0!" => Self::Div0,
"#VALUE!" => Self::Value,
"#REF!" => Self::Ref,
"#NAME?" => Self::Name,
"#NUM!" => Self::Num,
"#N/A" => Self::Na,
"#CALC!" => Self::Calc,
_ => return None,
})
}
}
impl core::fmt::Display for CellError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::CellError;
#[test]
fn error_codes_roundtrip() {
for e in [
CellError::Null,
CellError::Div0,
CellError::Value,
CellError::Ref,
CellError::Name,
CellError::Num,
CellError::Na,
CellError::Calc,
] {
assert_eq!(CellError::parse(e.as_str()), Some(e));
}
assert_eq!(CellError::parse("#OOPS!"), None);
assert_eq!(
CellError::parse("#n/a"),
None,
"Excel error literals are upper case"
);
}
}