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
use std::fmt; use std::slice; #[derive(PartialEq,Eq,Clone)] pub struct Oid (Vec<u64>); impl Oid { pub fn from(s: &[u64]) -> Oid { let v : Vec<u64> = s.iter().fold( Vec::new(), |mut acc,i| { acc.push(*i); acc } ); Oid(v) } pub fn from_vec(v: &Vec<u64>) -> Oid { Oid(v.clone()) } pub fn to_hex(&self) -> String { if self.0.len() == 0 { return String::new(); } let mut s = self.0[0].to_string(); for it in self.0.iter().skip(1) { s.push('.'); s = s + &it.to_string(); } s } pub fn iter(&self) -> slice::Iter<u64> { self.0.iter() } } impl fmt::Display for Oid { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&self.to_hex()) } } impl fmt::Debug for Oid { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&format!("OID({})", self.to_hex())) } } #[cfg(test)] mod tests { use oid::Oid; #[test] fn test_oid_fmt() { let oid = Oid::from(&[1, 2, 840, 113549, 1, 1, 5]); assert_eq!(format!("{}",oid), "1.2.840.113549.1.1.5".to_owned()); } }