Skip to content

Commit

Permalink
const-oid: return all matched names when looking up oid (#1129)
Browse files Browse the repository at this point in the history
Multiple RFCs may be defining the same OID. This commit provides
a `Database::find_names_for_oid` method that will return all values
that were matched.
  • Loading branch information
baloo committed Jun 29, 2023
1 parent f2e9c98 commit 41bbcec
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions const-oid/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const fn eq_case(lhs: &[u8], rhs: &[u8]) -> bool {
}

/// A query interface for OIDs/Names.
#[derive(Copy, Clone)]
pub struct Database<'a>(&'a [(&'a ObjectIdentifier, &'a str)]);

impl<'a> Database<'a> {
Expand Down Expand Up @@ -82,6 +83,43 @@ impl<'a> Database<'a> {

None
}

/// Return the list of matched name for the OID.
pub const fn find_names_for_oid(&self, oid: ObjectIdentifier) -> Names<'a> {
Names {
database: *self,
oid,
position: 0,
}
}
}

/// Iterator returning the multiple names that may be associated with an OID.
pub struct Names<'a> {
database: Database<'a>,
oid: ObjectIdentifier,
position: usize,
}

impl<'a> Iterator for Names<'a> {
type Item = &'a str;

fn next(&mut self) -> Option<&'a str> {
let mut i = self.position;

while i < self.database.0.len() {
let lhs = self.database.0[i].0;

if lhs.buffer.eq(&self.oid.buffer) {
self.position = i + 1;
return Some(self.database.0[i].1);
}

i += 1;
}

None
}
}

#[cfg(test)]
Expand Down

0 comments on commit 41bbcec

Please sign in to comment.