BR Code
A crate to parse and emit PIX BR Code.
Important Changes
- Version
1.2
has a small break for aBrCode
field. PR fixesmodel::BrCode
fieldinitiation_method
naming.
Usage
[]
= "1.3.0"
Build from source
- Install rustup.
make build-macos
for macos and ios files ormake build-linux
for linux and android files.- Files will be located at
target/release/libbrcode.*
,target/<target-platform>/release/libbrcode.so
.
Copy files from Github Release
Shellscript to get files from release:
So
| | | |
dylib
| | | |
Example
Parse String
use ;
str_to_brcode
use ;
brcode::to_string from Vec<(usize, Data)>
:
use ;
brcode::brcode_to_string for struct BrCode
:
use ;
Benchmark
from_str in benches/parse.rs
time: [15.734 us 15.758 us 15.782 us]
str_to_brcode in benches/to_brcode
time: [24.886 us 24.931 us 24.977 us]
edn_from_brcode in benches/to_brcode
time: [52.670 us 52.795 us 52.929 us]
json_from_brcode in benches/to_brcode
time: [28.229 us 28.284 us 28.339 us]
both-ways using BrCode
time: [33.238 us 33.555 us 33.924 us]
both-ways using Vec<(usize, Data)>
time: [22.867 us 22.958 us 23.107 us]
crc16_ccitt in benches/crc16
:
time: [3.0738 us 3.0825 us 3.0938 us]
FFI
Clojure FFI
BR Code as Edn call function FFI edn_from_brcode
or use clojar [clj-brcode "1.1.0-SNAPSHOT"]
. Example:
(ns example.core
(:require [clj-brcode.core :refer :all]))
(def code "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38")
(brcode->edn code)
; {:payload-version 1, :initiation-method nil, :merchant-information [{:id 26, :info [{:id 0, :info "BR.GOV.BCB.PIX"}, {:id 1, :info "123e4567-e12b-12d1-a456-426655440000"}]}, {:id 27, :info [{:id 0, :info "BR.COM.OUTRO"}, {:id 1, :info "0123456789"}]}], :merchant-category-code 0, :merchant-name "NOME DO RECEBEDOR", :merchant-city "BRASILIA", :postal-code "70074900", :currency "986", :amount 123.45, :country-code "BR", :field-template [{:reference-label "RP12345678-2019"}], :crc1610 "AD38", :templates [{:id 80, :info [{:id 0, :info "BR.COM.OUTRO"}, {:id 1, :info "0123.ABCD.3456.WXYZ"}]}]}
Input:
"00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"
Expected Edn:
{:payload-version 1,:initiation-method nil, :merchant-information [
{:id 26, :info [{ :id 0, :info "BR.GOV.BCB.PIX",}, {:id 1, :info "123e4567-e12b-12d1-a456-426655440000",}]},
{:id 27, :info [{ :id 0, :info "BR.COM.OUTRO",}, {:id 1, :info "0123456789",}]}
],:merchant-category-code 0, :merchant-name "NOME DO RECEBEDOR", :merchant-city "BRASILIA",
:postal-code "70074900", :currency "986", :amount 123.45, :country-code "BR",
:field-template [{ :reference-label "RP12345678-2019", }], :crc1610 "AD38", :templates [
{ :id 80, :info [{ :id 0, :info "BR.COM.OUTRO", },{ :id 1, :info "0123.ABCD.3456.WXYZ", }], }]
}
Edn as BR Code call function FFI edn_to_brcode
or use clojar [clj-brcode "1.1.0-SNAPSHOT"]
. Example:
(ns example.core
(:require [clj-brcode.core :refer :all]))
(def edn {:payload-version 1, :initiation-method nil, :merchant-information [{:id 26, :info [{:id 0, :info "BR.GOV.BCB.PIX"}, {:id 1, :info "123e4567-e12b-12d1-a456-426655440000"}]}, {:id 27, :info [{:id 0, :info "BR.COM.OUTRO"}, {:id 1, :info "0123456789"}]}], :merchant-category-code 0, :merchant-name "NOME DO RECEBEDOR", :merchant-city "BRASILIA", :postal-code "70074900", :currency "986", :amount 123.45, :country-code "BR", :field-template [{:reference-label "RP12345678-2019"}], :crc1610 "AD38", :templates [{:id 80, :info [{:id 0, :info "BR.COM.OUTRO"}, {:id 1, :info "0123.ABCD.3456.WXYZ"}]}]})
(brcode->edn edn)
; "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"
Other available functions:
json->brcode
brcode->json
crc16-ccitt
Clojure Benchmark with Criterium
brcode->edn
Evaluation count : 4644 in 6 samples of 774 calls.
Execution time mean : 131.416626 µs
Execution time std-deviation : 2.218919 µs
Execution time lower quantile : 130.073353 µs ( 2.5%)
Execution time upper quantile : 135.212868 µs (97.5%)
Overhead used : 8.079635 ns
edn->brcode
Evaluation count : 3816 in 6 samples of 636 calls.
Execution time mean : 157.407924 µs
Execution time std-deviation : 3.556917 µs
Execution time lower quantile : 154.338082 µs ( 2.5%)
Execution time upper quantile : 162.800564 µs (97.5%)
Overhead used : 8.102766 ns
(-> brcode brcode->edn edn->brcode)
Evaluation count : 1920 in 6 samples of 320 calls.
Execution time mean : 344.903181 µs
Execution time std-deviation : 26.518055 µs
Execution time lower quantile : 328.923528 µs ( 2.5%)
Execution time upper quantile : 390.059255 µs (97.5%)
Overhead used : 8.071450 ns
Node FFI
BR Code as Json call function parse
. Example:
const brcode = require;
const code = "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38";
console.log;
// {"payload_version":1,"initiation_method":null,
// "merchant_information":[
// {"id":26,"info":[{"id":0,"info":"BR.GOV.BCB.PIX"},{"id":1,"info":"123e4567-e12b-12d1-a456-426655440000"}]},
// {"id":27,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123456789"}]}
// ],
// "merchant_category_code":0,"merchant_name":"NOME DO RECEBEDOR","merchant_city":"BRASILIA","postal_code":"70074900",
// "currency":"986","amount":123.45,"country_code":"BR","field_template":[{"reference_label":"RP12345678-2019"}],
// "crc1610":"AD38","templates":[
// {"id":80,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123.ABCD.3456.WXYZ"}]}
// ]}
Input:
"00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"
Expected Json:
Json as BR Code call function emit
. Example:
const brcode = require;
const json = ;
console.log
// "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"
Other available functions:
crc16Ccitt
Node Benchmark with microbench
parse
emit
parse(emit(json))
Dart FFI
Parse
import 'package:dartbrcode/dartbrcode.dart';
final json = '{"payload_version":1,"initiation_method":null,"merchant_account_information":"12345678901234","merchant_information":[{"id":26,"info":[{"id":0,"info":"BR.GOV.BCB.PIX"},{"id":1,"info":"123e4567-e12b-12d1-a456-426655440000"}]},{"id":27,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123456789"}]}],"merchant_category_code":0,"merchant_name":"NOME DO RECEBEDOR","merchant_city":"BRASILIA","postal_code":"70074900","currency":"986","amount":123.45,"country_code":"BR","field_template":[{"reference_label":"RP12345678-2019"}],"crc1610":"AD38","templates":[{"id":80,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123.ABCD.3456.WXYZ"}]}]}';
void main() {
jsonToBrcode(json);
// '00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38'
}
Emit
import 'package:dartbrcode/dartbrcode.dart';
final brcode = '00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38';
void main() {
jsonFromBrcode(brcode);
// '{"payload_version":1,"initiation_method":null,"merchant_account_information":"12345678901234","merchant_information":[{"id":26,"info":[{"id":0,"info":"BR.GOV.BCB.PIX"},{"id":1,"info":"123e4567-e12b-12d1-a456-426655440000"}]},{"id":27,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123456789"}]}],"merchant_category_code":0,"merchant_name":"NOME DO RECEBEDOR","merchant_city":"BRASILIA","postal_code":"70074900","currency":"986","amount":123.45,"country_code":"BR","field_template":[{"reference_label":"RP12345678-2019"}],"crc1610":"AD38","templates":[{"id":80,"info":[{"id":0,"info":"BR.COM.OUTRO"},{"id":1,"info":"0123.ABCD.3456.WXYZ"}]}]}'
}
Other available functions:
crc16Ccitt
Benchmarks
- with
dart_benchmark
jsonToBrcode
For 100 runs: peak: 371 us, bottom: 048 us, avg: ~083 us
brcodeToJson
For 100 runs: peak: 327 us, bottom: 069 us, avg: ~101 us
- with
benchmark_harness
jsonToBrcode
For 10 runs: 207.51774227018055 us.
brcodeToJson
For 10 runs: 378.68780764861793 us.
Goals
- Parse BR Code String;
- Parse BR Code to
BrCode
struct; - Emit BR Code from
Vec<(usize, Data)>
; - Emit BR Code from
BrCode
struct; - CRC16_CCITT
- FFI