summaryrefslogtreecommitdiffstats
path: root/src/format.rs
diff options
context:
space:
mode:
authorPierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com>2017-06-05 15:34:54 +0200
committerPierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com>2017-06-05 16:31:04 +0200
commit4e36ac179fc5341b35221018573e265a1f1a0a53 (patch)
tree629b1332d2600cafd9292c4426c85de04624108d /src/format.rs
parent14dcf5f9615eeafb8eb7c20732f55d41d1045884 (diff)
Fixed lint (+clippy) warnings and line endings
Added some lint rustc checks and fixed warnings. Fixed some clippy warnings & errors Converted remaining CRLF line endings to LF
Diffstat (limited to 'src/format.rs')
-rw-r--r--src/format.rs400
1 files changed, 207 insertions, 193 deletions
diff --git a/src/format.rs b/src/format.rs
index 25f477e..4160737 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -9,25 +9,36 @@ use super::utils::NEWLINE;
/// Alignment for cell's content
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum Alignment {
+ /// Align left
LEFT,
+ /// Align in the center
CENTER,
+ /// Align right
RIGHT,
}
/// Position of a line separator in a table
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum LinePosition {
+ /// Table's border on top
Top,
+ /// Line separator between the titles row,
+ /// and the first data row
Title,
+ /// Line separator between data rows
Intern,
+ /// Bottom table's border
Bottom,
}
/// Position of a column separator in a row
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum ColumnPosition {
+ /// Left table's border
Left,
+ /// Internal column separators
Intern,
+ /// Rigth table's border
Right,
}
@@ -164,11 +175,11 @@ impl TableFormat {
/// Set a line separator
pub fn separator(&mut self, what: LinePosition, separator: LineSeparator) {
- *match what {
- LinePosition::Top => &mut self.top_sep,
- LinePosition::Bottom => &mut self.bottom_sep,
- LinePosition::Title => &mut self.tsep,
- LinePosition::Intern => &mut self.lsep,
+ *match what {
+ LinePosition::Top => &mut self.top_sep,
+ LinePosition::Bottom => &mut self.bottom_sep,
+ LinePosition::Title => &mut self.tsep,
+ LinePosition::Intern => &mut self.lsep,
} = Some(separator);
}
@@ -180,16 +191,16 @@ impl TableFormat {
}
fn get_sep_for_line(&self, pos: LinePosition) -> &Option<LineSeparator> {
- match pos {
- LinePosition::Intern => return &self.lsep,
- LinePosition::Top => return &self.top_sep,
- LinePosition::Bottom => return &self.bottom_sep,
+ match pos {
+ LinePosition::Intern => &self.lsep,
+ LinePosition::Top => &self.top_sep,
+ LinePosition::Bottom => &self.bottom_sep,
LinePosition::Title => {
- match &self.tsep {
- s @ &Some(_) => s,
- &None => &self.lsep,
+ match &self.tsep {
+ s @ &Some(_) => s,
+ &None => &self.lsep,
}
- }
+ }
}
}
@@ -199,7 +210,7 @@ impl TableFormat {
col_width: &[usize],
pos: LinePosition)
-> Result<(), Error> {
- match *self.get_sep_for_line(pos) {
+ match *self.get_sep_for_line(pos) {
Some(ref l) => {
l._print(out,
col_width,
@@ -207,16 +218,18 @@ impl TableFormat {
self.csep.is_some(),
self.lborder.is_some(),
self.rborder.is_some())
- }
- None => Ok(()),
+ }
+ None => Ok(()),
}
}
+ /// Returns the character used to separate columns.
+ /// `pos` specify if the separator is left/right final or internal to the table
pub fn get_column_separator(&self, pos: ColumnPosition) -> Option<char> {
- match pos {
- ColumnPosition::Left => self.lborder,
- ColumnPosition::Intern => self.csep,
- ColumnPosition::Right => self.rborder,
+ match pos {
+ ColumnPosition::Left => self.lborder,
+ ColumnPosition::Intern => self.csep,
+ ColumnPosition::Right => self.rborder,
}
}
@@ -225,9 +238,9 @@ impl TableFormat {
out: &mut T,
pos: ColumnPosition)
-> Result<(), Error> {
- match self.get_column_separator(pos) {
- Some(s) => out.write_all(Utf8Char::from(s).as_bytes()),
- None => Ok(()),
+ match self.get_column_separator(pos) {
+ Some(s) => out.write_all(Utf8Char::from(s).as_bytes()),
+ None => Ok(()),
}
}
}
@@ -244,6 +257,7 @@ pub struct FormatBuilder {
}
impl FormatBuilder {
+ /// Creates a new builder
pub fn new() -> FormatBuilder {
FormatBuilder { format: Box::new(TableFormat::new()) }
}
@@ -289,175 +303,175 @@ impl FormatBuilder {
pub mod consts {
use super::{TableFormat, LineSeparator, FormatBuilder, LinePosition};
- lazy_static! {
- /// A line separator made of `-` and `+`
- static ref MINUS_PLUS_SEP: LineSeparator = LineSeparator::new('-', '+', '+', '+');
- /// A line separator made of `=` and `+`
- static ref EQU_PLUS_SEP: LineSeparator = LineSeparator::new('=', '+', '+', '+');
-
- /// Default table format
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +====+====+
- /// | a | b |
- /// +----+----+
- /// | d | c |
- /// +----+----+
- /// ```
- pub static ref FORMAT_DEFAULT: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// Similar to `FORMAT_DEFAULT` but without special separator after title line
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +----+----+
- /// | a | b |
- /// +----+----+
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_TITLE: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// With no line separator, but with title separator
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +----+----+
- /// | a | b |
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// With no line or title separator
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// | a | b |
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_LINESEP: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// No column separator
- ///
- /// # Example
- /// ```text
- /// --------
- /// T1 T2
- /// ========
- /// a b
- /// --------
- /// d c
- /// --------
- /// ```
- pub static ref FORMAT_NO_COLSEP: TableFormat = FormatBuilder::new()
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// Format for printing a table without any separators (only alignment)
- ///
- /// # Example
- /// ```text
- /// T1 T2
- /// a b
- /// d c
- /// ```
- pub static ref FORMAT_CLEAN: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .build();
-
- /// Format for a table with only external borders and title separator
- ///
- /// # Example
- /// ```text
- /// +--------+
- /// | T1 T2 |
- /// +========+
- /// | a b |
- /// | c d |
- /// +--------+
- /// ```
- pub static ref FORMAT_BORDERS_ONLY: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .borders('|')
- .build();
-
- /// A table with no external border
- ///
- /// # Example
- /// ```text
- /// T1 | T2
- /// ====+====
- /// a | b
- /// ----+----
- /// c | d
- /// ```
- pub static ref FORMAT_NO_BORDER: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .column_separator('|')
- .build();
-
- /// A table with no external border and no line separation
- ///
- /// # Example
- /// ```text
- /// T1 | T2
- /// ----+----
- /// a | b
- /// c | d
- /// ```
- pub static ref FORMAT_NO_BORDER_LINE_SEPARATOR: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .column_separator('|')
- .build();
+ lazy_static! {
+ /// A line separator made of `-` and `+`
+ static ref MINUS_PLUS_SEP: LineSeparator = LineSeparator::new('-', '+', '+', '+');
+ /// A line separator made of `=` and `+`
+ static ref EQU_PLUS_SEP: LineSeparator = LineSeparator::new('=', '+', '+', '+');
+
+ /// Default table format
+ ///
+ /// # Example
+ /// ```text
+ /// +----+----+
+ /// | T1 | T2 |
+ /// +====+====+
+ /// | a | b |
+ /// +----+----+
+ /// | d | c |
+ /// +----+----+
+ /// ```
+ pub static ref FORMAT_DEFAULT: TableFormat = FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Title, *EQU_PLUS_SEP)
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .padding(1, 1)
+ .build();
+
+ /// Similar to `FORMAT_DEFAULT` but without special separator after title line
+ ///
+ /// # Example
+ /// ```text
+ /// +----+----+
+ /// | T1 | T2 |
+ /// +----+----+
+ /// | a | b |
+ /// +----+----+
+ /// | c | d |
+ /// +----+----+
+ /// ```
+ pub static ref FORMAT_NO_TITLE: TableFormat = FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Title, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .padding(1, 1)
+ .build();
+
+ /// With no line separator, but with title separator
+ ///
+ /// # Example
+ /// ```text
+ /// +----+----+
+ /// | T1 | T2 |
+ /// +----+----+
+ /// | a | b |
+ /// | c | d |
+ /// +----+----+
+ /// ```
+ pub static ref FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separator(LinePosition::Title, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .padding(1, 1)
+ .build();
+
+ /// With no line or title separator
+ ///
+ /// # Example
+ /// ```text
+ /// +----+----+
+ /// | T1 | T2 |
+ /// | a | b |
+ /// | c | d |
+ /// +----+----+
+ /// ```
+ pub static ref FORMAT_NO_LINESEP: TableFormat = FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .padding(1, 1)
+ .build();
+
+ /// No column separator
+ ///
+ /// # Example
+ /// ```text
+ /// --------
+ /// T1 T2
+ /// ========
+ /// a b
+ /// --------
+ /// d c
+ /// --------
+ /// ```
+ pub static ref FORMAT_NO_COLSEP: TableFormat = FormatBuilder::new()
+ .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Title, *EQU_PLUS_SEP)
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .padding(1, 1)
+ .build();
+
+ /// Format for printing a table without any separators (only alignment)
+ ///
+ /// # Example
+ /// ```text
+ /// T1 T2
+ /// a b
+ /// d c
+ /// ```
+ pub static ref FORMAT_CLEAN: TableFormat = FormatBuilder::new()
+ .padding(1, 1)
+ .build();
+
+ /// Format for a table with only external borders and title separator
+ ///
+ /// # Example
+ /// ```text
+ /// +--------+
+ /// | T1 T2 |
+ /// +========+
+ /// | a b |
+ /// | c d |
+ /// +--------+
+ /// ```
+ pub static ref FORMAT_BORDERS_ONLY: TableFormat = FormatBuilder::new()
+ .padding(1, 1)
+ .separator(LinePosition::Title, *EQU_PLUS_SEP)
+ .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Top, *MINUS_PLUS_SEP)
+ .borders('|')
+ .build();
+
+ /// A table with no external border
+ ///
+ /// # Example
+ /// ```text
+ /// T1 | T2
+ /// ====+====
+ /// a | b
+ /// ----+----
+ /// c | d
+ /// ```
+ pub static ref FORMAT_NO_BORDER: TableFormat = FormatBuilder::new()
+ .padding(1, 1)
+ .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
+ .separator(LinePosition::Title, *EQU_PLUS_SEP)
+ .column_separator('|')
+ .build();
+
+ /// A table with no external border and no line separation
+ ///
+ /// # Example
+ /// ```text
+ /// T1 | T2
+ /// ----+----
+ /// a | b
+ /// c | d
+ /// ```
+ pub static ref FORMAT_NO_BORDER_LINE_SEPARATOR: TableFormat = FormatBuilder::new()
+ .padding(1, 1)
+ .separator(LinePosition::Title, *MINUS_PLUS_SEP)
+ .column_separator('|')
+ .build();
}
}