cargo-treemap 0.1.0

Interactive treemap analyzer for Rust binary size and dependency API usage — like webpack-bundle-analyzer, for cargo.
//! Self-contained HTML for a diff: a treemap of changes, sized by change magnitude and
//! colored red (grew) or green (shrank).

use super::safe_json;
use crate::cli::Metric;
use crate::diff::DiffNode;
use crate::model::Meta;
use std::path::Path;

pub fn render(diff: &DiffNode, meta: &Meta, base_path: &Path, metric: Metric) -> String {
    let tmpl = include_str!("../../templates/diff.html");
    let css = include_str!("../../templates/treemap.css");
    let js = include_str!("../../templates/diff.js");
    let base = base_path
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| base_path.display().to_string())
        .replace('\\', "\\\\")
        .replace('"', "\\\"");

    tmpl.replace("/*__CSS__*/", css)
        .replace("/*__JS__*/", js)
        .replace("__DIFF_META__", &safe_json(meta))
        .replace("__DIFF_BASE__", &base)
        .replace("__DIFF_DATA__", &safe_json(diff))
        .replace("__DIFF_METRIC__", metric.field())
}