serializer.rs

997 B · rust · 33 lines

1use super::models::Glyph;2use super::names::name_of;3use mrlycore::Map;4use mrlycore::{json, Json};56/// Returns the glyph's rows as strings of '0' and '1'.7pub fn to_strings(glyph: &Glyph) -> Vec<String> {8    glyph.rows.clone()9}1011/// Returns the glyph's rows as lists of 0 and 1 bytes.12pub fn to_lists(glyph: &Glyph) -> Vec<Vec<u8>> {13    glyph14        .rows15        .iter()16        .map(|row| row.chars().map(|ch| (ch == '1') as u8).collect())17        .collect()18}1920/// Renders the glyphs to pretty JSON keyed by character, each entry carrying name, width, height and rows.21pub fn to_json(glyphs: &[Glyph]) -> String {22    let mut map = Map::new();23    for glyph in glyphs {24        let entry = json!({25            "name": name_of(glyph.char),26            "w": glyph.width(),27            "h": glyph.height(),28            "rows": glyph.rows.clone(),29        });30        map.insert(glyph.char.to_string(), entry);31    }32    serde_json::to_string_pretty(&Json::Object(map)).unwrap_or_default()33}