Framework for embedding localizations into Rust types
//! Example showing how to style localized types

use anstyle::AnsiColor;
use icu_locale::locale;
use jiff::Timestamp;
use l10n_embed::style::Styled;
use l10n_embed::{Context, Localize};

fn main() {
    // Create some localizable data to be styled
    let bold_green: Styled<u64> = Styled::new(5_000_000).bold().color(AnsiColor::Green);
    let italic_blue: Styled<&'static str> = Styled::new("&str").italic().color(AnsiColor::Blue);
    let strikethrough_red: Styled<Timestamp> = Styled::new(Timestamp::UNIX_EPOCH)
        .strikethrough()
        .color(AnsiColor::Red);

    // Localize these strings, which adds colors (if enabled) and forwards to the stored type's implementation.
    let colors = [("enabled", true), ("disabled", false)];
    let styles: [(&str, Box<dyn Localize>); 3] = [
        ("Bold green", Box::new(bold_green)),
        ("Italic blue", Box::new(italic_blue)),
        ("Strikethrough red", Box::new(strikethrough_red)),
    ];

    let mut context = Context::new(locale!("en-US"), true);
    let mut buffer = String::new();

    // Demonstrate both with and without colors
    for (color_name, colors_enabled) in colors {
        context.use_colors = colors_enabled;
        println!("----- Colors {color_name} -----");

        for (name, style) in &styles {
            buffer.clear();
            style.localize(&context, &mut buffer);

            println!("{name}: {buffer}");
        }
    }
}