Coverage Report

Created: 2024-12-17 06:15

/rust/registry/src/index.crates.io-6f17d22bba15001f/oid-registry-0.7.1/src/load.rs
Line
Count
Source (jump to first uncovered line)
1
use std::collections::BTreeMap;
2
use std::fs::File;
3
use std::io::{BufRead, BufReader, Result, Write};
4
use std::path::Path;
5
6
/// Temporary structure, created when reading a file containing OID declarations
7
#[derive(Debug)]
8
pub struct LoadedEntry {
9
    /// Name of the global constant for this entry.
10
    ///
11
    /// If `name` is "", then no global constant is defined
12
    pub name: String,
13
    /// Textual representation of OID (ex: 2.5.4.3)
14
    pub oid: String,
15
    /// A short name to describe OID. Should be unique (no check is done)
16
    pub sn: String,
17
    /// A description for this entry
18
    pub description: String,
19
}
20
21
/// Temporary structure, created when reading a file containing OID declarations
22
pub type LoadedMap = BTreeMap<String, Vec<LoadedEntry>>;
23
24
/// Load a file to an OID description map
25
///
26
/// format of the file: tab-separated values
27
/// <pre>
28
/// feature   name   oid   short_name   description (until end of line)
29
/// </pre>
30
///
31
/// `name` is used to declare a global constant when creating output file (see `generate_file`).
32
/// If `name` is "" then no constant will be written
33
///
34
0
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<LoadedMap> {
35
0
    let mut map = BTreeMap::new();
36
37
0
    let file = File::open(path)?;
38
0
    for line in BufReader::new(file).lines() {
39
0
        let line = line?;
40
0
        if line.is_empty() || line.starts_with('#') {
41
0
            continue;
42
0
        }
43
0
        // split by tabs
44
0
        let mut iter = line.splitn(5, '\t');
45
0
        let feature = iter.next().expect("invalid oid_db format: missing feature").replace('-', "_");
46
0
        let name = iter.next().expect("invalid oid_db format: missing name").to_string();
47
0
        let oid = iter.next().expect("invalid oid_db format: missing OID").to_string();
48
0
        let sn = iter.next().expect("invalid oid_db format: missing short name").to_string();
49
0
        let description = iter.next().expect("invalid oid_db format: missing description").to_string();
50
0
51
0
        let entry = LoadedEntry {
52
0
            name,
53
0
            oid,
54
0
            sn,
55
0
            description,
56
0
        };
57
0
58
0
        let v = map.entry(feature.to_string()).or_insert_with(Vec::new);
59
0
60
0
        v.push(entry);
61
    }
62
0
    Ok(map)
63
0
}
64
65
/// Generate a file containing a `with_<feat>` method for OidRegistry
66
0
pub fn generate_file<P: AsRef<Path>>(map: &LoadedMap, dest_path: P) -> Result<()> {
67
0
    let mut out_file = File::create(&dest_path)?;
68
0
    for feat_entries in map.values() {
69
0
        for v in feat_entries {
70
0
            if v.name != "\"\"" {
71
0
                writeln!(out_file, "/// {}", v.oid)?;
72
0
                writeln!(out_file, "pub const {}: Oid<'static> = oid!({});", v.name, v.oid)?;
73
0
            }
74
        }
75
    }
76
0
    writeln!(out_file)?;
77
0
    writeln!(out_file, r#"#[cfg(feature = "registry")]"#)?;
78
0
    writeln!(out_file, r#"#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]"#)?;
79
0
    writeln!(out_file, "impl<'a> OidRegistry<'a> {{")?;
80
0
    for (k, v) in map {
81
0
        writeln!(out_file, r#"    #[cfg(feature = "{}")]"#, k)?;
82
0
        writeln!(out_file, r#"    #[cfg_attr(docsrs, doc(cfg(feature = "{}")))]"#, k)?;
83
0
        writeln!(
84
0
            out_file,
85
0
            r#"    #[doc = "Load all known OIDs for feature `{}` in the registry."]"#,
86
0
            k
87
0
        )?;
88
0
        writeln!(out_file, "    pub fn with_{}(mut self) -> Self {{", k)?;
89
0
        for item in v {
90
0
            writeln!(
91
0
                out_file,
92
0
                r#"        self.insert(oid!({}), OidEntry::new("{}", "{}"));"#,
93
0
                item.oid, item.sn, item.description
94
0
            )?;
95
        }
96
0
        writeln!(out_file, "        self")?;
97
0
        writeln!(out_file, "    }}")?;
98
0
        writeln!(out_file)?;
99
    }
100
0
    writeln!(out_file, "}}")?;
101
0
    Ok(())
102
0
}