Skip to content

Commit 262c422

Browse files
AnonymousJontified
authored andcommitted
Make parse fields constants
1 parent 4a83392 commit 262c422

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

src/io/ascii/mod.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ use crate::{
99
pub use parser::ParseAsciiError;
1010
use parser::{Parser, Result};
1111

12+
const LOG_FIELD: &str = "log";
13+
const SIZE_FIELD: &str = "size";
14+
const LEAF_FIELD: &str = "leaf";
15+
const MESSAGE_FIELD: &str = "message";
16+
const VERSION_FIELD: &str = "version";
17+
const ROOT_HASH_FIELD: &str = "root_hash";
18+
const SIGNATURE_FIELD: &str = "signature";
19+
const NODE_HASH_FIELD: &str = "node_hash";
20+
const LEAF_INDEX_FIELD: &str = "leaf_index";
21+
const PUBLIC_KEY_FIELD: &str = "public_key";
22+
const COSIGNATURE_FIELD: &str = "cosignature";
23+
1224
impl SignedTreeHead {
1325
pub fn from_ascii(input: &str) -> Result<Self> {
1426
let mut p = Parser::new(input);
15-
let size: u64 = p.parse("size")?;
16-
let root_hash: Hash = p.parse("root_hash")?;
17-
let signature: Signature = p.parse("signature")?;
27+
let size: u64 = p.parse(SIZE_FIELD)?;
28+
let root_hash: Hash = p.parse(ROOT_HASH_FIELD)?;
29+
let signature: Signature = p.parse(SIGNATURE_FIELD)?;
1830
let mut cosignatures = Vec::new();
1931
while !p.at_end() {
2032
let (keyhash, timestamp, cosignature): (Hash, u64, Signature) =
21-
p.parse("cosignature")?;
33+
p.parse(COSIGNATURE_FIELD)?;
2234
cosignatures.push(WitnessCosignature {
2335
keyhash,
2436
timestamp,
@@ -36,13 +48,13 @@ impl SignedTreeHead {
3648
#[allow(unused_must_use)]
3749
pub fn to_ascii(&self) -> String {
3850
let mut ascii = String::new();
39-
writeln!(ascii, "size={}", self.size);
40-
writeln!(ascii, "root_hash={:x}", self.root_hash);
41-
writeln!(ascii, "signature={:x}", self.signature);
51+
writeln!(ascii, "{SIZE_FIELD}={}", self.size);
52+
writeln!(ascii, "{ROOT_HASH_FIELD}={:x}", self.root_hash);
53+
writeln!(ascii, "{SIGNATURE_FIELD}={:x}", self.signature);
4254
for cosig in self.cosignatures.iter() {
4355
writeln!(
4456
ascii,
45-
"cosignature={:x} {} {:x}",
57+
"{COSIGNATURE_FIELD}={:x} {} {:x}",
4658
cosig.keyhash, cosig.timestamp, cosig.cosignature
4759
);
4860
}
@@ -53,10 +65,10 @@ impl SignedTreeHead {
5365
impl InclusionProof {
5466
pub fn from_ascii(input: &str) -> Result<Self> {
5567
let mut p = Parser::new(input);
56-
let leaf_index = p.parse("leaf_index")?;
68+
let leaf_index = p.parse(LEAF_INDEX_FIELD)?;
5769
let mut node_hashes = Vec::new();
5870
while !p.at_end() {
59-
let hash = p.parse("node_hash")?;
71+
let hash = p.parse(NODE_HASH_FIELD)?;
6072
node_hashes.push(hash);
6173
}
6274
Ok(Self {
@@ -68,9 +80,9 @@ impl InclusionProof {
6880
#[allow(unused_must_use)]
6981
pub fn to_ascii(&self) -> String {
7082
let mut ascii = String::new();
71-
writeln!(ascii, "leaf_index={}", self.leaf_index);
83+
writeln!(ascii, "{LEAF_INDEX_FIELD}={}", self.leaf_index);
7284
for h in self.node_hashes.iter() {
73-
writeln!(ascii, "node_hash={h:x}");
85+
writeln!(ascii, "{NODE_HASH_FIELD}={h:x}");
7486
}
7587
ascii
7688
}
@@ -79,9 +91,9 @@ impl InclusionProof {
7991
impl Protoleaf {
8092
pub fn from_ascii(input: &str) -> Result<Self> {
8193
let mut p = Parser::new(input);
82-
let message = p.parse("message")?;
83-
let signature = p.parse("signature")?;
84-
let public_key = p.parse("public_key")?;
94+
let message = p.parse(MESSAGE_FIELD)?;
95+
let signature = p.parse(SIGNATURE_FIELD)?;
96+
let public_key = p.parse(PUBLIC_KEY_FIELD)?;
8597
Ok(Protoleaf {
8698
message,
8799
signature,
@@ -92,9 +104,9 @@ impl Protoleaf {
92104
#[allow(unused_must_use)]
93105
pub fn to_ascii(&self) -> String {
94106
let mut ascii = String::new();
95-
writeln!(ascii, "message={:x}", self.message);
96-
writeln!(ascii, "signature={:x}", self.signature);
97-
writeln!(ascii, "public_key={:x}", self.public_key);
107+
writeln!(ascii, "{MESSAGE_FIELD}={:x}", self.message);
108+
writeln!(ascii, "{SIGNATURE_FIELD}={:x}", self.signature);
109+
writeln!(ascii, "{PUBLIC_KEY_FIELD}={:x}", self.public_key);
98110
ascii
99111
}
100112
}
@@ -109,12 +121,12 @@ impl SigsumSignature {
109121
)));
110122
}
111123
let mut p = Parser::new(parts[0]);
112-
let version: u64 = p.parse("version")?;
124+
let version: u64 = p.parse(VERSION_FIELD)?;
113125
if version != 2 {
114126
return Err(ParseAsciiError(format!("version {version} not supported")));
115127
}
116-
let log_keyhash = p.parse("log")?;
117-
let (leaf_keyhash, leaf_signature) = p.parse("leaf")?;
128+
let log_keyhash = p.parse(LOG_FIELD)?;
129+
let (leaf_keyhash, leaf_signature) = p.parse(LEAF_FIELD)?;
118130
if !p.at_end() {
119131
return Err(ParseAsciiError(
120132
"expected an empty line after 'leaf'".into(),
@@ -134,28 +146,28 @@ impl SigsumSignature {
134146
#[allow(unused_must_use)]
135147
pub fn to_ascii(&self) -> String {
136148
let mut ascii = String::new();
137-
writeln!(ascii, "version=2");
138-
writeln!(ascii, "log={:x}", self.log_keyhash);
149+
writeln!(ascii, "{VERSION_FIELD}=2");
150+
writeln!(ascii, "{LOG_FIELD}={:x}", self.log_keyhash);
139151
writeln!(
140152
ascii,
141-
"leaf={:x} {:x}",
153+
"{LEAF_FIELD}={:x} {:x}",
142154
self.leaf_keyhash, self.leaf_signature
143155
);
144156
writeln!(ascii);
145-
writeln!(ascii, "size={}", self.sth.size);
146-
writeln!(ascii, "root_hash={:x}", self.sth.root_hash);
147-
writeln!(ascii, "signature={:x}", self.sth.signature);
157+
writeln!(ascii, "{SIZE_FIELD}={}", self.sth.size);
158+
writeln!(ascii, "{ROOT_HASH_FIELD}={:x}", self.sth.root_hash);
159+
writeln!(ascii, "{SIGNATURE_FIELD}={:x}", self.sth.signature);
148160
for cosig in self.sth.cosignatures.iter() {
149161
writeln!(
150162
ascii,
151-
"cosignature={:x} {} {:x}",
163+
"{COSIGNATURE_FIELD}={:x} {} {:x}",
152164
cosig.keyhash, cosig.timestamp, cosig.cosignature
153165
);
154166
}
155167
writeln!(ascii);
156-
writeln!(ascii, "leaf_index={}", self.proof.leaf_index);
168+
writeln!(ascii, "{LEAF_INDEX_FIELD}={}", self.proof.leaf_index);
157169
for h in self.proof.node_hashes.iter() {
158-
writeln!(ascii, "node_hash={h:x}");
170+
writeln!(ascii, "{NODE_HASH_FIELD}={h:x}");
159171
}
160172
ascii
161173
}

0 commit comments

Comments
 (0)