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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Sailfish runtime

mod buffer;
pub mod escape;
mod macros;
mod render;
mod size_hint;

pub use buffer::*;
pub use render::*;
pub use size_hint::*;

use std::fmt;

#[doc(hidden)]
pub use crate::{render, render_escaped, render_noop, render_text};

/// The error type which is returned from template function
#[derive(Clone, Debug)]
pub struct RenderError {
    // currently RenderError simply wraps the fmt::Error
    inner: fmt::Error,
}

impl fmt::Display for RenderError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl std::error::Error for RenderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.inner)
    }
}

impl From<fmt::Error> for RenderError {
    #[inline]
    fn from(other: fmt::Error) -> Self {
        Self { inner: other }
    }
}

pub type RenderResult = Result<String, RenderError>;

pub struct Context {
    #[doc(hidden)]
    pub buf: Buffer,
}

impl Context {
    #[inline]
    pub fn into_result(self) -> RenderResult {
        Ok(self.buf.into_string())
    }
}