summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-01 23:28:19 +0200
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-06-01 23:28:19 +0200
commit1343ba39d7b1b13f038ea58a9536c51cf560c67f (patch)
treeab6840ac38eab9f904d1e97385d23915e0252158 /src
parent7c3f7a0fe99a1feb3fc75f4557f66590104adc75 (diff)
Fixed table! doc
Code reorganisation : Created utils.rs private module
Diffstat (limited to 'src')
-rw-r--r--src/cell.rs2
-rw-r--r--src/lib.rs51
-rw-r--r--src/row.rs2
-rw-r--r--src/utils.rs41
4 files changed, 50 insertions, 46 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 602db26..96ae27b 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -21,7 +21,7 @@ impl Cell {
if l > width {
width = l;
}
- };
+ }
return Cell {
content: content,
width: width
diff --git a/src/lib.rs b/src/lib.rs
index f71ef2d..4f5019c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,19 +1,15 @@
//! A formatted and aligned table printer written in rust
-use std::io::{stdout, Write, Error, ErrorKind};
+use std::io::{stdout, Write, Error};
use std::fmt;
-use std::str;
use std::string::ToString;
pub mod cell;
pub mod row;
+mod utils;
use row::Row;
use cell::Cell;
-
-#[cfg(any(unix, macos))]
-static LINEFEED: &'static [u8] = b"\n";
-#[cfg(windows)]
-static LINEFEED: &'static [u8] = b"\r\n";
+use utils::{StringWriter, LINEFEED};
/// A Struct representing a printable table
#[derive(Clone, Debug)]
@@ -165,45 +161,12 @@ impl fmt::Display for Table {
}
}
-/// Internal utility for writing data into a string
-struct StringWriter {
- string: String
-}
-
-impl StringWriter {
- /// Create a new `StringWriter`
- fn new() -> StringWriter {
- return StringWriter{string: String::new()};
- }
-
- /// Return a reference to the internally written `String`
- fn as_string(&self) -> &String {
- return &self.string;
- }
-}
-
-impl Write for StringWriter {
- fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
- let string = match str::from_utf8(data) {
- Ok(s) => s,
- Err(e) => return Err(Error::new(ErrorKind::Other, format!("Cannot decode utf8 string : {}", e)))
- };
- self.string.push_str(string);
- return Ok(data.len());
- }
-
- fn flush(&mut self) -> Result<(), Error> {
- // Nothing to do here
- return Ok(());
- }
-}
-
-/// Create a table filled with some values.
-///
+/// Create a table filled with some values
+///
/// All the arguments used for elements must implement the `std::string::ToString` trait
/// # Syntax
/// table!([Element1_ row1, Element2_ row1, ...], [Element1_row2, ...], ...);
-///
+///
/// # Example
/// ```
/// # #[macro_use] extern crate tabprint;
@@ -211,7 +174,7 @@ impl Write for StringWriter {
/// // Create a table initialized with some rows :
/// let tab = table!(["Element1", "Element2", "Element3"],
/// [1, 2, 3],
-/// ["A", "B", "C"]
+/// ["A", "B", "C"]
/// );
/// # drop(tab);
/// # }
diff --git a/src/row.rs b/src/row.rs
index 4bada7b..bf86f8a 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -1,7 +1,7 @@
//! This module contains definition of table rows stuff
use std::io::{Write, Error};
-use super::LINEFEED;
+use super::utils::LINEFEED;
use super::cell::Cell;
/// Represent a table row made of cells
diff --git a/src/utils.rs b/src/utils.rs
new file mode 100644
index 0000000..ab18d04
--- /dev/null
+++ b/src/utils.rs
@@ -0,0 +1,41 @@
+//! Internal only utilities
+use std::io::{Error, ErrorKind, Write};
+use std::str;
+
+#[cfg(any(unix, macos))]
+pub static LINEFEED: &'static [u8] = b"\n";
+#[cfg(windows)]
+pub static LINEFEED: &'static [u8] = b"\r\n";
+
+/// Internal utility for writing data into a string
+pub struct StringWriter {
+ string: String
+}
+
+impl StringWriter {
+ /// Create a new `StringWriter`
+ pub fn new() -> StringWriter {
+ return StringWriter{string: String::new()};
+ }
+
+ /// Return a reference to the internally written `String`
+ pub fn as_string(&self) -> &String {
+ return &self.string;
+ }
+}
+
+impl Write for StringWriter {
+ fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
+ let string = match str::from_utf8(data) {
+ Ok(s) => s,
+ Err(e) => return Err(Error::new(ErrorKind::Other, format!("Cannot decode utf8 string : {}", e)))
+ };
+ self.string.push_str(string);
+ return Ok(data.len());
+ }
+
+ fn flush(&mut self) -> Result<(), Error> {
+ // Nothing to do here
+ return Ok(());
+ }
+} \ No newline at end of file