srcdmp 0.1.0-alpha.1

Amazing code snapshot tool and library
Documentation
use std::env;
use std::fs;
use std::path::Path;

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest = Path::new(&out_dir).join("tier2_table.rs");

    let ranges: &[(u32, u32)] = &[
        (0x00C0, 0x00FF), // Extended Latin (À–ÿ)
        (0x0391, 0x03C9), // Greek (Α–ω)
        (0x2200, 0x2230), // Mathematical operators (∀–∰)
        (0x2500, 0x257F), // Box drawing (─–╿)
        (0x2190, 0x21FF), // Arrows (←–⿿)
        (0x2080, 0x2089), // Subscripts (₀–₉)
        (0x2070, 0x2079), // Superscripts (⁰–⁹)
        (0x4E00, 0x4E7F), // Common CJK (first 128)
    ];

    let mut chars: Vec<char> = Vec::new();
    for &(start, end) in ranges {
        for codepoint in start..=end {
            if let Some(c) = char::from_u32(codepoint) {
                chars.push(c);
            }
        }
    }

    assert!(
        chars.len() <= 8192,
        "too many tier-2 characters: {}",
        chars.len()
    );

    let mut decode_entries = String::new();
    let mut encode_entries = String::new();

    for (index, &c) in chars.iter().enumerate() {
        decode_entries.push_str(&format!(
            "    table[{}] = Some('{}');\n",
            index,
            c.escape_unicode()
        ));
        encode_entries.push_str(&format!(
            "    map.insert('{}', {});\n",
            c.escape_unicode(),
            index
        ));
    }

    let code = format!(
        r"
/// The total number of entries in the SDMP-7 tier-2 table (8192).
pub const TIER2_SIZE: usize = 8192;

/// Build the tier-2 decode table: a `Vec<Option<char>>` where each
/// index corresponds to a 14-bit code read from the encoded stream.
///
/// Generated at build time from the codepoint ranges specified in
/// `build.rs`.
pub fn build_tier2_decode() -> Vec<Option<char>> {{
    let mut table = vec![None; TIER2_SIZE];
{decode_entries}    table
}}

/// Build the tier-2 encode map: a `HashMap<char, u16>` mapping
/// characters to their 14-bit tier-2 index.
///
/// Generated at build time from the codepoint ranges specified in
/// `build.rs`.
pub fn build_tier2_encode() -> std::collections::HashMap<char, u16> {{
    let mut map = std::collections::HashMap::new();
{encode_entries}    map
}}
"
    );

    fs::write(&dest, code).unwrap();

    println!("cargo:rerun-if-changed=build.rs");
}