Skip to content

Commit 5739075

Browse files
committed
Revert "const-oid: add ObjectIdentifierRef; use for db (#1212)"
This reverts commit 2f6303b. Per #1293 to keep the ergonomics of the v0.9 release we need derived `Eq`/`PartialEq` impls which allow the constants to be used in `match` expressions. That's not really possible with the generic backing storage approach that the v0.10 prereleases have been trying, and also precludes comparing OIDs with different backing storages. While an `ObjectIdentifierRef` is still worth experimenting with, it's clear it isn't ready for prime time yet, so this begins the process of backing out some changes so the database isn't yet dependent on its existence.
1 parent 734b7b1 commit 5739075

10 files changed

Lines changed: 3815 additions & 7155 deletions

File tree

const-oid/oiddbgen/Cargo.lock

Lines changed: 8 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

const-oid/oiddbgen/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
convert_case = "0.5.0"
8-
const-oid = { path = ".." }
98
proc-macro2 = "1.0.36"
109
quote = "1.0.15"
1110
regex = "1.5.5"

const-oid/oiddbgen/src/ldap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ impl<'a> LdapParser<'a> {
1010
self.0.lines().filter_map(|line| {
1111
let (name, next) = line.split_at(line.find(',').unwrap());
1212
let (.., next) = next[1..].split_at(next[1..].find(',').unwrap());
13-
let (oid, spec) = next[1..].split_at(next[1..].find(',').unwrap());
13+
let (obid, spec) = next[1..].split_at(next[1..].find(',').unwrap());
1414

15-
let indx = oid.find('.')?;
16-
oid.split_at(indx).0.parse::<usize>().ok()?;
15+
let indx = obid.find('.')?;
16+
obid.split_at(indx).0.parse::<usize>().ok()?;
1717

1818
if !spec.trim().starts_with(",[RFC") {
1919
return None;
2020
}
2121

2222
let spec = spec[2..][..spec.len() - 3].to_ascii_lowercase();
2323
let name = name.trim().to_string();
24-
let oid = oid.trim().to_string();
25-
Some((spec, name, oid))
24+
let obid = obid.trim().to_string();
25+
Some((spec, name, obid))
2626
})
2727
}
2828
}

const-oid/oiddbgen/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod asn1;
2+
mod ldap;
3+
mod node;
4+
mod root;
5+
mod spec;
6+
7+
pub use asn1::Asn1Parser;
8+
pub use ldap::LdapParser;
9+
pub use root::Root;

const-oid/oiddbgen/src/main.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
mod asn1;
2-
mod ldap;
3-
mod node;
4-
mod root;
5-
mod spec;
6-
7-
pub use asn1::Asn1Parser;
8-
pub use ldap::LdapParser;
9-
pub use root::Root;
1+
use oiddbgen::{Asn1Parser, LdapParser, Root};
102

