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
use crate::error::{Error, Result};
use askama_escape::{Escaper, MarkupDisplay};
use serde::Serialize;

/// Serialize to YAML (requires `serde_yaml` feature)
///
/// ## Errors
///
/// This will panic if `S`'s implementation of `Serialize` decides to fail,
/// or if `T` contains a map with non-string keys.
pub fn yaml<E: Escaper, S: Serialize>(e: E, s: S) -> Result<MarkupDisplay<E, String>> {
    match serde_yaml::to_string(&s) {
        Ok(s) => Ok(MarkupDisplay::new_safe(s, e)),
        Err(e) => Err(Error::from(e)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use askama_escape::Html;

    #[test]
    fn test_yaml() {
        assert_eq!(yaml(Html, true).unwrap().to_string(), "true\n");
        assert_eq!(yaml(Html, "foo").unwrap().to_string(), "foo\n");
        assert_eq!(yaml(Html, true).unwrap().to_string(), "true\n");
        assert_eq!(yaml(Html, "foo").unwrap().to_string(), "foo\n");
        assert_eq!(
            yaml(Html, &vec!["foo", "bar"]).unwrap().to_string(),
            "- foo\n- bar\n"
        );
    }
}