Skip to content

Commit b00f52c

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 b00f52c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

crates/mtc_api/src/relative_oid.rs

Lines changed: 21 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,15 @@ impl RelativeOid {
3943
}
4044
}
4145

46+
impl std::fmt::Display for RelativeOid {
47+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48+
for arc in self.arcs.iter().take(self.arcs.len() - 1) {
49+
write!(f, "{}.", arc)?;
50+
}
51+
write!(f, "{}", self.arcs[self.arcs.len() - 1])
52+
}
53+
}
54+
4255
impl FromStr for RelativeOid {
4356
type Err = MtcError;
4457
/// Parse the [`RelativeOid`] from a decimal-dotted string.
@@ -61,9 +74,15 @@ mod tests {
6174
use super::*;
6275

6376
#[test]
64-
fn encode_decode() {
77+
fn encode_tagged() {
6578
let relative_oid = RelativeOid::from_str("13335.2").unwrap();
6679
let any = Any::new(Tag::RelativeOid, relative_oid.as_bytes()).unwrap();
6780
assert_eq!(any.to_der().unwrap(), b"\x0d\x03\xe8\x17\x02");
6881
}
82+
83+
#[test]
84+
fn encode_string() {
85+
let relative_oid = RelativeOid::from_str("13335.2").unwrap();
86+
assert_eq!(relative_oid.to_string(), "13335.2");
87+
}
6988
}

0 commit comments

Comments
 (0)