num2phrase 0.1.0

Convert long number to a sequence of memorisable phrase with combination of short numbers
Documentation
use num2phrase::generation::{bigint_to_string, string_to_bigint};
use num2phrase::parser::{parse, translate};
use num_bigint::BigUint;
use std::io::{self, Write};


fn print_usage() {
    eprintln!("Usage:");
    eprintln!("  num2phrase encode");
    eprintln!("  num2phrase decode");
    eprintln!("You will be prompted for structure and value/phrase interactively.");
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() != 2 {
        print_usage();
        std::process::exit(1);
    }
    let mode = &args[1];

    let mut structure = String::new();
    let mut value = String::new();

    print!("Enter structure: ");
    io::stdout().flush().unwrap();
    io::stdin().read_line(&mut structure).expect("Failed to read structure");
    let structure = structure.trim();

    match mode.as_str() {
        "encode" => {
            print!("Enter number to encode: ");
            io::stdout().flush().unwrap();
            io::stdin().read_line(&mut value).expect("Failed to read value");
            let value = value.trim();

            let parsed = match parse(structure) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("Failed to parse structure: {}", e);
                    std::process::exit(1);
                }
            };
            let intermediate = match translate(&parsed) {
                Ok(i) => i,
                Err(e) => {
                    eprintln!("Failed to translate structure: {}", e);
                    std::process::exit(1);
                }
            };
            let value = match value.parse::<BigUint>() {
                Ok(v) => v,
                Err(_) => {
                    eprintln!("Input is not a valid number");
                    std::process::exit(1);
                }
            };
            let phrase = bigint_to_string(&intermediate, value);
            println!("{}", phrase);
        }
        "decode" => {
            print!("Enter phrase to decode: ");
            io::stdout().flush().unwrap();
            io::stdin().read_line(&mut value).expect("Failed to read value");
            let value = value.trim();

            let parsed = match parse(structure) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("Failed to parse structure: {}", e);
                    std::process::exit(1);
                }
            };
            let intermediate = match translate(&parsed) {
                Ok(i) => i,
                Err(e) => {
                    eprintln!("Failed to translate structure: {}", e);
                    std::process::exit(1);
                }
            };
            match string_to_bigint(&intermediate, value) {
                Ok(num) => println!("{}", num),
                Err(e) => {
                    eprintln!("Decode error: {}", e);
                    std::process::exit(1);
                }
            }
        }
        _ => {
            print_usage();
            std::process::exit(1);
        }
    }
}