qred 0.1.2

A fast, light-footprint native QR code generator with custom color styling.
Documentation
use crate::gf28::GF28;
use crate::matrix::Matrix;
use crate::mode_encoder::prepare_data;
use crate::qr_spec::EcLevel;
use crate::reed::{interleave_blocks, BlockSpec, ReedSolomon};

fn generate_qr_code(text: &str, ec_level: EcLevel) -> Result<String, &'static str> {
    // 1. Encode data payload into bit buffer and get QR Spec
    let (buf, spec) = prepare_data(text, Some(ec_level))?;
    let data_bytes = buf.to_bytes();

    // 2. Initialize Galois Field and Reed-Solomon engine
    let gf = GF28::new();
    let rs = ReedSolomon::new(&gf);

    // 3. Construct BlockSpec for interleaving
    // (Note: For Versions 1-2, g1_blocks = num_blocks and g2_blocks = 0)
    let block_spec = spec.to_block_spec();

    // 4. Interleave data and generate EC bytes
    let final_codewords = interleave_blocks(&data_bytes, &block_spec, &rs);

    // 5. Initialize Matrix, place data, apply optimal mask
    let mut matrix = Matrix::new(spec.version);
    matrix.place_data_bits(&final_codewords);

    let ec_str = match ec_level {
        EcLevel::L => "L",
        EcLevel::M => "M",
        EcLevel::Q => "Q",
        EcLevel::H => "H",
    };

    matrix.select_mask(ec_str);

    // 6. Render final matrix to ASCII
    Ok(matrix.render_ascii())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version_1_qr_generation() {
        let text = "https://amarch.vercel.app";
        let result = generate_qr_code(text, EcLevel::M);
        assert!(result.is_ok());

        let ascii_qr = result.unwrap();
        assert!(!ascii_qr.is_empty());
        println!("Generated QR Code:\n{}", ascii_qr);
    }

    #[test]
    fn test_numeric_mode_encoding() {
        let text = "1234567890";
        let result = generate_qr_code(text, EcLevel::L);
        assert!(result.is_ok());
    }
}