Switch to new std::io module

This commit is contained in:
Chris Wong 2015-02-18 20:28:44 +13:00
parent 34662a2a56
commit 9b13557781
2 changed files with 31 additions and 23 deletions
maud/src
maud_macros/src

View file

@ -156,11 +156,11 @@
#![feature(io)] #![feature(io)]
use std::fmt; use std::fmt;
use std::old_io::{IoError, IoErrorKind, IoResult}; use std::io;
/// Escape an HTML value. /// Escape an HTML value.
pub fn escape(s: &str) -> String { pub fn escape(s: &str) -> String {
use std::fmt::Writer; use std::fmt::Write;
let mut buf = String::new(); let mut buf = String::new();
rt::Escaper { inner: &mut buf }.write_str(s).unwrap(); rt::Escaper { inner: &mut buf }.write_str(s).unwrap();
buf buf
@ -174,32 +174,40 @@ pub struct Markup<F> {
callback: F, callback: F,
} }
impl<F> Markup<F> where F: Fn(&mut fmt::Writer) -> fmt::Result { impl<F> Markup<F> where F: Fn(&mut fmt::Write) -> fmt::Result {
/// Render the markup to a `std::io::Writer`. /// Render the markup to a `std::io::Write`.
pub fn render(&self, w: &mut Writer) -> IoResult<()> { pub fn render<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
struct WriterWrapper<'a, 'b: 'a> { struct Adaptor<'a, W: ?Sized + 'a> {
inner: &'a mut (Writer + 'b), inner: &'a mut W,
error: io::Result<()>,
} }
impl<'a, 'b> fmt::Writer for WriterWrapper<'a, 'b> {
impl<'a, W: ?Sized + io::Write> fmt::Write for Adaptor<'a, W> {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
self.inner.write_str(s).map_err(|_| fmt::Error) match self.inner.write_all(s.as_bytes()) {
Ok(()) => Ok(()),
Err(e) => {
self.error = Err(e);
Err(fmt::Error)
},
}
} }
} }
self.render_fmt(&mut WriterWrapper { inner: w })
.map_err(|_| IoError { let mut output = Adaptor { inner: w, error: Ok(()) };
kind: IoErrorKind::OtherIoError, match self.render_fmt(&mut output) {
desc: "formatting error", Ok(()) => Ok(()),
detail: None, Err(_) => output.error,
}) }
} }
/// Render the markup to a `std::fmt::Writer`. /// Render the markup to a `std::fmt::Write`.
pub fn render_fmt(&self, w: &mut fmt::Writer) -> fmt::Result { pub fn render_fmt(&self, w: &mut fmt::Write) -> fmt::Result {
(self.callback)(w) (self.callback)(w)
} }
} }
impl<F> ToString for Markup<F> where F: Fn(&mut fmt::Writer) -> fmt::Result { impl<F> ToString for Markup<F> where F: Fn(&mut fmt::Write) -> fmt::Result {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let mut buf = String::new(); let mut buf = String::new();
self.render_fmt(&mut buf).unwrap(); self.render_fmt(&mut buf).unwrap();
@ -216,7 +224,7 @@ pub mod rt {
#[inline] #[inline]
pub fn make_markup<F>(f: F) -> Markup<F> pub fn make_markup<F>(f: F) -> Markup<F>
where F: Fn(&mut fmt::Writer) -> fmt::Result where F: Fn(&mut fmt::Write) -> fmt::Result
{ {
Markup { callback: f } Markup { callback: f }
} }
@ -227,15 +235,15 @@ pub mod rt {
/// ///
/// See <https://github.com/rust-lang/rust/issues/16617> /// See <https://github.com/rust-lang/rust/issues/16617>
#[inline] #[inline]
pub fn write_fmt<T: fmt::Display>(w: &mut fmt::Writer, value: T) -> fmt::Result { pub fn write_fmt<T: fmt::Display>(w: &mut fmt::Write, value: T) -> fmt::Result {
write!(w, "{}", value) write!(w, "{}", value)
} }
pub struct Escaper<'a, 'b: 'a> { pub struct Escaper<'a, 'b: 'a> {
pub inner: &'a mut (fmt::Writer + 'b), pub inner: &'a mut (fmt::Write + 'b),
} }
impl<'a, 'b> fmt::Writer for Escaper<'a, 'b> { impl<'a, 'b> fmt::Write for Escaper<'a, 'b> {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.chars() { for c in s.chars() {
try!(match c { try!(match c {

View file

@ -42,7 +42,7 @@ impl<'cx, 's> Renderer<'cx, 's> {
pub fn into_expr(self) -> P<Expr> { pub fn into_expr(self) -> P<Expr> {
let Renderer { cx, stmts, w } = self; let Renderer { cx, stmts, w } = self;
quote_expr!(cx, quote_expr!(cx,
::maud::rt::make_markup(|$w: &mut ::std::fmt::Writer| -> Result<(), ::std::fmt::Error> { ::maud::rt::make_markup(|$w: &mut ::std::fmt::Write| -> Result<(), ::std::fmt::Error> {
$stmts $stmts
Ok(()) Ok(())
})) }))