diff --git a/papergrid/examples/common_grid.rs b/papergrid/examples/common_grid.rs index b16ac69c..72ecca35 100644 --- a/papergrid/examples/common_grid.rs +++ b/papergrid/examples/common_grid.rs @@ -55,15 +55,15 @@ const fn generate_table_config() -> CompactConfig { intersection: Some('+'), }; - let mut cfg = CompactConfig::new(); - cfg.set_borders(STYLE); - cfg.set_alignment_horizontal(AlignmentHorizontal::Center); - cfg.set_padding(Sides::new( - Indent::spaced(1), - Indent::spaced(1), - Indent::spaced(0), - Indent::spaced(0), - )); + let cfg = CompactConfig::new() + .set_borders(STYLE) + .set_alignment_horizontal(AlignmentHorizontal::Center) + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::spaced(0), + Indent::spaced(0), + )); cfg } diff --git a/papergrid/examples/common_grid_no_std.rs b/papergrid/examples/common_grid_no_std.rs index 2abf1aae..c258c9c7 100644 --- a/papergrid/examples/common_grid_no_std.rs +++ b/papergrid/examples/common_grid_no_std.rs @@ -49,15 +49,15 @@ const fn generate_table_config() -> CompactConfig { intersection: Some('+'), }; - let mut cfg = CompactConfig::new(); - cfg.set_borders(STYLE); - cfg.set_alignment_horizontal(AlignmentHorizontal::Center); - cfg.set_padding(Sides::new( - Indent::spaced(1), - Indent::spaced(1), - Indent::spaced(3), - Indent::spaced(0), - )); + let cfg = CompactConfig::new() + .set_borders(STYLE) + .set_alignment_horizontal(AlignmentHorizontal::Center) + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::spaced(3), + Indent::spaced(0), + )); cfg } diff --git a/papergrid/src/config/compact/mod.rs b/papergrid/src/config/compact/mod.rs index a6c61c99..4ed8e9a2 100644 --- a/papergrid/src/config/compact/mod.rs +++ b/papergrid/src/config/compact/mod.rs @@ -45,8 +45,9 @@ impl CompactConfig { } /// Set grid margin. - pub const fn set_margin(&mut self, margin: Sides) { + pub const fn set_margin(mut self, margin: Sides) -> Self { self.margin = margin; + self } /// Returns a grid margin. @@ -55,8 +56,9 @@ impl CompactConfig { } /// Set the [`Borders`] value as correct one. - pub const fn set_borders(&mut self, borders: Borders) { + pub const fn set_borders(mut self, borders: Borders) -> Self { self.borders = borders; + self } /// Returns a current [`Borders`] structure. @@ -70,8 +72,9 @@ impl CompactConfig { } /// Set a padding to a given cells. - pub const fn set_padding(&mut self, padding: Sides) { + pub const fn set_padding(mut self, padding: Sides) -> Self { self.padding = padding; + self } /// Get a padding for a given. @@ -80,8 +83,9 @@ impl CompactConfig { } /// Set a horizontal alignment. - pub const fn set_alignment_horizontal(&mut self, alignment: AlignmentHorizontal) { + pub const fn set_alignment_horizontal(mut self, alignment: AlignmentHorizontal) -> Self { self.halignment = alignment; + self } /// Get a alignment horizontal. @@ -90,13 +94,15 @@ impl CompactConfig { } /// Sets colors of border carcass on the grid. - pub const fn set_borders_color(&mut self, borders: Borders>) { + pub const fn set_borders_color(mut self, borders: Borders>) -> Self { self.border_colors = borders; + self } /// Set colors for a margin. - pub const fn set_margin_color(&mut self, color: Sides>) { + pub const fn set_margin_color(mut self, color: Sides>) -> Self { self.margin_color = color; + self } /// Returns a margin color. @@ -105,8 +111,9 @@ impl CompactConfig { } /// Set a padding to a given cells. - pub const fn set_padding_color(&mut self, color: Sides>) { + pub const fn set_padding_color(mut self, color: Sides>) -> Self { self.padding_color = color; + self } /// Set a padding to a given cells. diff --git a/tabled/README.md b/tabled/README.md index 9188addf..18cfdf57 100644 --- a/tabled/README.md +++ b/tabled/README.md @@ -78,4 +78,4 @@ assert_table!( "| Hare | Drew DeVault | 2022 |" "'------------------------------'" ); -``` \ No newline at end of file +``` diff --git a/tabled/src/grid/config/compact_multiline_config.rs b/tabled/src/grid/config/compact_multiline_config.rs index 6fe9c465..6b95d9aa 100644 --- a/tabled/src/grid/config/compact_multiline_config.rs +++ b/tabled/src/grid/config/compact_multiline_config.rs @@ -44,7 +44,7 @@ impl CompactMultilineConfig { /// Set grid margin. pub fn set_margin(&mut self, margin: Sides) { - self.config.set_margin(margin); + self.config = self.config.set_margin(margin); } /// Returns a grid margin. @@ -54,7 +54,7 @@ impl CompactMultilineConfig { /// Set the [`Borders`] value as correct one. pub fn set_borders(&mut self, borders: Borders) { - self.config.set_borders(borders) + self.config = self.config.set_borders(borders); } /// Returns a current [`Borders`] structure. @@ -69,7 +69,7 @@ impl CompactMultilineConfig { /// Set a padding to a given cells. pub fn set_padding(&mut self, padding: Sides) { - self.config.set_padding(padding) + self.config = self.config.set_padding(padding) } /// Get a padding for a given. @@ -79,7 +79,7 @@ impl CompactMultilineConfig { /// Set a horizontal alignment. pub fn set_alignment_horizontal(&mut self, alignment: AlignmentHorizontal) { - self.config.set_alignment_horizontal(alignment) + self.config = self.config.set_alignment_horizontal(alignment) } /// Get a alignment horizontal. @@ -89,12 +89,12 @@ impl CompactMultilineConfig { /// Sets colors of border carcass on the grid. pub fn set_borders_color(&mut self, borders: Borders>) { - self.config.set_borders_color(borders) + self.config = self.config.set_borders_color(borders) } /// Set colors for a margin. pub fn set_margin_color(&mut self, color: Sides>) { - self.config.set_margin_color(color) + self.config = self.config.set_margin_color(color) } /// Returns a margin color. @@ -104,7 +104,7 @@ impl CompactMultilineConfig { /// Set a padding color to all cells. pub fn set_padding_color(&mut self, color: Sides>) { - self.config.set_padding_color(color) + self.config = self.config.set_padding_color(color) } /// get a padding color. diff --git a/tabled/src/lib.rs b/tabled/src/lib.rs index 315baa3a..ebce8d8a 100644 --- a/tabled/src/lib.rs +++ b/tabled/src/lib.rs @@ -167,33 +167,22 @@ #![cfg_attr(not(all(feature = "derive", feature = "std")), doc = "```ignore")] //! use tabled::{Tabled, Table}; //! use testing_table::assert_table; -//! use std::iter::once; -//! -//! #[derive(Tabled)] -//! struct Data( -//! #[tabled(rename = "word")] -//! String, -//! #[tabled(rename = "id")] -//! usize, -//! ); //! -//! let data = once(Data(String::from("Hello"), 0)) -//! .chain(once(Data(String::from("World"), 1))) -//! .chain(once(Data(String::from("!!!"), 2))); +//! let data = (0..3).map(|i| [i, i * 2, i * 3]); //! //! let mut table = Table::new(data); //! //! assert_table!( //! table, -//! "+-------+----+" -//! "| word | id |" -//! "+-------+----+" -//! "| Hello | 0 |" -//! "+-------+----+" -//! "| World | 1 |" -//! "+-------+----+" -//! "| !!! | 2 |" -//! "+-------+----+" +//! "+---+---+---+" +//! "| 0 | 1 | 2 |" +//! "+---+---+---+" +//! "| 0 | 0 | 0 |" +//! "+---+---+---+" +//! "| 1 | 2 | 3 |" +//! "+---+---+---+" +//! "| 2 | 4 | 6 |" +//! "+---+---+---+" //! ); //! ``` //! diff --git a/tabled/src/settings/alignment/mod.rs b/tabled/src/settings/alignment/mod.rs index b142d043..425be5a1 100644 --- a/tabled/src/settings/alignment/mod.rs +++ b/tabled/src/settings/alignment/mod.rs @@ -195,7 +195,7 @@ impl TableOption for Alignment { impl TableOption for Alignment { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { if let Horizontal(a) = self.inner { - cfg.set_alignment_horizontal(a) + *cfg = cfg.set_alignment_horizontal(a); } } diff --git a/tabled/src/settings/margin/mod.rs b/tabled/src/settings/margin/mod.rs index 3b791c71..7357c42a 100644 --- a/tabled/src/settings/margin/mod.rs +++ b/tabled/src/settings/margin/mod.rs @@ -104,7 +104,7 @@ impl TableOption for Margin { impl TableOption for Margin { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { - cfg.set_margin(self.indent); + *cfg = cfg.set_margin(self.indent); } } diff --git a/tabled/src/settings/margin_color/mod.rs b/tabled/src/settings/margin_color/mod.rs index 6a13aac6..6b811054 100644 --- a/tabled/src/settings/margin_color/mod.rs +++ b/tabled/src/settings/margin_color/mod.rs @@ -103,7 +103,7 @@ where { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { let colors = self.colors.convert_into(); - cfg.set_margin_color(colors); + *cfg = cfg.set_margin_color(colors); } } diff --git a/tabled/src/settings/padding/mod.rs b/tabled/src/settings/padding/mod.rs index 5ccf8aab..e6a7e09e 100644 --- a/tabled/src/settings/padding/mod.rs +++ b/tabled/src/settings/padding/mod.rs @@ -132,7 +132,7 @@ impl TableOption for Padding { impl TableOption for Padding { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { - cfg.set_padding(self.indent); + *cfg = cfg.set_padding(self.indent); } } diff --git a/tabled/src/settings/padding_color/mod.rs b/tabled/src/settings/padding_color/mod.rs index cd68aba4..dfae51f6 100644 --- a/tabled/src/settings/padding_color/mod.rs +++ b/tabled/src/settings/padding_color/mod.rs @@ -142,7 +142,7 @@ where fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { let c = self.colors.clone(); let colors = Sides::new(c.left.into(), c.right.into(), c.top.into(), c.bottom.into()); - cfg.set_padding_color(colors); + *cfg = cfg.set_padding_color(colors); } } diff --git a/tabled/src/settings/style/builder.rs b/tabled/src/settings/style/builder.rs index 3be16e93..3cb1df04 100644 --- a/tabled/src/settings/style/builder.rs +++ b/tabled/src/settings/style/builder.rs @@ -1453,7 +1453,7 @@ impl TableOption for Style { fn change(self, _: &mut Data, cfg: &mut CompactConfig, _: &mut Dims) { - cfg.set_borders(self.borders); + *cfg = cfg.set_borders(self.borders); } } diff --git a/tabled/src/settings/style/mod.rs b/tabled/src/settings/style/mod.rs index 70291652..39284ea8 100644 --- a/tabled/src/settings/style/mod.rs +++ b/tabled/src/settings/style/mod.rs @@ -142,7 +142,7 @@ impl TableOption for Borders { impl TableOption for Borders { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { - cfg.set_borders(self); + *cfg = cfg.set_borders(self); } } diff --git a/tabled/src/settings/themes/theme.rs b/tabled/src/settings/themes/theme.rs index 00fda714..a59492c3 100644 --- a/tabled/src/settings/themes/theme.rs +++ b/tabled/src/settings/themes/theme.rs @@ -371,7 +371,7 @@ impl TableOption for Theme { impl TableOption for Theme { fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) { - cfg.set_borders(self.chars); + *cfg = cfg.set_borders(self.chars); } } diff --git a/tabled/src/tables/compact.rs b/tabled/src/tables/compact.rs index d0a9c450..d1e7487d 100644 --- a/tabled/src/tables/compact.rs +++ b/tabled/src/tables/compact.rs @@ -301,16 +301,15 @@ where } const fn create_config() -> CompactConfig { - let mut cfg = CompactConfig::new(); - cfg.set_padding(Sides::new( - Indent::spaced(1), - Indent::spaced(1), - Indent::zero(), - Indent::zero(), - )); - cfg.set_alignment_horizontal(AlignmentHorizontal::Left); - cfg.set_borders(Style::ascii().get_borders()); - cfg + CompactConfig::new() + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::zero(), + Indent::zero(), + )) + .set_alignment_horizontal(AlignmentHorizontal::Left) + .set_borders(Style::ascii().get_borders()) } impl TableOption for CompactConfig { diff --git a/tabled/src/tables/iter.rs b/tabled/src/tables/iter.rs index 3b03626a..3e99fb99 100644 --- a/tabled/src/tables/iter.rs +++ b/tabled/src/tables/iter.rs @@ -352,16 +352,15 @@ fn get_count_columns(opts: &Settings, buf: &[Vec]) -> usize { } const fn create_config() -> CompactConfig { - let mut cfg = CompactConfig::new(); - cfg.set_padding(Sides::new( - Indent::spaced(1), - Indent::spaced(1), - Indent::zero(), - Indent::zero(), - )); - cfg.set_alignment_horizontal(AlignmentHorizontal::Left); - cfg.set_borders(Style::ascii().get_borders()); - cfg + CompactConfig::new() + .set_padding(Sides::new( + Indent::spaced(1), + Indent::spaced(1), + Indent::zero(), + Indent::zero(), + )) + .set_alignment_horizontal(AlignmentHorizontal::Left) + .set_borders(Style::ascii().get_borders()) } fn build_records(