From d6b336a4c910e26563c4877fe809110c47cbc226 Mon Sep 17 00:00:00 2001 From: Wenxuan Zhang Date: Mon, 3 Jan 2022 15:21:09 +0800 Subject: [PATCH] refactor: rename --- src/style.rs | 18 ++-- src/table/mod.rs | 18 ++-- src/table/row.rs | 8 +- src/table/{format.rs => style.rs} | 144 ++++++++++++++---------------- 4 files changed, 88 insertions(+), 100 deletions(-) rename src/table/{format.rs => style.rs} (84%) diff --git a/src/style.rs b/src/style.rs index 258c870..a46ad05 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,40 +1,40 @@ use crate::{ cli::Style, - table::{RowSep, TableFormat, TableFormatBuilder}, + table::{RowSep, TableStyle, TableStyleBuilder}, }; -pub fn table_format(style: Style, padding: usize, indent: usize) -> TableFormat { +pub fn table_format(style: Style, padding: usize, indent: usize) -> TableStyle { let builder = match style { - Style::None => TableFormatBuilder::new().clear_seps(), - Style::Ascii => TableFormatBuilder::new().col_sep('|').row_seps( + Style::None => TableStyleBuilder::new().clear_seps(), + Style::Ascii => TableStyleBuilder::new().col_sep('|').row_seps( RowSep::new('-', '+', '+', '+'), RowSep::new('-', '+', '+', '+'), None, RowSep::new('-', '+', '+', '+'), ), - Style::Sharp => TableFormatBuilder::new().col_sep('│').row_seps( + Style::Sharp => TableStyleBuilder::new().col_sep('│').row_seps( RowSep::new('─', '┌', '┬', '┐'), RowSep::new('─', '├', '┼', '┤'), None, RowSep::new('─', '└', '┴', '┘'), ), - Style::Rounded => TableFormatBuilder::new().col_sep('│').row_seps( + Style::Rounded => TableStyleBuilder::new().col_sep('│').row_seps( RowSep::new('─', '╭', '┬', '╮'), RowSep::new('─', '├', '┼', '┤'), None, RowSep::new('─', '╰', '┴', '╯'), ), - Style::Reinforced => TableFormatBuilder::new().col_sep('│').row_seps( + Style::Reinforced => TableStyleBuilder::new().col_sep('│').row_seps( RowSep::new('─', '┏', '┬', '┓'), RowSep::new('─', '├', '┼', '┤'), None, RowSep::new('─', '┗', '┴', '┛'), ), Style::Markdown => - TableFormatBuilder::new() + TableStyleBuilder::new() .col_sep('|') .row_seps(None, RowSep::new('-', '|', '|', '|'), None, None), - Style::Grid => TableFormatBuilder::new().col_sep('│').row_seps( + Style::Grid => TableStyleBuilder::new().col_sep('│').row_seps( RowSep::new('─', '┌', '┬', '┐'), RowSep::new('─', '├', '┼', '┤'), RowSep::new('─', '├', '┼', '┤'), diff --git a/src/table/mod.rs b/src/table/mod.rs index 82c0e61..82be844 100644 --- a/src/table/mod.rs +++ b/src/table/mod.rs @@ -1,6 +1,6 @@ mod cell; -mod format; mod row; +mod style; use anyhow::Result; use cell::Cell; @@ -9,7 +9,7 @@ use row::Row; use std::io::{self, Write}; use unicode_width::UnicodeWidthStr; -pub use format::{RowPos, RowSep, TableFormat, TableFormatBuilder}; +pub use style::{RowPos, RowSep, TableStyle, TableStyleBuilder}; pub struct CsvTableWriter { pub(crate) header: Option, @@ -25,7 +25,7 @@ impl CsvTableWriter { Ok(Self { header, widths, records }) } - pub fn writeln(self, wtr: &mut W, fmt: &TableFormat) -> Result<()> { + pub fn writeln(self, wtr: &mut W, fmt: &TableStyle) -> Result<()> { let widths = &self.widths; fmt.write_row_sep(wtr, widths, RowPos::Top)?; @@ -84,8 +84,6 @@ fn sniff_widths( #[cfg(test)] mod test { - use crate::table::format::TableFormatBuilder; - use super::*; use csv::ReaderBuilder; @@ -96,7 +94,7 @@ mod test { let wtr = CsvTableWriter::new(rdr, 3)?; let mut buf = Vec::new(); - wtr.writeln(&mut buf, &TableFormat::default())?; + wtr.writeln(&mut buf, &TableStyle::default())?; assert_eq!( " @@ -119,7 +117,7 @@ mod test { let text = "a,b,c\n1,2,3\n4,5,6"; let rdr = ReaderBuilder::new().has_headers(true).from_reader(text.as_bytes()); let wtr = CsvTableWriter::new(rdr, 3)?; - let fmt = TableFormatBuilder::default().padding(0).build(); + let fmt = TableStyleBuilder::default().padding(0).build(); let mut buf = Vec::new(); wtr.writeln(&mut buf, &fmt)?; @@ -145,7 +143,7 @@ mod test { let text = "a,b,c\n1,2,3\n4,5,6"; let rdr = ReaderBuilder::new().has_headers(true).from_reader(text.as_bytes()); let wtr = CsvTableWriter::new(rdr, 3)?; - let fmt = TableFormatBuilder::default().indent(4).build(); + let fmt = TableStyleBuilder::default().indent(4).build(); let mut buf = Vec::new(); wtr.writeln(&mut buf, &fmt)?; @@ -171,7 +169,7 @@ mod test { let text = "a,ab,abc"; let rdr = ReaderBuilder::new().has_headers(true).from_reader(text.as_bytes()); let wtr = CsvTableWriter::new(rdr, 3)?; - let fmt = TableFormat::default(); + let fmt = TableStyle::default(); let mut buf = Vec::new(); wtr.writeln(&mut buf, &fmt)?; @@ -193,7 +191,7 @@ mod test { let text = "1,123,35\n383,2, 17"; let rdr = ReaderBuilder::new().has_headers(false).from_reader(text.as_bytes()); let wtr = CsvTableWriter::new(rdr, 3)?; - let fmt = TableFormatBuilder::new() + let fmt = TableStyleBuilder::new() .col_sep('│') .row_seps( RowSep::new('─', '╭', '┬', '╮'), diff --git a/src/table/row.rs b/src/table/row.rs index bb5caff..c511440 100644 --- a/src/table/row.rs +++ b/src/table/row.rs @@ -1,7 +1,7 @@ use anyhow::Result; use std::io::Write; -use crate::table::{format::ColPos, Cell, TableFormat}; +use crate::table::{style::ColPos, Cell, TableStyle}; /// Represent a table row made of cells #[derive(Clone, Debug)] @@ -16,7 +16,7 @@ impl<'a> FromIterator<&'a str> for Row<'a> { } impl<'a> Row<'a> { - pub fn writeln(&self, wtr: &mut T, fmt: &TableFormat, widths: &[usize]) -> Result<()> { + pub fn writeln(&self, wtr: &mut T, fmt: &TableStyle, widths: &[usize]) -> Result<()> { let sep = fmt.get_col_sep(ColPos::Mid).map(|c| c.to_string()).unwrap_or_default(); write!(wtr, "{:indent$}", "", indent = fmt.indent)?; @@ -42,7 +42,7 @@ mod test { fn write_ascii_row() -> Result<()> { let row = Row::from_iter(["a", "b"]); let buf = &mut Vec::new(); - let fmt = TableFormat::default(); + let fmt = TableStyle::default(); let widths = [3, 4]; row.writeln(buf, &fmt, &widths)?; @@ -54,7 +54,7 @@ mod test { fn write_cjk_row() -> Result<()> { let row = Row::from_iter(["李磊(Jack)", "四川省成都市", "💍"]); let buf = &mut Vec::new(); - let fmt = TableFormat::default(); + let fmt = TableStyle::default(); let widths = [10, 8, 2]; row.writeln(buf, &fmt, &widths)?; diff --git a/src/table/format.rs b/src/table/style.rs similarity index 84% rename from src/table/format.rs rename to src/table/style.rs index 5735259..490072a 100644 --- a/src/table/format.rs +++ b/src/table/style.rs @@ -2,7 +2,7 @@ use anyhow::Result; use std::io::Write; /// Position of row separator -#[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)] +#[derive(Debug, Clone, Copy)] pub enum RowPos { /// Top separator row (top border) /// ``` @@ -32,31 +32,8 @@ pub enum RowPos { Bot, } -/// Position of column separator -#[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)] -pub enum ColPos { - /// Left separator column (left border) - /// ``` - /// │ 1 │ 2 │ - /// ^ - /// ``` - Lhs, - /// Middle column separators - /// ``` - /// │ 1 │ 2 │ - /// ^ - /// ``` - Mid, - /// Right separator column (right border) - /// ``` - /// │ 1 │ 2 │ - /// ^ - /// ``` - Rhs, -} - /// The characters used for printing a row separator -#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Copy)] pub struct RowSep { /// Normal row separator /// ``` @@ -84,6 +61,51 @@ pub struct RowSep { rjunc: char, } +#[derive(Debug, Clone, Copy)] +pub struct RowSeps { + /// Optional top line separator + pub top: Option, + /// Optional title line separator + pub snd: Option, + /// Optional internal row separator + pub mid: Option, + /// Optional bottom line separator + pub bot: Option, +} + +/// Position of column separator +#[derive(Debug, Clone, Copy)] +pub enum ColPos { + /// Left separator column (left border) + /// ``` + /// │ 1 │ 2 │ + /// ^ + /// ``` + Lhs, + /// Middle column separators + /// ``` + /// │ 1 │ 2 │ + /// ^ + /// ``` + Mid, + /// Right separator column (right border) + /// ``` + /// │ 1 │ 2 │ + /// ^ + /// ``` + Rhs, +} + +#[derive(Debug, Clone, Copy)] +pub struct ColSeps { + /// Optional left border character + pub lhs: Option, + /// Optional inner column separator character + pub mid: Option, + /// Optional right border character + pub rhs: Option, +} + impl RowSep { pub fn new(sep: char, ljunc: char, cjunc: char, rjunc: char) -> RowSep { RowSep { sep, ljunc, cjunc, rjunc } @@ -96,18 +118,6 @@ impl Default for RowSep { } } -#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] -pub struct RowSeps { - /// Optional top line separator - pub top: Option, - /// Optional title line separator - pub snd: Option, - /// Optional internal row separator - pub mid: Option, - /// Optional bottom line separator - pub bot: Option, -} - impl Default for RowSeps { fn default() -> Self { Self { @@ -119,34 +129,28 @@ impl Default for RowSeps { } } -#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] -pub struct ColSeps { - /// Optional left border character - pub lhs: Option, - /// Optional inner column separator character - pub mid: Option, - /// Optional right border character - pub rhs: Option, -} - impl Default for ColSeps { fn default() -> Self { Self { lhs: Some('|'), mid: Some('|'), rhs: Some('|') } } } -#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] -pub struct TableFormat { +#[derive(Debug, Clone, Copy)] +pub struct TableStyle { + /// Column separators pub colseps: ColSeps, + + /// Row separators pub rowseps: RowSeps, /// Left and right padding pub padding: usize, + /// Global indentation - pub indent: usize, + pub indent: usize, } -impl Default for TableFormat { +impl Default for TableStyle { fn default() -> Self { Self { indent: 0, @@ -157,7 +161,7 @@ impl Default for TableFormat { } } -impl TableFormat { +impl TableStyle { fn get_row_sep(&self, pos: RowPos) -> &Option { match pos { RowPos::Mid => &self.rowseps.mid, @@ -205,19 +209,17 @@ impl TableFormat { } pub(crate) fn write_col_sep(&self, wtr: &mut W, pos: ColPos) -> Result<()> { - match self.get_col_sep(pos) { - Some(s) => Ok(write!(wtr, "{}", s)?), - None => Ok(()), - } + self.get_col_sep(pos).map(|s| write!(wtr, "{}", s)).transpose()?; + Ok(()) } } -#[derive(Default)] -pub struct TableFormatBuilder { - format: Box, +#[derive(Default, Debug, Clone)] +pub struct TableStyleBuilder { + format: Box, } -impl TableFormatBuilder { +impl TableStyleBuilder { pub fn new() -> Self { Self::default() } @@ -267,30 +269,18 @@ impl TableFormatBuilder { self } - pub fn build(&self) -> TableFormat { + pub fn build(&self) -> TableStyle { *self.format } } -impl From for TableFormat { - fn from(val: TableFormatBuilder) -> Self { - *val.format - } -} - -impl From for TableFormatBuilder { - fn from(fmt: TableFormat) -> Self { - TableFormatBuilder { format: Box::new(fmt) } - } -} - #[cfg(test)] mod test { use super::*; #[test] - fn print_column_separator() -> Result<()> { - let fmt = TableFormatBuilder::new().col_seps('|', '|', '|').padding(1).build(); + fn test_write_column_separator() -> Result<()> { + let fmt = TableStyleBuilder::new().col_seps('|', '|', '|').padding(1).build(); let mut out = Vec::new(); fmt.write_col_sep(&mut out, ColPos::Lhs)?; @@ -300,8 +290,8 @@ mod test { } #[test] - fn test_print_line_separator() -> Result<()> { - let fmt = TableFormatBuilder::new().indent(4).build(); + fn test_write_row_separator() -> Result<()> { + let fmt = TableStyleBuilder::new().indent(4).build(); let mut out = Vec::new(); fmt.write_row_sep(&mut out, &[2, 4, 6], RowPos::Top)?;