use std::fmt;
use std::sync::Arc;
pub type InternedString = Arc<str>;
#[derive(Clone, Debug, PartialEq, Default)]
pub enum CellValue {
String(InternedString),
Number(f64),
Boolean(bool),
Date(String),
Formula(String),
#[default]
Empty,
}
impl CellValue {
pub fn is_empty(&self) -> bool {
matches!(self, CellValue::Empty)
}
pub fn is_formula(&self) -> bool {
matches!(self, CellValue::Formula(_))
}
pub fn as_string(&self) -> Option<&str> {
match self {
CellValue::String(s) => Some(s.as_ref()),
_ => None,
}
}
pub fn as_number(&self) -> Option<f64> {
match self {
CellValue::Number(n) => Some(*n),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
CellValue::Boolean(b) => Some(*b),
_ => None,
}
}
pub fn as_formula(&self) -> Option<&str> {
match self {
CellValue::Formula(f) => Some(f),
_ => None,
}
}
pub fn data_type_code(&self) -> &'static str {
match self {
CellValue::String(_) => "s",
CellValue::Number(_) => "n",
CellValue::Boolean(_) => "b",
CellValue::Date(_) => "d",
CellValue::Formula(_) => "str",
CellValue::Empty => "",
}
}
}
impl fmt::Display for CellValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CellValue::String(s) => write!(f, "{}", s.as_ref()),
CellValue::Number(n) => write!(f, "{}", n),
CellValue::Boolean(b) => write!(f, "{}", if *b { "TRUE" } else { "FALSE" }),
CellValue::Date(d) => write!(f, "{}", d),
CellValue::Formula(formula) => write!(f, "={}", formula),
CellValue::Empty => write!(f, ""),
}
}
}
impl From<String> for CellValue {
fn from(s: String) -> Self {
CellValue::String(Arc::from(s))
}
}
impl From<&str> for CellValue {
fn from(s: &str) -> Self {
CellValue::String(Arc::from(s))
}
}
impl From<f64> for CellValue {
fn from(n: f64) -> Self {
CellValue::Number(n)
}
}
impl From<i32> for CellValue {
fn from(n: i32) -> Self {
CellValue::Number(n as f64)
}
}
impl From<i64> for CellValue {
fn from(n: i64) -> Self {
CellValue::Number(n as f64)
}
}
impl From<bool> for CellValue {
fn from(b: bool) -> Self {
CellValue::Boolean(b)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cell_value_string() {
let val = CellValue::String(Arc::from("Hello"));
assert_eq!(val.as_string(), Some("Hello"));
assert_eq!(val.to_string(), "Hello");
assert_eq!(val.data_type_code(), "s");
}
#[test]
fn test_cell_value_number() {
let val = CellValue::Number(42.5);
assert_eq!(val.as_number(), Some(42.5));
assert_eq!(val.to_string(), "42.5");
assert_eq!(val.data_type_code(), "n");
}
#[test]
fn test_cell_value_boolean() {
let val = CellValue::Boolean(true);
assert_eq!(val.as_bool(), Some(true));
assert_eq!(val.to_string(), "TRUE");
assert_eq!(val.data_type_code(), "b");
}
#[test]
fn test_cell_value_formula() {
let val = CellValue::Formula("SUM(A1:A10)".to_string());
assert!(val.is_formula());
assert_eq!(val.as_formula(), Some("SUM(A1:A10)"));
assert_eq!(val.to_string(), "=SUM(A1:A10)");
}
#[test]
fn test_cell_value_empty() {
let val = CellValue::Empty;
assert!(val.is_empty());
assert_eq!(val.to_string(), "");
}
#[test]
fn test_cell_value_from() {
let val: CellValue = "Hello".into();
assert_eq!(val, CellValue::String(Arc::from("Hello")));
let val: CellValue = 42.5.into();
assert_eq!(val, CellValue::Number(42.5));
let val: CellValue = true.into();
assert_eq!(val, CellValue::Boolean(true));
}
}