use spreadsheet_ods_cellref::refs_format::{ fmt_cell_range, fmt_cell_range_list, fmt_cell_ref, fmt_col_name, fmt_row_name, fmt_table_name, }; use spreadsheet_ods_cellref::{CellRange, CellRef}; use std::fmt; use std::fmt::{Debug, Display, Formatter, Write}; pub(crate) struct Fmt(pub F) where for<'a> F: Fn(&mut Formatter<'a>) -> fmt::Result; impl Debug for Fmt where for<'a> F: Fn(&mut Formatter<'a>) -> fmt::Result, { /// Calls f with the given Formatter. fn fmt<'a>(&self, f: &mut Formatter<'a>) -> fmt::Result { (self.0)(f) } } impl Display for Fmt where for<'a> F: Fn(&mut Formatter<'a>) -> fmt::Result, { /// Calls f with the given Formatter. fn fmt<'a>(&self, f: &mut Formatter<'a>) -> fmt::Result { (self.0)(f) } } #[test] pub fn test_print_range() { let mut vec = Vec::new(); vec.push(CellRange::local(1, 1, 9, 9)); vec.push(CellRange::local(11, 11, 19, 19)); let mut buf = String::new(); let _ = write!(buf, "{}", Fmt(|f| fmt_cell_range_list(f, &vec))); assert_eq!(buf.as_str(), ".B2:.J10 .L12:.T20"); } #[test] fn test_names() { let mut buf = String::new(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, 0))).unwrap(); assert_eq!(buf, "A"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, 1))).unwrap(); assert_eq!(buf, "B"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, 26))).unwrap(); assert_eq!(buf, "AA"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, 675))).unwrap(); assert_eq!(buf, "YZ"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, 676))).unwrap(); assert_eq!(buf, "ZA"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, u32::MAX - 1))).unwrap(); assert_eq!(buf, "MWLQKWU"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_col_name(f, u32::MAX))).unwrap(); assert_eq!(buf, "MWLQKWV"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_row_name(f, 0))).unwrap(); assert_eq!(buf, "1"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_row_name(f, 927))).unwrap(); assert_eq!(buf, "928"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_row_name(f, u32::MAX - 1))).unwrap(); assert_eq!(buf, "4294967295"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_row_name(f, u32::MAX))).unwrap(); assert_eq!(buf, "4294967296"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_table_name(f, "fable", false))).unwrap(); assert_eq!(buf, "fable"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_table_name(f, "fa le", false))).unwrap(); assert_eq!(buf, "'fa le'"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_table_name(f, "fa'le", false))).unwrap(); assert_eq!(buf, "'fa''le'"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_table_name(f, "fa.le", false))).unwrap(); assert_eq!(buf, "'fa.le'"); buf.clear(); write!(buf, "{}", Fmt(|f| fmt_cell_ref(f, &CellRef::local(5, 6)))).unwrap(); assert_eq!(buf, ".G6"); buf.clear(); write!( buf, "{}", Fmt(|f| fmt_cell_range(f, &CellRange::local(5, 6, 7, 8))) ) .unwrap(); assert_eq!(buf, ".G6:.I8"); buf.clear(); write!( buf, "{}", Fmt(|f| fmt_cell_range(f, &CellRange::remote("blame", 5, 6, 7, 8))) ) .unwrap(); assert_eq!(buf, "blame.G6:.I8"); buf.clear(); }