pix-brcode-parser 0.1.0

A Rust library for parsing and validating Brazilian PIX QR codes (BR Code) following EMV QRCPS standard
Documentation
//! Example: Parse PIX QR codes and display information

use pix_brcode_parser::{parse_brcode, BRCodeError};

fn main() {
    // Example 1: Static PIX QR code with amount
    let static_qr = "00020126580014br.gov.bcb.pix0136123e4567-e12b-12d1-a456-426614174000520400005303986540510.005802BR5913FULANO DE TAL6008BRASILIA62070503***630457B8";
    
    println!("=== PIX QR Code Parser Example ===\n");
    
    match parse_brcode(static_qr) {
        Ok(brcode) => {
            println!("✅ Successfully parsed PIX QR code!");
            println!();
            
            // Basic information
            println!("📋 Basic Information:");
            println!("  Type: {}", if brcode.is_static() { "Static" } else { "Dynamic" });
            println!("  PIX Key: {}", brcode.merchant_account_info.pix_key);
            println!("  Key Type: {:?}", brcode.pix_key_type());
            println!("  Merchant: {}", brcode.merchant_name);
            println!("  City: {}", brcode.merchant_city);
            println!();
            
            // Payment information
            println!("💰 Payment Information:");
            println!("  Currency: {} (BRL)", brcode.transaction_currency);
            if let Some(amount) = &brcode.transaction_amount {
                println!("  Amount: R$ {}", amount);
            } else {
                println!("  Amount: User defined");
            }
            println!();
            
            // Additional details
            if let Some(description) = &brcode.merchant_account_info.description {
                println!("📝 Description: {}", description);
            }
            
            if let Some(additional_data) = &brcode.additional_data {
                println!("📊 Additional Data:");
                if let Some(ref_label) = &additional_data.reference_label {
                    println!("  Reference: {}", ref_label);
                }
                if let Some(bill_number) = &additional_data.bill_number {
                    println!("  Bill Number: {}", bill_number);
                }
            }
            
            println!("🔒 Checksum: {} (Valid)", brcode.crc16);
        }
        Err(e) => {
            println!("❌ Failed to parse PIX QR code: {}", e);
            
            match e {
                BRCodeError::InvalidChecksum => {
                    println!("   The QR code has an invalid checksum. It may be corrupted.");
                }
                BRCodeError::InvalidGui(gui) => {
                    println!("   Expected PIX GUI 'br.gov.bcb.pix', got: {}", gui);
                }
                BRCodeError::MissingField(field) => {
                    println!("   Required field is missing: {}", field);
                }
                _ => {
                    println!("   See error details above.");
                }
            }
        }
    }
    
    println!("\n{}", "=".repeat(50));
    
    // Example 2: Parse multiple QR codes
    let test_cases = vec![
        ("Valid Email Key", "00020126440014br.gov.bcb.pix0122user@example.com520400005303986540505802BR5913MERCHANT NAME6008SAO PAULO62070503***63041234"),
        ("Invalid CRC", "00020126440014br.gov.bcb.pix0122user@example.com520400005303986540505802BR5913MERCHANT NAME6008SAO PAULO62070503***6304WXYZ"),
        ("Invalid GUI", "00020126500014invalid.gui.here0122user@example.com520400005303986540505802BR5913MERCHANT NAME6008SAO PAULO62070503***63041234"),
    ];
    
    println!("\n📋 Testing Multiple QR Codes:\n");
    
    for (description, qr_code) in test_cases {
        print!("{}: ", description);
        match parse_brcode(qr_code) {
            Ok(_) => println!("✅ Valid"),
            Err(e) => println!("❌ Invalid ({})", e),
        }
    }
    
    println!("\n💡 Tip: Use this parser in your payment applications to validate");
    println!("   and extract information from PIX QR codes safely!");
}