use crate::style::StyleId;
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Value {
#[default]
Empty,
Number(f64),
String(String),
Bool(bool),
Formula(String),
Error(String),
Date(f64),
}
impl Value {
pub fn is_empty(&self) -> bool {
matches!(self, Value::Empty)
}
pub fn is_formula(&self) -> bool {
matches!(self, Value::Formula(_))
}
pub fn as_formula(&self) -> Option<&str> {
match self {
Value::Formula(f) => Some(f),
_ => None,
}
}
pub fn as_number(&self) -> Option<f64> {
match self {
Value::Number(n) => Some(*n),
_ => None,
}
}
pub fn as_string(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::String(s.to_string())
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::String(s)
}
}
impl From<f64> for Value {
fn from(n: f64) -> Self {
Value::Number(n)
}
}
impl From<i32> for Value {
fn from(n: i32) -> Self {
Value::Number(f64::from(n))
}
}
impl From<i64> for Value {
fn from(n: i64) -> Self {
Value::Number(n as f64)
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
Value::Bool(b)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell {
pub(crate) value: Value,
pub(crate) style_id: Option<StyleId>,
}
impl Cell {
pub const fn empty() -> Self {
Self {
value: Value::Empty,
style_id: None,
}
}
pub fn new<V: Into<Value>>(value: V) -> Self {
Self {
value: value.into(),
style_id: None,
}
}
pub fn value(&self) -> &Value {
&self.value
}
pub fn set_value<V: Into<Value>>(&mut self, value: V) {
self.value = value.into();
}
pub fn set_formula<S: Into<String>>(&mut self, formula: S) {
self.value = Value::Formula(formula.into());
}
pub fn style_id(&self) -> Option<StyleId> {
self.style_id
}
pub fn set_style_id(&mut self, style_id: StyleId) {
self.style_id = Some(style_id);
}
pub fn clear_style(&mut self) {
self.style_id = None;
}
}
impl Default for Cell {
fn default() -> Self {
Cell::empty()
}
}
impl<V: Into<Value>> From<V> for Cell {
fn from(value: V) -> Self {
Cell::new(value)
}
}