1use std::fmt;
2
3use rustorb_core::name::{self, InvalidName, Name};
4use rustorb_core::{CompletionStatus, SystemException, SystemExceptionKind, minor};
5
6use crate::client::{NamingContext, from_idl_name, to_idl_name};
7use crate::idl::cos_naming::{self as idl, naming_context as nc, naming_context_ext as nce};
8use crate::repository_id;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum NotFoundReason {
13 MissingNode,
16 NotContext,
19 NotObject,
22}
23
24impl NotFoundReason {
25 pub fn name(self) -> &'static str {
27 match self {
28 NotFoundReason::MissingNode => "missing_node",
29 NotFoundReason::NotContext => "not_context",
30 NotFoundReason::NotObject => "not_object",
31 }
32 }
33}
34
35impl fmt::Display for NotFoundReason {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 f.write_str(self.name())
38 }
39}
40
41#[derive(Debug, Clone)]
48pub enum NamingError {
49 NotFound {
52 why: NotFoundReason,
54 rest_of_name: Name,
57 },
58 CannotProceed {
61 context: Option<NamingContext>,
63 rest_of_name: Name,
65 },
66 InvalidName,
69 AlreadyBound,
71 NotEmpty,
73 InvalidAddress,
76 System(SystemException),
78}
79
80impl NamingError {
81 pub fn repository_id(&self) -> &str {
83 match self {
84 NamingError::NotFound { .. } => repository_id::NOT_FOUND,
85 NamingError::CannotProceed { .. } => repository_id::CANNOT_PROCEED,
86 NamingError::InvalidName => repository_id::INVALID_NAME,
87 NamingError::AlreadyBound => repository_id::ALREADY_BOUND,
88 NamingError::NotEmpty => repository_id::NOT_EMPTY,
89 NamingError::InvalidAddress => repository_id::INVALID_ADDRESS,
90 NamingError::System(e) => &e.repository_id,
91 }
92 }
93
94 pub fn as_system(&self) -> Option<&SystemException> {
96 match self {
97 NamingError::System(e) => Some(e),
98 _ => None,
99 }
100 }
101}
102
103impl From<nc::NotFoundReason> for NotFoundReason {
106 fn from(why: nc::NotFoundReason) -> NotFoundReason {
107 match why {
108 nc::NotFoundReason::MissingNode => NotFoundReason::MissingNode,
109 nc::NotFoundReason::NotContext => NotFoundReason::NotContext,
110 nc::NotFoundReason::NotObject => NotFoundReason::NotObject,
111 }
112 }
113}
114
115impl From<NotFoundReason> for nc::NotFoundReason {
116 fn from(why: NotFoundReason) -> nc::NotFoundReason {
117 match why {
118 NotFoundReason::MissingNode => nc::NotFoundReason::MissingNode,
119 NotFoundReason::NotContext => nc::NotFoundReason::NotContext,
120 NotFoundReason::NotObject => nc::NotFoundReason::NotObject,
121 }
122 }
123}
124
125impl From<nc::NotFound> for NamingError {
126 fn from(e: nc::NotFound) -> NamingError {
127 NamingError::NotFound {
128 why: e.why.into(),
129 rest_of_name: from_idl_name(e.rest_of_name),
130 }
131 }
132}
133
134impl From<nc::CannotProceed> for NamingError {
135 fn from(e: nc::CannotProceed) -> NamingError {
136 NamingError::CannotProceed {
137 context: e
138 .cxt
139 .map(|c| NamingContext::unchecked_narrow(c.into_object())),
140 rest_of_name: from_idl_name(e.rest_of_name),
141 }
142 }
143}
144
145impl From<nc::InvalidName> for NamingError {
146 fn from(_: nc::InvalidName) -> NamingError {
147 NamingError::InvalidName
148 }
149}
150
151impl From<nc::AlreadyBound> for NamingError {
152 fn from(_: nc::AlreadyBound) -> NamingError {
153 NamingError::AlreadyBound
154 }
155}
156
157impl From<nc::NotEmpty> for NamingError {
158 fn from(_: nc::NotEmpty) -> NamingError {
159 NamingError::NotEmpty
160 }
161}
162
163impl From<nce::InvalidAddress> for NamingError {
164 fn from(_: nce::InvalidAddress) -> NamingError {
165 NamingError::InvalidAddress
166 }
167}
168
169enum User {
172 NotFound(nc::NotFound),
173 CannotProceed(nc::CannotProceed),
174 InvalidName(nc::InvalidName),
175 AlreadyBound(nc::AlreadyBound),
176 NotEmpty(nc::NotEmpty),
177 InvalidAddress(nce::InvalidAddress),
178}
179
180impl NamingError {
181 fn into_user(self) -> Result<User, SystemException> {
182 Ok(match self {
183 NamingError::NotFound { why, rest_of_name } => User::NotFound(nc::NotFound {
184 why: why.into(),
185 rest_of_name: to_idl_name(&rest_of_name),
186 }),
187 NamingError::CannotProceed {
188 context,
189 rest_of_name,
190 } => User::CannotProceed(nc::CannotProceed {
191 cxt: context.map(|c| idl::NamingContext::unchecked_narrow(c.into_object())),
192 rest_of_name: to_idl_name(&rest_of_name),
193 }),
194 NamingError::InvalidName => User::InvalidName(nc::InvalidName {}),
195 NamingError::AlreadyBound => User::AlreadyBound(nc::AlreadyBound {}),
196 NamingError::NotEmpty => User::NotEmpty(nc::NotEmpty {}),
197 NamingError::InvalidAddress => User::InvalidAddress(nce::InvalidAddress {}),
198 NamingError::System(e) => return Err(e),
199 })
200 }
201}
202
203macro_rules! errors {
207 ($($ty:ty { $($user:ident),* })*) => {
208 $(
209 impl From<$ty> for NamingError {
210 fn from(e: $ty) -> NamingError {
211 type E = $ty;
212 match e {
213 $(E::$user(e) => e.into(),)*
214 E::System(e) => NamingError::System(e),
215 }
216 }
217 }
218
219 impl From<NamingError> for $ty {
220 fn from(e: NamingError) -> $ty {
221 type E = $ty;
222 match e.into_user() {
223 $(Ok(User::$user(e)) => E::$user(e),)*
224 #[allow(unreachable_patterns)]
225 Ok(_) => E::System(SystemException::new(
226 SystemExceptionKind::UNKNOWN,
227 minor::UNLISTED_USER_EXCEPTION,
228 CompletionStatus::Maybe,
229 )),
230 Err(e) => E::System(e),
231 }
232 }
233 }
234 )*
235 };
236}
237
238errors! {
239 nc::BindError { NotFound, CannotProceed, InvalidName, AlreadyBound }
240 nc::RebindError { NotFound, CannotProceed, InvalidName }
241 nc::BindContextError { NotFound, CannotProceed, InvalidName, AlreadyBound }
242 nc::RebindContextError { NotFound, CannotProceed, InvalidName }
243 nc::ResolveError { NotFound, CannotProceed, InvalidName }
244 nc::UnbindError { NotFound, CannotProceed, InvalidName }
245 nc::BindNewContextError { NotFound, CannotProceed, InvalidName, AlreadyBound }
246 nc::DestroyError { NotEmpty }
247 nce::ToStringError { InvalidName }
248 nce::ToNameError { InvalidName }
249 nce::ToUrlError { InvalidAddress, InvalidName }
250 nce::ResolveStrError { NotFound, CannotProceed, InvalidName }
251}
252
253impl From<SystemException> for NamingError {
254 fn from(e: SystemException) -> Self {
255 NamingError::System(e)
256 }
257}
258
259impl From<InvalidName> for NamingError {
260 fn from(_: InvalidName) -> Self {
261 NamingError::InvalidName
262 }
263}
264
265impl fmt::Display for NamingError {
266 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267 let rest = |name: &Name| name::to_string(name).unwrap_or_else(|_| "<empty>".to_owned());
268 match self {
269 NamingError::NotFound { why, rest_of_name } => {
270 write!(
271 f,
272 "CosNaming::NamingContext::NotFound ({why}, rest {:?})",
273 rest(rest_of_name)
274 )
275 }
276 NamingError::CannotProceed { rest_of_name, .. } => {
277 write!(
278 f,
279 "CosNaming::NamingContext::CannotProceed (rest {:?})",
280 rest(rest_of_name)
281 )
282 }
283 NamingError::InvalidName => f.write_str("CosNaming::NamingContext::InvalidName"),
284 NamingError::AlreadyBound => f.write_str("CosNaming::NamingContext::AlreadyBound"),
285 NamingError::NotEmpty => f.write_str("CosNaming::NamingContext::NotEmpty"),
286 NamingError::InvalidAddress => {
287 f.write_str("CosNaming::NamingContextExt::InvalidAddress")
288 }
289 NamingError::System(e) => e.fmt(f),
290 }
291 }
292}
293
294impl std::error::Error for NamingError {}