use core::fmt;
use core::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExcelError {
Div0,
Na,
Name,
Null,
Num,
Ref,
Value,
Spill,
Calc,
}
impl ExcelError {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
ExcelError::Div0 => "#DIV/0!",
ExcelError::Na => "#N/A",
ExcelError::Name => "#NAME?",
ExcelError::Null => "#NULL!",
ExcelError::Num => "#NUM!",
ExcelError::Ref => "#REF!",
ExcelError::Value => "#VALUE!",
ExcelError::Spill => "#SPILL!",
ExcelError::Calc => "#CALC!",
}
}
}
impl fmt::Display for ExcelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for ExcelError {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"#DIV/0!" => Ok(ExcelError::Div0),
"#N/A" => Ok(ExcelError::Na),
"#NAME?" => Ok(ExcelError::Name),
"#NULL!" => Ok(ExcelError::Null),
"#NUM!" => Ok(ExcelError::Num),
"#REF!" => Ok(ExcelError::Ref),
"#VALUE!" => Ok(ExcelError::Value),
"#SPILL!" => Ok(ExcelError::Spill),
"#CALC!" => Ok(ExcelError::Calc),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum CellValue {
Number(f64),
Text(alloc::string::String),
Boolean(bool),
Error(ExcelError),
Formula(alloc::string::String),
Blank,
}
impl CellValue {
#[must_use]
pub const fn number(n: f64) -> Self {
CellValue::Number(n)
}
#[must_use]
pub fn text(s: impl Into<alloc::string::String>) -> Self {
CellValue::Text(s.into())
}
#[must_use]
pub const fn boolean(b: bool) -> Self {
CellValue::Boolean(b)
}
#[must_use]
pub const fn error(e: ExcelError) -> Self {
CellValue::Error(e)
}
#[must_use]
pub fn formula(text: impl Into<alloc::string::String>) -> Self {
CellValue::Formula(text.into())
}
#[must_use]
pub const fn is_formula(&self) -> bool {
matches!(self, CellValue::Formula(_))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell {
pub value: CellValue,
pub style: Option<u32>,
}
impl Cell {
#[must_use]
pub const fn new(value: CellValue) -> Self {
Cell { value, style: None }
}
#[must_use]
pub const fn number(n: f64) -> Self {
Cell::new(CellValue::Number(n))
}
#[must_use]
pub fn text(s: impl Into<alloc::string::String>) -> Self {
Cell::new(CellValue::Text(s.into()))
}
#[must_use]
pub const fn boolean(b: bool) -> Self {
Cell::new(CellValue::Boolean(b))
}
#[must_use]
pub const fn error(e: ExcelError) -> Self {
Cell::new(CellValue::Error(e))
}
#[must_use]
pub fn formula(text: impl Into<alloc::string::String>) -> Self {
Cell::new(CellValue::Formula(text.into()))
}
#[must_use]
pub const fn blank() -> Self {
Cell::new(CellValue::Blank)
}
#[must_use]
pub const fn with_style(mut self, style: u32) -> Self {
self.style = Some(style);
self
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use super::{Cell, CellValue, ExcelError};
use alloc::format;
use alloc::string::ToString;
use core::str::FromStr;
#[test]
fn error_literals_round_trip_through_display_and_from_str() {
let all = [
ExcelError::Div0,
ExcelError::Na,
ExcelError::Name,
ExcelError::Null,
ExcelError::Num,
ExcelError::Ref,
ExcelError::Value,
ExcelError::Spill,
ExcelError::Calc,
];
for e in all {
let rendered = format!("{e}");
assert_eq!(rendered, e.as_str(), "{e:?} Display/as_str mismatch");
assert_eq!(ExcelError::from_str(&rendered), Ok(e), "{e:?} round trip");
}
}
#[test]
fn unknown_error_literal_is_rejected() {
assert_eq!(ExcelError::from_str("#NOPE!"), Err(()));
assert_eq!(ExcelError::from_str("#ref!"), Err(())); assert_eq!(ExcelError::from_str(""), Err(()));
}
#[test]
fn cell_constructors_and_style_builder() {
let styled = Cell::number(1.5).with_style(7);
assert_eq!(
styled,
Cell {
value: CellValue::Number(1.5),
style: Some(7)
}
);
assert_eq!(Cell::text("hi").style, None);
assert_eq!(Cell::boolean(true).value, CellValue::Boolean(true));
assert_eq!(
Cell::formula("=A1").value,
CellValue::Formula("=A1".to_string())
);
assert_eq!(Cell::blank().value, CellValue::Blank);
assert_eq!(Cell::new(CellValue::Blank).style, None);
}
#[test]
fn value_helpers() {
assert!(CellValue::formula("=A1").is_formula());
assert!(!CellValue::number(0.0).is_formula());
assert!(!CellValue::Blank.is_formula());
assert_eq!(CellValue::text("x"), CellValue::Text("x".to_string()));
assert_eq!(CellValue::boolean(false), CellValue::Boolean(false));
assert_eq!(CellValue::number(2.0), CellValue::Number(2.0));
}
}