//! This example is based on the original mustache crate's example, utilizing serde to generate `Data`.

use std::collections::HashMap;

use mustache2::render::{RenderManager, SourceCache, provider::StaticProvider};
use serde::Serialize;

#[derive(Serialize)]
struct Planet {
    name: &'static str,
}

fn main() {
    let cache = SourceCache::default();
    let mut renderer = RenderManager::new(
        // You can use `FsProvider` to read files off the disk.
        StaticProvider(HashMap::from([("template", "Hello, {{name}}!\n")])),
        &cache,
    );
    let mut out = String::new();

    // Render the template, saying "Hello, Mercury!".
    let mercury = Planet { name: "Mercury" };
    if let Err(e) = renderer.render_serde_to(&mut out, "template", mercury) {
        renderer.display_error(e);
        return;
    }

    // Render it again, saying "Hello, Venus!".
    let venus = Planet { name: "Venus" };
    if let Err(e) = renderer.render_serde_to(&mut out, "template", venus) {
        renderer.display_error(e);
        return;
    }

    assert_eq!(out, "Hello, Mercury!\nHello, Venus!\n");
}
