1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufRead, BufReader, Result, Write};
use std::path::Path;
#[derive(Debug)]
pub struct LoadedEntry {
pub name: String,
pub oid: String,
pub sn: String,
pub description: String,
}
pub type LoadedMap = BTreeMap<String, Vec<LoadedEntry>>;
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<LoadedMap> {
let mut map = BTreeMap::new();
let file = File::open(path)?;
for line in BufReader::new(file).lines() {
let line = line?;
if line.is_empty() || line.starts_with('#') {
continue;
}
let mut iter = line.splitn(5, '\t');
let feature = iter.next().expect("invalid oid_db format: missing feature").replace('-', "_");
let name = iter.next().expect("invalid oid_db format: missing name").to_string();
let oid = iter.next().expect("invalid oid_db format: missing OID").to_string();
let sn = iter.next().expect("invalid oid_db format: missing short name").to_string();
let description = iter.next().expect("invalid oid_db format: missing description").to_string();
let entry = LoadedEntry {
name,
oid,
sn,
description,
};
let v = map.entry(feature.to_string()).or_insert_with(Vec::new);
v.push(entry);
}
Ok(map)
}
pub fn generate_file<P: AsRef<Path>>(map: &LoadedMap, dest_path: P) -> Result<()> {
let mut out_file = File::create(&dest_path)?;
for feat_entries in map.values() {
for v in feat_entries {
if v.name != "\"\"" {
writeln!(out_file, "/// {}", v.oid)?;
writeln!(out_file, "pub const {}: Oid<'static> = oid!({});", v.name, v.oid)?;
}
}
}
writeln!(out_file)?;
writeln!(out_file, r#"#[cfg(feature = "registry")]"#)?;
writeln!(out_file, r#"#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]"#)?;
writeln!(out_file, "impl<'a> OidRegistry<'a> {{")?;
for (k, v) in map {
writeln!(out_file, r#" #[cfg(feature = "{}")]"#, k)?;
writeln!(out_file, r#" #[cfg_attr(docsrs, doc(cfg(feature = "{}")))]"#, k)?;
writeln!(
out_file,
r#" #[doc = "Load all known OIDs for feature `{}` in the registry."]"#,
k
)?;
writeln!(out_file, " pub fn with_{}(mut self) -> Self {{", k)?;
for item in v {
writeln!(
out_file,
r#" self.insert(oid!({}), OidEntry::new("{}", "{}"));"#,
item.oid, item.sn, item.description
)?;
}
writeln!(out_file, " self")?;
writeln!(out_file, " }}")?;
writeln!(out_file)?;
}
writeln!(out_file, "}}")?;
Ok(())
}