use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GraphError {
EmptyFormula,
UnterminatedString {
position: usize,
},
UnterminatedQuote {
position: usize,
},
InvalidReference {
position: usize,
text: String,
},
ColumnOverflow {
position: usize,
text: String,
},
RowOverflow {
position: usize,
text: String,
},
RangeTooLarge {
start: (u32, u32),
end: (u32, u32),
cells: u64,
},
}
impl fmt::Display for GraphError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GraphError::EmptyFormula => write!(f, "formula is empty"),
GraphError::UnterminatedString { position } => {
write!(f, "unterminated string literal at byte {position}")
}
GraphError::UnterminatedQuote { position } => {
write!(f, "unterminated quoted sheet name at byte {position}")
}
GraphError::InvalidReference { position, text } => {
write!(f, "invalid cell reference at byte {position}: {text:?}")
}
GraphError::ColumnOverflow { position, text } => {
write!(
f,
"column {text:?} at byte {position} exceeds u32 coordinate space"
)
}
GraphError::RowOverflow { position, text } => {
write!(
f,
"row {text:?} at byte {position} exceeds u32 coordinate space"
)
}
GraphError::RangeTooLarge { start, end, cells } => write!(
f,
"range ({},{}):({},{}) covers {cells} cells, above the expansion cap",
start.0, start.1, end.0, end.1
),
}
}
}
impl core::error::Error for GraphError {}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use super::GraphError;
use alloc::{format, string::String};
#[test]
fn display_is_informative_and_total() {
let cases = [
GraphError::EmptyFormula,
GraphError::UnterminatedString { position: 3 },
GraphError::UnterminatedQuote { position: 5 },
GraphError::InvalidReference {
position: 8,
text: String::from("1A"),
},
GraphError::ColumnOverflow {
position: 1,
text: String::from("ZZZZ"),
},
GraphError::RowOverflow {
position: 2,
text: String::from("99999"),
},
GraphError::RangeTooLarge {
start: (0, 0),
end: (9, 9),
cells: 100,
},
];
for e in cases {
let s = format!("{e}");
assert!(!s.is_empty(), "{e:?} rendered empty");
}
}
}