Skip to content

Commit 45a60da

Browse files
committed
Implement ToString for RelativeOid
For the purposes of computing the tlog key ID, the log ID is used as the key name. Add a method for encoding the arcs as a string so that we can properly construct the key name. Implementation note: Currently we only keep around the DER encoding of the arcs. We could decode the arcs from the DER blob, but it's a bit more convenient to just keep the arcs around in the data structure.
1 parent d818076 commit 45a60da

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

crates/mtc_api/src/relative_oid.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::str::FromStr;
77
#[derive(Clone)]
88
pub struct RelativeOid {
99
ber: Vec<u8>,
10+
arcs: Vec<u32>,
1011
}
1112

1213
impl RelativeOid {
@@ -30,7 +31,10 @@ impl RelativeOid {
3031
if ber.len() > 255 {
3132
return Err(MtcError::Dynamic("invalid relative OID".into()));
3233
}
33-
Ok(Self { ber })
34+
Ok(Self {
35+
ber,
36+
arcs: arcs.to_vec(),
37+
})
3438
}
3539

3640
/// Returns the DER-encoded content bytes.
@@ -39,6 +43,16 @@ impl RelativeOid {
3943
}
4044
}
4145

46+
impl ToString for RelativeOid {
47+
fn to_string(&self) -> String {
48+
self.arcs
49+
.iter()
50+
.map(|arc| arc.to_string())
51+
.collect::<Vec<_>>()
52+
.join(".".into())
53+
}
54+
}
55+
4256
impl FromStr for RelativeOid {
4357
type Err = MtcError;
4458
/// Parse the [`RelativeOid`] from a decimal-dotted string.
@@ -61,9 +75,15 @@ mod tests {
6175
use super::*;
6276

6377
#[test]
64-
fn encode_decode() {
78+
fn encode_tagged() {
6579
let relative_oid = RelativeOid::from_str("13335.2").unwrap();
6680
let any = Any::new(Tag::RelativeOid, relative_oid.as_bytes()).unwrap();
6781
assert_eq!(any.to_der().unwrap(), b"\x0d\x03\xe8\x17\x02");
6882
}
83+
84+
#[test]
85+
fn encode_string() {
86+
let relative_oid = RelativeOid::from_str("13335.2").unwrap();
87+
assert_eq!(relative_oid.to_string(), "13335.2");
88+
}
6989
}

0 commit comments

Comments
 (0)