Skip to content

Commit c32f236

Browse files
committed
refactor: eliminate duplicate logic in cesr-proof mod
1 parent ab6d1fc commit c32f236

10 files changed

Lines changed: 196 additions & 220 deletions

File tree

cesr/src/cesr_proof/codes.rs

Lines changed: 0 additions & 107 deletions
This file was deleted.

cesr/src/cesr_proof/mod.rs

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use base64::URL_SAFE;
22
use serde::Deserialize;
33

4-
use crate::derivation_code::DerivationCode;
5-
6-
use self::codes::MaterialPathCode;
4+
use crate::{
5+
conversion::from_bytes_to_text,
6+
variable_length::{
7+
LeadBytes, SmallVariableLengthCode, VariableLengthCode, VariableLengthPrimitive,
8+
},
9+
};
710

811
use super::error::Error;
912

10-
pub mod codes;
11-
pub mod parsers;
12-
1313
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
1414
pub struct MaterialPath {
1515
lead_bytes: usize,
@@ -18,36 +18,47 @@ pub struct MaterialPath {
1818
}
1919

2020
impl MaterialPath {
21-
pub fn new(pt: MaterialPathCode, path: String) -> Self {
22-
let lead_bytes = pt.lead_bytes_len();
21+
pub fn new(lb: LeadBytes, path: String) -> Self {
22+
let lead_bytes = match lb {
23+
LeadBytes::Zero => 0,
24+
LeadBytes::One => 1,
25+
LeadBytes::Two => 2,
26+
};
2327
MaterialPath {
2428
lead_bytes,
2529
base: path,
2630
}
2731
}
2832

29-
pub fn to_path(path: String) -> Self {
30-
let len_modulo = path.len() % 4;
31-
let leading_bytes = (3 - (len_modulo % 3)) % 3;
33+
fn lead_bytes(&self) -> LeadBytes {
34+
match self.lead_bytes {
35+
0 => LeadBytes::Zero,
36+
1 => LeadBytes::One,
37+
2 => LeadBytes::Two,
38+
_ => panic!("Invalid lead bytes"),
39+
}
40+
}
3241

33-
// fill input string with A
34-
let b64path = match len_modulo {
35-
0 => path,
36-
n => ["A".repeat(4 - n), path].join(""),
37-
};
42+
pub fn create_from_str(path: String) -> Self {
43+
let primitive =
44+
VariableLengthPrimitive::create_from_str(SmallVariableLengthCode::Base64String, &path);
3845

3946
Self {
40-
base: b64path,
41-
lead_bytes: leading_bytes,
47+
base: from_bytes_to_text(primitive.value()),
48+
lead_bytes: primitive.code().lead_bytes() as usize,
4249
}
4350
}
4451

4552
pub fn to_cesr(&self) -> String {
4653
let decoded_base = base64::decode_config(&self.base, URL_SAFE).unwrap();
4754

4855
let size = decoded_base.len() / 3;
49-
let code = MaterialPathCode::new(self.lead_bytes, size as u16).unwrap();
50-
[code.to_str(), self.base.clone()].join("")
56+
let code = VariableLengthCode::Small {
57+
lb: self.lead_bytes(),
58+
code: SmallVariableLengthCode::Base64String,
59+
length: size as u16,
60+
};
61+
[code.to_cesr(), self.base.clone()].join("")
5162
}
5263

5364
pub fn to_raw(&self) -> Result<Vec<u8>, Error> {
@@ -59,22 +70,34 @@ impl MaterialPath {
5970

6071
#[test]
6172
pub fn test_path_to_cesr() -> Result<(), Error> {
62-
assert_eq!(MaterialPath::to_path("-".into()).to_cesr(), "6AABAAA-");
63-
assert_eq!(MaterialPath::to_path("-A".into()).to_cesr(), "5AABAA-A");
64-
assert_eq!(MaterialPath::to_path("-A-".into()).to_cesr(), "4AABA-A-");
65-
assert_eq!(MaterialPath::to_path("-A-B".into()).to_cesr(), "4AAB-A-B");
6673
assert_eq!(
67-
MaterialPath::to_path("-a-b-c".into()).to_cesr(),
74+
MaterialPath::create_from_str("-".into()).to_cesr(),
75+
"6AABAAA-"
76+
);
77+
assert_eq!(
78+
MaterialPath::create_from_str("-A".into()).to_cesr(),
79+
"5AABAA-A"
80+
);
81+
assert_eq!(
82+
MaterialPath::create_from_str("-A-".into()).to_cesr(),
83+
"4AABA-A-"
84+
);
85+
assert_eq!(
86+
MaterialPath::create_from_str("-A-B".into()).to_cesr(),
87+
"4AAB-A-B"
88+
);
89+
assert_eq!(
90+
MaterialPath::create_from_str("-a-b-c".into()).to_cesr(),
6891
"5AACAA-a-b-c"
6992
);
7093

7194
assert_eq!(
72-
MaterialPath::to_path("-field0".into()).to_cesr(),
95+
MaterialPath::create_from_str("-field0".into()).to_cesr(),
7396
"4AACA-field0"
7497
);
7598

7699
assert_eq!(
77-
MaterialPath::to_path("-field0-field1-field3".into()).to_cesr(),
100+
MaterialPath::create_from_str("-field0-field1-field3".into()).to_cesr(),
78101
"6AAGAAA-field0-field1-field3"
79102
);
80103

cesr/src/cesr_proof/parsers.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

cesr/src/group/parsers.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use nom::{
77
sequence::tuple,
88
};
99

10-
#[cfg(feature = "cesr-proof")]
11-
use crate::cesr_proof::parsers::material_path;
1210
use crate::{
1311
primitives::{
1412
codes::{
@@ -80,10 +78,29 @@ pub fn parse_group(stream: &str) -> nom::IResult<&str, Group> {
8078
// n * 4 is all path and attachments length (?)
8179
match nom::bytes::complete::take(n * 4)(rest) {
8280
Ok((rest, total)) => {
83-
let (extra, mp) = material_path(total)?;
81+
use crate::variable_length::{
82+
variable_length_value, SmallVariableLengthCode, VariableLengthCode,
83+
};
84+
let (extra, mp) = variable_length_value(total)?;
85+
let material_path = match mp.code() {
86+
VariableLengthCode::Small {
87+
code: SmallVariableLengthCode::Base64String,
88+
lb,
89+
length: _,
90+
} => {
91+
use crate::{cesr_proof::MaterialPath, conversion::from_bytes_to_text};
92+
93+
let value = from_bytes_to_text(&mp.value());
94+
MaterialPath::new(lb.clone(), value)
95+
}
96+
_ => return Err(nom::Err::Error(make_error(total, ErrorKind::IsNot))),
97+
};
8498
let (_extra, attachment) = many0(parse_group)(extra)?;
8599

86-
Ok((rest, Group::PathedMaterialQuadruplet(mp, attachment)))
100+
Ok((
101+
rest,
102+
Group::PathedMaterialQuadruplet(material_path, attachment),
103+
))
87104
}
88105
Err(e) => Err(e),
89106
}?
@@ -122,7 +139,7 @@ fn test_pathed_material() {
122139

123140
let attached_str = "-PAZ5AABAA-a-KABAAFjjD99-xy7J0LGmCkSE_zYceED5uPF4q7l8J23nNQ64U-oWWulHI5dh3cFDWT4eICuEQCALdh8BO5ps-qx0qBA";
124141
let (_rest, attached_material) = parse_group(attached_str).unwrap();
125-
let expected_path = MaterialPath::to_path("-a".into());
142+
let expected_path = MaterialPath::create_from_str("-a".into());
126143
if let Group::PathedMaterialQuadruplet(material_path, groups) = attached_material {
127144
assert_eq!(material_path, expected_path);
128145
assert_eq!(groups.len(), 1)

cesr/src/primitives/codes/rand_128.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::str::FromStr;
22

3-
use crate::{
4-
conversion::from_bytes_to_text, derivation_code::DerivationCode, error::Error,
5-
primitives::parsers::parse_primitive,
6-
};
3+
use crate::{conversion::from_bytes_to_text, derivation_code::DerivationCode, error::Error};
74

85
// Random salt, seed, nonce, private key, or sequence number of length 128 bits
96
#[derive(PartialEq, Eq, Debug, Clone)]

cesr/src/primitives/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ pub mod codes;
22
pub mod parsers;
33
use chrono::{DateTime, FixedOffset};
44

5-
use crate::{
6-
conversion::from_bytes_to_text,
7-
primitives::codes::{rand_128::Rand128Code, seed::SeedCode},
8-
};
5+
use crate::{conversion::from_bytes_to_text, primitives::codes::rand_128::Rand128Code};
96

107
use self::codes::{
118
attached_signature_code::AttachedSignatureCode, basic::Basic, self_addressing::SelfAddressing,

cesr/src/primitives/parsers.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,14 @@ pub fn anchoring_event_seal(s: &str) -> nom::IResult<&str, AnchoringEventSeal> {
8989
#[cfg(test)]
9090
pub mod tests {
9191

92-
#[cfg(feature = "cesr-proof")]
93-
use crate::cesr_proof::{parsers::material_path, MaterialPath};
9492
use crate::{
9593
parse_one,
9694
primitives::{
9795
codes::{
9896
attached_signature_code::{AttachedSignatureCode, Index},
9997
basic::Basic,
100-
rand_128::Rand128Code,
10198
self_addressing::SelfAddressing,
10299
self_signing::SelfSigning,
103-
PrimitiveCode,
104100
},
105101
parsers::{parse_primitive, serial_number_parser, timestamp_parser},
106102
},
@@ -247,14 +243,6 @@ pub mod tests {
247243
);
248244
}
249245

250-
#[cfg(feature = "cesr-proof")]
251-
#[test]
252-
fn test_path_parse() {
253-
let attached_str = "6AABAAA-";
254-
let (_rest, attached_material) = material_path(attached_str).unwrap();
255-
assert_eq!(attached_material, MaterialPath::to_path("-".into()));
256-
}
257-
258246
#[test]
259247
fn parse_tag() {
260248
use crate::derivation_code::DerivationCode;

cesr/src/universal_codes.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,23 @@ impl DerivationCode for UniversalGroupCode {
200200
CustomizableCode::GenericPipeline => 2,
201201
CustomizableCode::Attachments => 2,
202202
},
203-
UniversalGroupCode::OverrideNotAllowed { code, quadlets } => 2,
203+
UniversalGroupCode::OverrideNotAllowed { .. } => 2,
204204
}
205205
}
206206

207207
fn soft_size(&self) -> usize {
208208
match self {
209209
UniversalGroupCode::Genus(_genus_count_code) => 0,
210-
UniversalGroupCode::OverrideAllowed {
211-
code: _,
212-
quadlets: _,
213-
} => 2,
214-
UniversalGroupCode::OverrideNotAllowed { code, quadlets } => 2,
210+
UniversalGroupCode::OverrideAllowed { .. } => 2,
211+
UniversalGroupCode::OverrideNotAllowed { .. } => 2,
215212
}
216213
}
217214

218215
fn value_size(&self) -> usize {
219216
match self {
220217
UniversalGroupCode::Genus(_) => 0,
221218
UniversalGroupCode::OverrideAllowed { quadlets, .. } => *quadlets as usize,
222-
UniversalGroupCode::OverrideNotAllowed { code, quadlets } => *quadlets as usize,
219+
UniversalGroupCode::OverrideNotAllowed { quadlets, .. } => *quadlets as usize,
223220
}
224221
}
225222

0 commit comments

Comments
 (0)