113
// Update this database by downloading the CSV file here:
124
// https://www.iana.org/assignments/ldap-parameters/ldap-parameters.xhtml#ldap-parameters-3
@@ -41,19 +33,19 @@ const NO_BASES: &[(&str, &str)] = &[("", "")];
4133
fn main() {
4234
let mut root = Root::default();
4335

44-
for (spec, name, oid) in LdapParser::new(LDAP).iter() {
45-
root.add(&spec, &name, &oid);
36+
for (spec, name, obid) in LdapParser::new(LDAP).iter() {
37+
root.add(&spec, &name, &obid);
4638
}
4739

4840
for (spec, body) in RFCS {
49-
for (name, oid) in Asn1Parser::new(body, BASES).iter() {
50-
root.add(spec, &name, &oid);
41+
for (name, obid) in Asn1Parser::new(body, BASES).iter() {
42+
root.add(spec, &name, &obid);
5143
}
5244
}
5345

5446
for (spec, body) in MDS {
55-
for (name, oid) in Asn1Parser::new(body, NO_BASES).iter() {
56-
root.add(spec, &name, &oid);
47+
for (name, obid) in Asn1Parser::new(body, NO_BASES).iter() {
48+
root.add(spec, &name, &obid);
5749
}
5850
}
5951

const-oid/oiddbgen/src/node.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
use const_oid::ObjectIdentifier;
1+
use std::cmp::Ordering;
2+
23
use convert_case::{Case, Casing};
34
use proc_macro2::{Ident, Span, TokenStream};
45
use quote::quote;
56

6-
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
7+
#[derive(Clone, Debug, PartialEq, Eq)]
78
pub struct Node {
8-
oid: ObjectIdentifier,
9+
obid: String,
910
name: String,
1011
symb: Ident,
1112
}
1213

14+
impl Ord for Node {
15+
fn cmp(&self, other: &Self) -> Ordering {
16+
match self.obid.cmp(&other.obid) {
17+
Ordering::Equal => match self.name.len().cmp(&other.name.len()) {
18+
Ordering::Equal => self.name.cmp(&other.name),
19+
o => o,
20+
},
21+
o => o,
22+
}
23+
}
24+
}
25+
26+
impl PartialOrd for Node {
27+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
28+
Some(self.cmp(other))
29+
}
30+
}
31+
1332
impl Node {
14-
pub fn new(oid: ObjectIdentifier, name: String) -> Self {
33+
pub fn new(obid: String, name: String) -> Self {
1534
// Raise the first letter in the beginning or after a hyphen.
1635
// This produces more natural UpperSnake conversions below.
1736
let mut upper = true;
@@ -32,7 +51,7 @@ impl Node {
3251
let symb = symb.to_case(Case::UpperSnake);
3352
let symb = Ident::new(&symb, Span::call_site());
3453

35-
Self { oid, name, symb }
54+
Self { obid, name, symb }
3655
}
3756

3857
pub fn name(&self) -> &str {
@@ -44,19 +63,11 @@ impl Node {
4463
}
4564

4665
pub fn definition(&self) -> TokenStream {
66+
let obid = self.obid.replace(' ', ""); // Fix a typo.
4767
let symb = &self.symb;
48-
let oid = self.oid.to_string();
49-
let doc = format!("#[doc=\"{}: {}\"]", &self.oid, &self.name)
50-
.parse::<TokenStream>()
51-
.expect("malformed doc comment");
52-
53-
let bytes = format!("&{:?}", oid.as_bytes())
54-
.parse::<TokenStream>()
55-
.expect("malformed byte slice literal");
5668

5769
quote! {
58-
#doc
59-
pub const #symb: crate::ObjectIdentifierRef<'static> = crate::ObjectIdentifierRef::from_bytes_unchecked(#bytes);
70+
pub const #symb: crate::ObjectIdentifier = crate::ObjectIdentifier::new_unwrap(#obid);
6071
}
6172
}
6273
}

const-oid/oiddbgen/src/root.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
use crate::{node::Node, spec::Spec};
2-
use const_oid::ObjectIdentifier;
2+
3+
use std::collections::BTreeMap;
4+
35
use proc_macro2::{Ident, Span, TokenStream};
46
use quote::quote;
5-
use std::collections::BTreeMap;
67

78
#[derive(Clone, Debug, Default)]
89
pub struct Root(BTreeMap<Ident, Spec>);
910

1011
impl Root {
11-
pub fn add(&mut self, spec: &str, name: &str, oid: &str) {
12+
pub fn add(&mut self, spec: &str, name: &str, obid: &str) {
1213
let name = name.trim().to_string();
13-
let oid = oid.trim().parse::<ObjectIdentifier>().expect("invalid OID");
14+
let obid = obid.trim().to_string();
1415
let spec = spec.trim().to_ascii_lowercase();
1516
let spec = Ident::new(&spec, Span::call_site());
1617

1718
self.0
1819
.entry(spec)
1920
.or_insert_with(Spec::default)
20-
.insert(Node::new(oid, name));
21+
.insert(Node::new(obid, name));
2122
}
2223

2324
pub fn module(&self) -> TokenStream {
@@ -26,7 +27,7 @@ impl Root {
2627

2728
for (spec, s) in &self.0 {
2829
mods.extend(s.module(spec));
29-
recs.extend(s.records(quote! { #spec }));
30+
recs.extend(s.records(quote! { &#spec }));
3031
}
3132

3233
quote! {

const-oid/src/db.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod gen;
1515

1616
pub use gen::*;
1717

18-
use crate::{Error, ObjectIdentifier, ObjectIdentifierRef};
18+
use crate::{Error, ObjectIdentifier};
1919

2020
/// A const implementation of case-insensitive ASCII equals.
2121
const fn eq_case(lhs: &[u8], rhs: &[u8]) -> bool {
@@ -37,7 +37,7 @@ const fn eq_case(lhs: &[u8], rhs: &[u8]) -> bool {
3737

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

4242
impl<'a> Database<'a> {
4343
/// Looks up a name for an OID.
@@ -52,14 +52,13 @@ impl<'a> Database<'a> {
5252
}
5353

5454
/// Finds a named oid by its associated OID.
55-
pub fn by_oid<B>(&self, oid: &ObjectIdentifier<B>) -> Option<&'a str>
56-
where
57-
B: AsRef<[u8]>,
58-
{
55+
pub const fn by_oid(&self, oid: &ObjectIdentifier) -> Option<&'a str> {
5956
let mut i = 0;
6057

6158
while i < self.0.len() {
62-
if oid == &self.0[i].0 {
59+
let lhs = self.0[i].0;
60+
61+
if lhs.buffer.eq(&oid.buffer) {
6362
return Some(self.0[i].1);
6463
}
6564

@@ -70,7 +69,7 @@ impl<'a> Database<'a> {
7069
}
7170

7271
/// Finds a named oid by its associated name.
73-
pub const fn by_name(&self, name: &str) -> Option<ObjectIdentifierRef<'a>> {
72+
pub const fn by_name(&self, name: &str) -> Option<&'a ObjectIdentifier> {
7473
let mut i = 0;
7574

7675
while i < self.0.len() {
@@ -86,7 +85,7 @@ impl<'a> Database<'a> {
8685
}
8786

8887
/// Return the list of matched name for the OID.
89-
pub fn find_names_for_oid(&self, oid: ObjectIdentifier) -> Names<'a> {
88+
pub const fn find_names_for_oid(&self, oid: ObjectIdentifier) -> Names<'a> {
9089
Names {
9190
database: *self,
9291
oid,
@@ -111,7 +110,7 @@ impl<'a> Iterator for Names<'a> {
111110
while i < self.database.0.len() {
112111
let lhs = self.database.0[i].0;
113112

114-
if lhs.eq(&self.oid) {
113+
if lhs.buffer.eq(&self.oid.buffer) {
115114
self.position = i + 1;
116115
return Some(self.database.0[i].1);
117116
}
@@ -141,7 +140,7 @@ mod tests {
141140
#[test]
142141
fn by_name() {
143142
let cn = super::DB.by_name("CN").expect("cn not found");
144-
assert_eq!(CN, cn);
143+
assert_eq!(&CN, cn);
145144

146145
assert_eq!(None, super::DB.by_name("purplePeopleEater"));
147146
}

0 commit comments

Comments
 (0)