extern crate cuach_derive;
extern crate uuid;
extern crate v_htmlescape;
use v_htmlescape::HTMLEscape;
pub use cuach_derive::template;

pub trait Render {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error>;
    fn render(&self) -> Result<String, anyhow::Error> {
        let mut v = String::new();
        self.render_into(&mut v)?;
        Ok(v)
    }
}

impl Render for () {
    fn render_into<W: std::fmt::Write>(&self, _: &mut W) -> Result<(), anyhow::Error> {
        Ok(())
    }
}

impl<'a, R: Render> Render for &'a R {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok((*self).render_into(w)?)
    }
}

impl Render for usize {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for isize {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for i32 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for i64 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for u32 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for u64 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for f32 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl Render for f64 {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

impl<'a> Render for &'a str {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", HTMLEscape::new(self.as_bytes()))?)
    }
}

impl Render for String {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", HTMLEscape::new(self.as_bytes()))?)
    }
}

#[derive(Debug, Clone)]
pub struct PreEscaped<S: AsRef<str>>(pub S);

impl<S: AsRef<str>> Render for PreEscaped<S> {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self.0.as_ref())?)
    }
}

impl Render for uuid::Uuid {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        Ok(write!(w, "{}", self)?)
    }
}

use std::borrow::Borrow;
impl<'a> Render for std::borrow::Cow<'a, str> {
    fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), anyhow::Error> {
        let r: &str = self.borrow();
        r.render_into(w)
    }
}