basic_usage/
basic_usage.rs

1use promptparse::{
2    generate::{any_id, bill_payment, bot_barcode, slip_verify, true_money},
3    generate::{
4        AnyIdConfig, BillPaymentConfig, BotBarcodeConfig, ProxyType, SlipVerifyConfig,
5        TrueMoneyConfig,
6    },
7    parse, parse_barcode, validate,
8};
9
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    println!("PromptParse Rust Library Examples\n");
12
13    // Example 1: Parse QR Code
14    println!("1. Parsing QR Code:");
15    let qr_payload =
16        "00020101021229370016A0000006770101110113006680111111153037645802TH540520.15630442BE";
17    if let Some(qr) = parse(qr_payload, true, true) {
18        println!("   Parsed successfully!");
19        println!("   Tag count: {}", qr.get_tags().len());
20        if let Some(amount) = qr.get_tag_value("54", None) {
21            println!("   Amount: {amount}");
22        }
23    }
24
25    // Example 2: Generate PromptPay AnyID QR
26    println!("\n2. Generating PromptPay AnyID QR:");
27    let any_id_config = AnyIdConfig {
28        proxy_type: ProxyType::Msisdn,
29        target: "0812223333".to_string(),
30        amount: Some(100.50),
31    };
32    let any_id_qr = any_id(any_id_config)?;
33    println!("   Generated QR: {any_id_qr}");
34
35    // Example 3: Generate Bill Payment QR
36    println!("\n3. Generating Bill Payment QR:");
37    let bill_config = BillPaymentConfig {
38        biller_id: "1234567890123".to_string(),
39        amount: Some(500.0),
40        ref1: "CUSTOMER001".to_string(),
41        ref2: Some("INV001".to_string()),
42        ref3: None,
43    };
44    let bill_qr = bill_payment(bill_config)?;
45    println!("   Generated QR: {bill_qr}");
46
47    // Example 4: Generate TrueMoney QR
48    println!("\n4. Generating TrueMoney QR:");
49    let true_money_config = TrueMoneyConfig {
50        mobile_no: "0801111111".to_string(),
51        amount: Some(25.0),
52        message: Some("Coffee money".to_string()),
53    };
54    let true_money_qr = true_money(true_money_config);
55    println!("   Generated QR: {true_money_qr}");
56
57    // Example 5: Generate Slip Verify QR
58    println!("\n5. Generating Slip Verify QR:");
59    let slip_config = SlipVerifyConfig {
60        sending_bank: "014".to_string(),
61        trans_ref: "REF123456789".to_string(),
62    };
63    let slip_qr = slip_verify(slip_config);
64    println!("   Generated QR: {slip_qr}");
65
66    // Example 6: Generate BOT Barcode
67    println!("\n6. Generating BOT Barcode:");
68    let bot_config = BotBarcodeConfig {
69        biller_id: "099999999999990".to_string(),
70        ref1: "111222333444".to_string(),
71        ref2: Some("REF2".to_string()),
72        amount: Some(150.0),
73    };
74    let bot_code = bot_barcode(bot_config);
75    println!("   Generated Barcode: {bot_code}");
76
77    // Example 7: Parse and convert BOT Barcode
78    println!("\n7. Parsing and converting BOT Barcode:");
79    let barcode_payload = "|099999999999990\r111222333444\r\r0";
80    if let Some(barcode) = parse_barcode(barcode_payload) {
81        println!("   Parsed barcode successfully!");
82        println!("   Biller ID: {}", barcode.biller_id);
83        println!("   Ref1: {}", barcode.ref1);
84
85        if let Ok(qr_equivalent) = barcode.to_qr_tag30() {
86            println!("   Converted to QR: {qr_equivalent}");
87        }
88    }
89
90    // Example 8: Validate Slip Verify QR
91    println!("\n8. Validating Slip Verify QR:");
92    let slip_payload = "004100060000010103014022000111222233344ABCD125102TH910417DF";
93    if let Some(slip_data) = validate::slip_verify(slip_payload) {
94        println!("   Valid slip verify QR!");
95        println!("   Bank: {}", slip_data.sending_bank);
96        println!("   Transaction Ref: {}", slip_data.trans_ref);
97    }
98
99    println!("\nAll examples completed successfully!");
100    Ok(())
101}