use crate::cell::utils::{column_index_from_string, get_column_letter};
use crate::exceptions::Result;
#[derive(Debug, Clone, PartialEq)]
pub struct Dimension {
pub index: String,
pub visible: bool,
pub outline_level: u32,
pub collapsed: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RowDimension {
pub index: u32,
pub height: f64,
pub visible: bool,
pub outline_level: u32,
pub collapsed: bool,
}
impl RowDimension {
pub fn new(index: u32) -> Self {
RowDimension {
index,
height: -1.0,
visible: true,
outline_level: 0,
collapsed: false,
}
}
pub fn with_height(mut self, height: f64) -> Self {
self.height = height;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn as_dimension(&self) -> Dimension {
Dimension {
index: self.index.to_string(),
visible: self.visible,
outline_level: self.outline_level,
collapsed: self.collapsed,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDimension {
pub index: String,
pub width: f64,
pub auto_size: bool,
pub visible: bool,
pub outline_level: u32,
pub collapsed: bool,
}
impl ColumnDimension {
pub fn new(index: &str) -> Self {
ColumnDimension {
index: index.to_uppercase(),
width: -1.0,
auto_size: false,
visible: true,
outline_level: 0,
collapsed: false,
}
}
pub fn from_index(index: u32) -> Result<Self> {
Ok(ColumnDimension::new(&get_column_letter(index)?))
}
pub fn column_index(&self) -> Result<u32> {
column_index_from_string(&self.index)
}
pub fn with_width(mut self, width: f64) -> Self {
self.width = width;
self
}
pub fn with_auto_size(mut self, auto_size: bool) -> Self {
self.auto_size = auto_size;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn as_dimension(&self) -> Dimension {
Dimension {
index: self.index.clone(),
visible: self.visible,
outline_level: self.outline_level,
collapsed: self.collapsed,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn row_defaults() {
let row = RowDimension::new(1);
assert_eq!(row.index, 1);
assert_eq!(row.height, -1.0);
assert!(row.visible);
assert_eq!(row.outline_level, 0);
assert!(!row.collapsed);
}
#[test]
fn column_defaults_and_index_round_trip() {
let col = ColumnDimension::from_index(27).unwrap();
assert_eq!(col.index, "AA");
assert_eq!(col.column_index().unwrap(), 27);
assert_eq!(col.width, -1.0);
assert!(!col.auto_size);
}
#[test]
fn builders_apply() {
let row = RowDimension::new(3).with_height(30.0).with_visible(false);
assert_eq!(row.height, 30.0);
assert!(!row.visible);
let col = ColumnDimension::new("c")
.with_width(12.5)
.with_auto_size(true);
assert_eq!(col.index, "C");
assert_eq!(col.width, 12.5);
assert!(col.auto_size);
}
#[test]
fn dimension_view_shares_common_fields() {
let dim = RowDimension::new(2).with_visible(false).as_dimension();
assert_eq!(dim.index, "2");
assert!(!dim.visible);
assert_eq!(dim.outline_level, 0);
}
}