Skip to main content

render_auto

Function render_auto 

Source
pub fn render_auto<T: Serialize>(
    template: &str,
    data: &T,
    theme: &Theme,
    mode: OutputMode,
) -> Result<String, RenderError>
Expand description

Auto-dispatches between template rendering and direct serialization.

This is the recommended function when you want to support both human-readable output (terminal, text) and machine-readable output (JSON, YAML, etc.). For structured modes like Json, the data is serialized directly, skipping template rendering entirely.

§Arguments

  • template - A minijinja template string (ignored for structured modes)
  • data - Any serializable data to render or serialize
  • theme - Theme definitions for the style filter (ignored for structured modes)
  • mode - Output mode determining the output format

§Example

use standout_render::{render_auto, Theme, OutputMode};
use console::Style;
use serde::Serialize;

#[derive(Serialize)]
struct Report { title: String, count: usize }

let theme = Theme::new().add("title", Style::new().bold());
let data = Report { title: "Summary".into(), count: 42 };

// Terminal output uses the template
let term = render_auto(
    r#"[title]{{ title }}[/title]: {{ count }}"#,
    &data,
    &theme,
    OutputMode::Text,
).unwrap();
assert_eq!(term, "Summary: 42");

// JSON output serializes directly
let json = render_auto(
    r#"[title]{{ title }}[/title]: {{ count }}"#,
    &data,
    &theme,
    OutputMode::Json,
).unwrap();
assert!(json.contains("\"title\": \"Summary\""));
assert!(json.contains("\"count\": 42"));