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
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png",
html_root_url = "https://docs.rs/const-oid/0.1.0"
)]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(test)]
extern crate std;
use core::fmt;
pub struct ObjectIdentifier {
nodes: &'static [u32],
}
impl ObjectIdentifier {
pub const fn new(nodes: &'static [u32]) -> Self {
Self { nodes }
}
}
impl AsRef<[u32]> for ObjectIdentifier {
fn as_ref(&self) -> &[u32] {
self.nodes
}
}
impl fmt::Display for ObjectIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, node) in self.as_ref().iter().enumerate() {
write!(f, "{}", node)?;
if i < self.as_ref().len() - 1 {
write!(f, ".")?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::ObjectIdentifier;
use std::string::ToString;
const EXAMPLE_OID: ObjectIdentifier = ObjectIdentifier::new(&[1, 2, 840, 10045, 3, 1, 7]);
#[test]
fn display_test() {
let oid = EXAMPLE_OID.to_string();
assert_eq!(oid, "1.2.840.10045.3.1.7");
}
}