1use std::fmt;
2use std::str::FromStr;
3
4use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, Decoder, Encoder, Encoding};
5
6use crate::{Error, IiopProfile};
7
8pub const TAG_INTERNET_IOP: u32 = 0;
10pub const TAG_MULTIPLE_COMPONENTS: u32 = 1;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct TaggedProfile {
16 pub tag: u32,
18 pub profile_data: Vec<u8>,
20}
21
22impl CdrEncode for TaggedProfile {
23 fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
24 enc.write_u32(self.tag);
25 enc.write_octet_seq(&self.profile_data)
26 }
27}
28
29impl CdrDecode for TaggedProfile {
30 fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
31 Ok(TaggedProfile {
32 tag: dec.read_u32()?,
33 profile_data: dec.read_octet_seq()?.to_vec(),
34 })
35 }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct Ior {
47 pub type_id: String,
49 pub profiles: Vec<TaggedProfile>,
51}
52
53impl Ior {
54 pub fn nil() -> Ior {
56 Ior {
57 type_id: String::new(),
58 profiles: Vec::new(),
59 }
60 }
61
62 pub fn is_nil(&self) -> bool {
64 self.type_id.is_empty() && self.profiles.is_empty()
65 }
66
67 pub fn iiop_profiles(&self) -> impl Iterator<Item = crate::Result<IiopProfile>> {
69 self.profiles
70 .iter()
71 .filter(|p| p.tag == TAG_INTERNET_IOP)
72 .map(IiopProfile::from_tagged_profile)
73 }
74
75 pub fn from_encapsulation(data: &[u8]) -> crate::Result<Ior> {
78 let mut dec = Decoder::encapsulation(data, Encoding::Giop1_2)?;
79 Ok(Ior::decode(&mut dec)?)
80 }
81
82 pub fn to_encapsulation(&self, order: ByteOrder) -> crate::Result<Vec<u8>> {
84 let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
85 self.encode(&mut enc)?;
86 Ok(enc.into_bytes())
87 }
88
89 pub fn from_ior_string(s: &str) -> crate::Result<Ior> {
94 let s = s.trim_ascii();
95 let hex = match s.get(..4) {
96 Some(prefix) if prefix.eq_ignore_ascii_case("IOR:") => &s.as_bytes()[4..],
97 _ => return Err(Error::InvalidIorString("missing IOR: prefix")),
98 };
99 if hex.len() % 2 != 0 {
100 return Err(Error::InvalidIorString("odd number of hex digits"));
101 }
102 let bytes = hex
103 .chunks_exact(2)
104 .map(|pair| Some(hex_value(pair[0])? << 4 | hex_value(pair[1])?))
105 .collect::<Option<Vec<u8>>>()
106 .ok_or(Error::InvalidIorString("invalid hex digit"))?;
107 Ior::from_encapsulation(&bytes)
108 }
109
110 pub fn to_ior_string(&self, order: ByteOrder) -> crate::Result<String> {
113 const DIGITS: &[u8; 16] = b"0123456789abcdef";
114 let bytes = self.to_encapsulation(order)?;
115 let mut s = String::with_capacity(4 + bytes.len() * 2);
116 s.push_str("IOR:");
117 for b in bytes {
118 s.push(DIGITS[usize::from(b >> 4)] as char);
119 s.push(DIGITS[usize::from(b & 0xf)] as char);
120 }
121 Ok(s)
122 }
123}
124
125fn hex_value(digit: u8) -> Option<u8> {
126 (digit as char).to_digit(16).map(|v| v as u8)
127}
128
129impl FromStr for Ior {
130 type Err = Error;
131
132 fn from_str(s: &str) -> crate::Result<Ior> {
133 Ior::from_ior_string(s)
134 }
135}
136
137impl fmt::Display for Ior {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 let s = self
141 .to_ior_string(ByteOrder::NATIVE)
142 .map_err(|_| fmt::Error)?;
143 f.write_str(&s)
144 }
145}
146
147impl CdrEncode for Ior {
148 fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
149 enc.write_string(&self.type_id)?;
150 self.profiles.encode(enc)
151 }
152}
153
154impl CdrDecode for Ior {
155 fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
156 Ok(Ior {
157 type_id: dec.read_string()?,
158 profiles: Vec::decode(dec)?,
159 })
160 }
161}