umya_spreadsheet/structs/
error.rs

1use crate::from_err;
2use std::{fmt, str::FromStr};
3
4// https://msdn.microsoft.com/en-us/library/office/ff839168.aspx
5/// An enum to represent all different errors that can appear as
6/// a value in a worksheet cell
7#[derive(Debug, Clone, PartialEq, PartialOrd)]
8pub enum CellErrorType {
9    /// Division by 0 error
10    Div0,
11    /// Invalid name error
12    Name,
13    /// Unavailable value error
14    NA,
15    /// Number error
16    Num,
17    /// Value error
18    Value,
19    /// Invalid cell reference error
20    Ref,
21    /// Null value error
22    Null,
23    /// Getting data
24    Data,
25}
26
27impl fmt::Display for CellErrorType {
28    #[inline]
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
30        match *self {
31            CellErrorType::Div0 => write!(f, "#DIV/0!"),
32            CellErrorType::NA => write!(f, "#N/A"),
33            CellErrorType::Name => write!(f, "#NAME?"),
34            CellErrorType::Null => write!(f, "#NULL!"),
35            CellErrorType::Num => write!(f, "#NUM!"),
36            CellErrorType::Ref => write!(f, "#REF!"),
37            CellErrorType::Value => write!(f, "#VALUE!"),
38            CellErrorType::Data => write!(f, "#DATA!"),
39        }
40    }
41}
42impl FromStr for CellErrorType {
43    type Err = XlsxError;
44
45    #[inline]
46    fn from_str(s: &str) -> Result<Self, Self::Err> {
47        match s {
48            "#DIV/0!" => Ok(CellErrorType::Div0),
49            "#N/A" => Ok(CellErrorType::NA),
50            "#NAME?" => Ok(CellErrorType::Name),
51            "#NULL!" => Ok(CellErrorType::Null),
52            "#NUM!" => Ok(CellErrorType::Num),
53            "#REF!" => Ok(CellErrorType::Ref),
54            "#VALUE!" => Ok(CellErrorType::Value),
55            "#DATA!" => Ok(CellErrorType::Data),
56            _ => Err(XlsxError::CellError(s.into())),
57        }
58    }
59}
60
61#[derive(Debug)]
62pub enum XlsxError {
63    /// IO error
64    Io(std::io::Error),
65    /// Xml error
66    Xml(quick_xml::Error),
67    /// Zip error
68    Zip(zip::result::ZipError),
69
70    Uft8(std::string::FromUtf8Error),
71    /// Cell error
72    CellError(String),
73}
74
75from_err!(std::io::Error, XlsxError, Io);
76from_err!(quick_xml::Error, XlsxError, Xml);
77from_err!(zip::result::ZipError, XlsxError, Zip);
78from_err!(std::string::FromUtf8Error, XlsxError, Uft8);
79
80impl fmt::Display for XlsxError {
81    #[inline]
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        use self::XlsxError::*;
84        match self {
85            Io(i) => write!(f, "IoError: {}", i),
86            Xml(s) => write!(f, "XmlError: {}", s),
87            Zip(s) => write!(f, "ZipError: {}", s),
88            Uft8(s) => write!(f, "Uft8Error: {}", s),
89            CellError(e) => write!(f, "Unsupported cell error value '{e}'"),
90        }
91    }
92}
93impl std::error::Error for XlsxError {}