dubp_documents_parser/compact_text/
certifications.rs1use crate::*;
17
18pub fn parse_compact_certifications(
20 str_certs: &[&str],
21) -> Result<Vec<TextDocumentFormat<CertificationDocumentV10>>, ParseCompactDocError> {
22 let mut certifications: Vec<TextDocumentFormat<CertificationDocumentV10>> = Vec::new();
23 for certification in str_certs {
24 let certifications_datas: Vec<&str> = certification.split(':').collect();
25 if certifications_datas.len() == 4 {
26 certifications.push(TextDocumentFormat::Compact(
27 CompactCertificationDocumentV10 {
28 issuer: ed25519::PublicKey::from_base58(certifications_datas[0])
29 .map_err(ParseCompactDocError::Issuer)?,
30 target: ed25519::PublicKey::from_base58(certifications_datas[1])
31 .map_err(ParseCompactDocError::Target)?,
32 block_number: BlockNumber(
33 certifications_datas[2]
34 .parse()
35 .map_err(ParseCompactDocError::BlockNumber)?,
36 ),
37 signature: ed25519::Signature::from_base64(certifications_datas[3])
38 .map_err(ParseCompactDocError::Sig)?,
39 },
40 ));
41 }
42 }
43 Ok(certifications)
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use unwrap::unwrap;
50
51 #[test]
52 fn test_parse_compact_certifications() -> Result<(), ParseCompactDocError> {
53 let compact_certs_strs = &[
54 "5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ:3Rt4qTmHoLPn5z2FWGAXwQp9AqVokgo85f5N47D2Fosu:352364:462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg==",
55 "GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31:Aav6dYSbWiGZVSeunqYswZRcFkrobpk1NWw3PCjpD8Mz:346647:PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ==",
56 ];
57
58 let compact_certs = parse_compact_certifications(compact_certs_strs)?;
59
60 assert_eq!(
61 vec![
62 TextDocumentFormat::Compact(CompactCertificationDocumentV10 {
63 issuer: unwrap!(ed25519::PublicKey::from_base58("5bG4wxsFDpG3n7vtgDyv8jCC9h5pWjJdDxDDSE21RgZJ")),
64 target: unwrap!(ed25519::PublicKey::from_base58("3Rt4qTmHoLPn5z2FWGAXwQp9AqVokgo85f5N47D2Fosu")),
65 block_number: BlockNumber(352_364),
66 signature: unwrap!(ed25519::Signature::from_base64("462+UqY616pj2WU1M9/xLQIppfuT2CLruoPSGT8Frm1iKepp1fQ3iNk3b/Z6EaFJ3cFD4Eu2jMmgwsbcnVQXBg==")),
67 }),
68 TextDocumentFormat::Compact(CompactCertificationDocumentV10 {
69 issuer: unwrap!(ed25519::PublicKey::from_base58("GFShJBGAnXXNvWuWv2sBTc2jxPfuJLgB6sxunEj69i31")),
70 target: unwrap!(ed25519::PublicKey::from_base58("Aav6dYSbWiGZVSeunqYswZRcFkrobpk1NWw3PCjpD8Mz")),
71 block_number: BlockNumber(346_647),
72 signature: unwrap!(ed25519::Signature::from_base64("PPuXwwmL/Voc4Q+6NNKV31cfwlK07SC10m+u91RovPLj4Dn7F+452BucruiFZ190L8aB66RbiHByebE5kVD/DQ==")),
73 }),
74 ],
75 compact_certs,
76 );
77
78 Ok(())
79 }
80}