Skip to content

Commit

Permalink
const-oid: return all matched names when looking up oid
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 f36bcbb commit 68a3205
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions const-oid/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ impl<'a> Database<'a> {

None
}

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

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

impl<'a, 'o> Iterator for Names<'a, 'o> {
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;
}

return None;
}
}

#[cfg(test)]
Expand Down

0 comments on commit 68a3205

Please sign in to comment.