diff --git a/Cargo.toml b/Cargo.toml index 9bc0205..84d037e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brcode" -version = "1.1.0" +version = "1.1.1" authors = ["Julia Naomi "] edition = "2018" description = "Crate to parse and emit BR Codes" diff --git a/README.md b/README.md index 70b44fe..1d77216 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A crate to parse and emit [PIX BR Code](https://www.bcb.gov.br/content/estabilid ```toml [dependencies] -brcode = "1.1.0" +brcode = "1.1.1" ``` Shellscript to get files from release: diff --git a/benches/both_ways.rs b/benches/both_ways.rs index 243d7ba..a527adb 100644 --- a/benches/both_ways.rs +++ b/benches/both_ways.rs @@ -10,7 +10,7 @@ fn brcode_benchmark(c: &mut Criterion) { fn vec_benchmark(c: &mut Criterion) { let code = code(); - c.bench_function("using vec", |b| b.iter(|| to_string(from_str(&code)))); + c.bench_function("using vec", |b| b.iter(|| to_string(&from_str(&code)))); } criterion_group!(benches, brcode_benchmark, vec_benchmark); diff --git a/clj-brcode/libbrcode.dylib b/clj-brcode/libbrcode.dylib index 8a163ba..e02c2a0 100755 Binary files a/clj-brcode/libbrcode.dylib and b/clj-brcode/libbrcode.dylib differ diff --git a/clj-brcode/libbrcode.so b/clj-brcode/libbrcode.so index 832abf6..39738ab 100644 Binary files a/clj-brcode/libbrcode.so and b/clj-brcode/libbrcode.so differ diff --git a/libbrcode.dylib b/libbrcode.dylib index 8a163ba..e02c2a0 100755 Binary files a/libbrcode.dylib and b/libbrcode.dylib differ diff --git a/libbrcode.so b/libbrcode.so index 832abf6..39738ab 100644 Binary files a/libbrcode.so and b/libbrcode.so differ diff --git a/node-brcode/libbrcode.dylib b/node-brcode/libbrcode.dylib index 8a163ba..e02c2a0 100755 Binary files a/node-brcode/libbrcode.dylib and b/node-brcode/libbrcode.dylib differ diff --git a/node-brcode/libbrcode.so b/node-brcode/libbrcode.so index 832abf6..39738ab 100644 Binary files a/node-brcode/libbrcode.so and b/node-brcode/libbrcode.so differ diff --git a/src/aux.rs b/src/aux.rs index f8afb5e..c4cadff 100644 --- a/src/aux.rs +++ b/src/aux.rs @@ -26,7 +26,7 @@ impl Data { pub fn to_hash(&self) -> HashMap { match self { - Data::Hash(map) => (map.0).clone().to_owned(), + Data::Hash(map) => (map.0).to_owned(), _ => HashMap::new(), } } diff --git a/src/emit.rs b/src/emit.rs index 22584b4..be84d38 100644 --- a/src/emit.rs +++ b/src/emit.rs @@ -1,6 +1,6 @@ use crate::parse::Data; -pub(crate) fn emit(code: Vec<(usize, Data)>) -> String { +pub(crate) fn emit(code: &[(usize, Data)]) -> String { code.iter() .map(|e| match &e.1 { Data::Single(s) => format!("{:02}{:02}{}", e.0, s.len(), s), @@ -29,7 +29,7 @@ mod test { fn emits_same_data_as_receives() { let code = "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"; let parsed_data = from_str(code); - let emited = emit(parsed_data); + let emited = emit(&parsed_data); assert_eq!(code, emited); } @@ -38,7 +38,7 @@ mod test { fn helloworld_in_tag_00() { let code = "26062602oi"; let parsed_data = from_str(code); - let emited = emit(parsed_data); + let emited = emit(&parsed_data); assert_eq!(code, emited); } diff --git a/src/lib.rs b/src/lib.rs index 7d3c7e6..6578cbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,8 +11,8 @@ pub fn from_str(code: &str) -> Vec<(usize, parse::Data)> { parse::parse(code, 99) } -pub fn to_string(code: Vec<(usize, parse::Data)>) -> String { - emit::emit(code) +pub fn to_string(code: &[(usize, parse::Data)]) -> String { + emit::emit(&code) } pub fn brcode_to_string(code: BrCode) -> String { @@ -35,7 +35,7 @@ fn chars_to_string(pointer: *const c_char) -> String { str::from_utf8(slice).unwrap().to_string() } -fn to_c_char(s: String) -> *const c_char { +fn to_c_char(s: &str) -> *const c_char { let cs = CString::new(s.as_bytes()).unwrap(); let ptr = cs.as_ptr(); mem::forget(cs); @@ -47,7 +47,7 @@ fn to_c_char(s: String) -> *const c_char { pub extern "C" fn edn_from_brcode(edn: *const c_char) -> *const c_char { let edn_str = chars_to_string(edn); let brcode = str_to_brcode(&edn_str); - to_c_char(edn_rs::to_string(brcode)) + to_c_char(&edn_rs::to_string(brcode)) } #[no_mangle] @@ -55,7 +55,7 @@ pub extern "C" fn edn_to_brcode(edn: *const c_char) -> *const c_char { let edn_str = chars_to_string(edn); let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap(); - to_c_char(brcode_to_string(brcode)) + to_c_char(&brcode_to_string(brcode)) } // Json @@ -63,7 +63,7 @@ pub extern "C" fn edn_to_brcode(edn: *const c_char) -> *const c_char { pub extern "C" fn json_from_brcode(json: *const c_char) -> *const c_char { let json_str = chars_to_string(json); let brcode = str_to_brcode(&json_str); - to_c_char(serde_json::to_string(&brcode).unwrap_or("error".to_string())) + to_c_char(&serde_json::to_string(&brcode).unwrap_or_else(|_| "error".to_string())) } #[no_mangle] @@ -71,5 +71,5 @@ pub extern "C" fn json_to_brcode(edn: *const c_char) -> *const c_char { let edn_str = chars_to_string(edn); let brcode: BrCode = serde_json::from_str(&edn_str).unwrap(); - to_c_char(brcode_to_string(brcode)) + to_c_char(&brcode_to_string(brcode)) } diff --git a/src/model.rs b/src/model.rs index 092ee5b..0006651 100644 --- a/src/model.rs +++ b/src/model.rs @@ -54,14 +54,14 @@ impl From> for BrCode { fn from(code: Vec<(usize, Data)>) -> Self { let hash = HashBrCode::new(code).0; let merchant_information = (26usize..=51usize) - .map(|i| (i, hash.get(&i).clone())) + .map(|i| (i, hash.get(&i))) .filter_map(|e| { e.1.map(|d| { Some(MerchantInfo { id: e.0, info: { let hm = d.to_hash(); - let mut keys = hm.keys().map(|k| *k).collect::>(); + let mut keys = hm.keys().copied().collect::>(); keys.sort(); keys.into_iter() .map(|idx| Info { @@ -75,14 +75,14 @@ impl From> for BrCode { }) .collect::>(); let templates = (80usize..=99usize) - .map(|i| (i, hash.get(&i).clone())) + .map(|i| (i, hash.get(&i))) .filter_map(|e| { e.1.map(|d| { Some(Template { id: e.0, info: { let hm = d.to_hash(); - let mut keys = hm.keys().map(|k| *k).collect::>(); + let mut keys = hm.keys().copied().collect::>(); keys.sort(); keys.into_iter() .map(|idx| Info { @@ -99,12 +99,12 @@ impl From> for BrCode { BrCode { payload_version: hash[&0usize].to_str().parse().unwrap(), initiation_methos: hash.get(&1usize).map(|e| e.to_str().parse().unwrap()), - merchant_account_information: hash.get(&4usize).map(|e| e.to_str()), + merchant_account_information: hash.get(&4usize).map(crate::aux::Data::to_str), merchant_information: merchant_information, merchant_category_code: hash[&52usize].to_str().parse().unwrap(), merchant_name: hash[&59usize].to_str(), merchant_city: hash[&60usize].to_str(), - postal_code: hash.get(&61usize).map(|e| e.to_str()), + postal_code: hash.get(&61usize).map(crate::aux::Data::to_str), currency: hash[&53usize].to_str(), amount: hash.get(&54usize).map(|e| e.to_str().parse().unwrap()), country_code: hash[&58usize].to_str(), diff --git a/src/parse.rs b/src/parse.rs index 8d88df8..213e535 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -27,8 +27,7 @@ pub(crate) fn parse(code: &str, max: usize) -> Vec<(usize, Data)> { (0usize..=max) .filter_map(|_| parse_code(&mut chars)) .map(|code| match code.0 { - 26..=51 => (code.0, Data::Vector(inner_parse(&code.1, 99))), - 80..=99 => (code.0, Data::Vector(inner_parse(&code.1, 99))), + 26..=51 | 80..=98 => (code.0, Data::Vector(inner_parse(&code.1, 99))), 62 => (code.0, Data::Vector(inner_parse(&code.1, 25))), _ => (code.0, Data::Single(code.1)), }) diff --git a/tests/lib.rs b/tests/lib.rs index 5203b12..115792c 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -10,7 +10,7 @@ fn test_from_str() { #[test] fn test_to_string() { - let actual = brcode::to_string(data_expected()); + let actual = brcode::to_string(&data_expected()); let expected = code(); assert_eq!(actual, expected); @@ -19,7 +19,7 @@ fn test_to_string() { #[test] fn assert_both_ways() { let from = brcode::from_str(&dynamic_code()); - let to = brcode::to_string(from); + let to = brcode::to_string(&from); assert_eq!(to, dynamic_code()) }