blob: d460441cdf14d2695339f868117794a9bc306942 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//! Internal only utilities
use std::io::{Error, ErrorKind, Write};
use std::str;
#[cfg(any(unix, macos))]
pub static NEWLINE: &'static [u8] = b"\n";
#[cfg(windows)]
pub static NEWLINE: &'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) -> &str {
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(());
}
}
|