Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions const-oid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl ObjectIdentifier {
}

/// Parse an OID from a slice of [`Arc`] values (i.e. integers).
pub fn from_arcs<'a>(arcs: impl IntoIterator<Item = &'a Arc>) -> Result<Self> {
pub fn from_arcs(arcs: impl IntoIterator<Item = Arc>) -> Result<Self> {
let mut encoder = Encoder::new();

for &arc in arcs {
for arc in arcs {
encoder = encoder.arc(arc)?;
}

Expand Down Expand Up @@ -158,6 +158,17 @@ impl ObjectIdentifier {
Arcs::new(self)
}

/// Get the length of this [`ObjectIdentifier`] in arcs.
pub fn len(&self) -> usize {
self.arcs().count()
}

/// Get the parent OID of this one (if applicable).
pub fn parent(&self) -> Option<Self> {
let num_arcs = self.len().checked_sub(1)?;
Self::from_arcs(self.arcs().take(num_arcs)).ok()
}

/// Push an additional arc onto this OID, returning the child OID.
pub const fn push_arc(self, arc: Arc) -> Result<Self> {
// TODO(tarcieri): use `?` when stable in `const fn`
Expand Down
22 changes: 15 additions & 7 deletions const-oid/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,24 @@ fn display() {

#[test]
fn try_from_u32_slice() {
let oid1 = ObjectIdentifier::from_arcs(&[1, 2, 840, 10045, 2, 1]).unwrap();
let oid1 = ObjectIdentifier::from_arcs([1, 2, 840, 10045, 2, 1]).unwrap();
assert_eq!(oid1.arc(0).unwrap(), 1);
assert_eq!(oid1.arc(1).unwrap(), 2);
assert_eq!(EXAMPLE_OID_1, oid1);

let oid2 = ObjectIdentifier::from_arcs(&[2, 16, 840, 1, 101, 3, 4, 1, 42]).unwrap();
let oid2 = ObjectIdentifier::from_arcs([2, 16, 840, 1, 101, 3, 4, 1, 42]).unwrap();
assert_eq!(oid2.arc(0).unwrap(), 2);
assert_eq!(oid2.arc(1).unwrap(), 16);
assert_eq!(EXAMPLE_OID_2, oid2);

// Too short
assert!(ObjectIdentifier::from_arcs(&[1, 2]).is_err());
assert!(ObjectIdentifier::from_arcs([1, 2]).is_err());

// Invalid first arc
assert!(ObjectIdentifier::from_arcs(&[3, 2, 840, 10045, 3, 1, 7]).is_err());
assert!(ObjectIdentifier::from_arcs([3, 2, 840, 10045, 3, 1, 7]).is_err());

// Invalid second arc
assert!(ObjectIdentifier::from_arcs(&[1, 40, 840, 10045, 3, 1, 7]).is_err());
assert!(ObjectIdentifier::from_arcs([1, 40, 840, 10045, 3, 1, 7]).is_err());
}

#[test]
Expand Down Expand Up @@ -167,11 +167,19 @@ fn parse_invalid_second_arc() {
);
}

#[test]
fn parent() {
let oid = ObjectIdentifier::new("1.2.3.4").unwrap();
let parent = oid.parent().unwrap();
assert_eq!(parent, ObjectIdentifier::new("1.2.3").unwrap());
assert_eq!(parent.parent(), None);
}

#[test]
fn push_arc() {
let oid = ObjectIdentifier::new_unwrap("1.2.3");
let oid = ObjectIdentifier::new("1.2.3").unwrap();
assert_eq!(
oid.push_arc(4).unwrap(),
ObjectIdentifier::new_unwrap("1.2.3.4")
ObjectIdentifier::new("1.2.3.4").unwrap()
);
}