use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::address::Address;
use crate::cell::Cell;
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Worksheet {
cells: BTreeMap<String, Cell>,
name: String,
}
impl Worksheet {
pub fn new(name: impl Into<String>) -> Self {
Self {
cells: BTreeMap::new(),
name: name.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub(crate) fn set_name(&mut self, name: impl Into<String>) {
self.name = name.into();
}
pub fn cells(&self) -> &BTreeMap<String, Cell> {
&self.cells
}
pub fn cells_mut(&mut self) -> &mut BTreeMap<String, Cell> {
&mut self.cells
}
pub fn get(&self, addr: Address) -> Option<&Cell> {
self.cells.get(&addr.to_a1())
}
pub fn get_mut(&mut self, addr: Address) -> Option<&mut Cell> {
self.cells.get_mut(&addr.to_a1())
}
pub fn set(&mut self, addr: Address, cell: Cell) -> Option<Cell> {
self.cells.insert(addr.to_a1(), cell)
}
pub fn clear(&mut self, addr: Address) -> Option<Cell> {
self.cells.remove(&addr.to_a1())
}
pub fn contains(&self, addr: Address) -> bool {
self.cells.contains_key(&addr.to_a1())
}
pub fn len(&self) -> usize {
self.cells.len()
}
pub fn is_empty(&self) -> bool {
self.cells.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (Address, &Cell)> {
self.cells.iter().map(|(key, cell)| {
let addr = Address::from_a1(key).expect("worksheet keys are always valid A1");
(addr, cell)
})
}
}