summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
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/utils.rs
parent7c3f7a0fe99a1feb3fc75f4557f66590104adc75 (diff)
Fixed table! doc
Code reorganisation : Created utils.rs private module
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs41
1 files changed, 41 insertions, 0 deletions
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