#![deny(missing_docs, unstable_features, unused_import_braces, unused_qualifications, unreachable_pub)]
#![forbid(unsafe_code)]
#![warn(
/* missing_docs,
rust_2018_idioms,*/
missing_debug_implementations,
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub use asn1_rs;
pub use asn1_rs::Oid;
use asn1_rs::oid;
use std::borrow::Cow;
use std::collections::HashMap;
mod deprecated;
mod load;
pub use deprecated::*;
pub use load::*;
#[derive(Debug)]
pub struct OidEntry {
sn: Cow<'static, str>,
description: Cow<'static, str>,
}
impl OidEntry {
pub fn new<S, T>(sn: S, description: T) -> OidEntry
where
S: Into<Cow<'static, str>>,
T: Into<Cow<'static, str>>,
{
let sn = sn.into();
let description = description.into();
OidEntry { sn, description }
}
#[inline]
pub fn sn(&self) -> &str {
&self.sn
}
#[inline]
pub fn description(&self) -> &str {
&self.description
}
}
impl From<(&'static str, &'static str)> for OidEntry {
fn from(t: (&'static str, &'static str)) -> Self {
Self::new(t.0, t.1)
}
}
#[derive(Debug, Default)]
pub struct OidRegistry<'a> {
map: HashMap<Oid<'a>, OidEntry>,
}
impl<'a> OidRegistry<'a> {
pub fn insert<E>(&mut self, oid: Oid<'a>, entry: E) -> Option<OidEntry>
where
E: Into<OidEntry>,
{
self.map.insert(oid, entry.into())
}
pub fn get(&self, oid: &Oid<'a>) -> Option<&OidEntry> {
self.map.get(oid)
}
pub fn keys(&self) -> impl Iterator<Item = &Oid<'a>> {
self.map.keys()
}
pub fn values(&self) -> impl Iterator<Item = &OidEntry> {
self.map.values()
}
pub fn iter(&self) -> impl Iterator<Item = (&Oid<'a>, &OidEntry)> {
self.map.iter()
}
pub fn iter_by_sn<S: Into<String>>(&self, sn: S) -> impl Iterator<Item = (&Oid<'a>, &OidEntry)> {
let s = sn.into();
self.map.iter().filter(move |(_, entry)| entry.sn == s)
}
#[cfg(feature = "crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
pub fn with_crypto(self) -> Self {
self.with_pkcs1().with_x962().with_kdf().with_nist_algs()
}
#[cfg(feature = "crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
pub fn with_all_crypto(self) -> Self {
self.with_crypto().with_pkcs7().with_pkcs9().with_pkcs12()
}
}
pub fn format_oid(oid: &Oid, registry: &OidRegistry) -> String {
if let Some(entry) = registry.map.get(oid) {
format!("{} ({})", entry.sn, oid)
} else {
format!("{}", oid)
}
}
include!(concat!(env!("OUT_DIR"), "/oid_db.rs"));
#[rustfmt::skip::macros(oid)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lifetimes() {
fn add_entry(input: &str, oid: Oid<'static>, registry: &mut OidRegistry) {
let s = String::from(input);
let entry = OidEntry::new("test", s);
registry.insert(oid, entry);
}
let mut registry = OidRegistry::default();
add_entry("a", oid!(1.2.3.4), &mut registry);
add_entry("b", oid!(1.2.3.5), &mut registry);
let e = OidEntry::new("c", "test_c");
registry.insert(oid!(1.2.4.1), e);
registry.insert(oid!(1.2.5.1), ("a", "b"));
let iter = registry.iter_by_sn("test");
assert_eq!(iter.count(), 2);
}
}