• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::target::TargetError;
2 use crate::GdbStubError;
3 
4 /// Extension trait to ease working with `TargetResult` in the GdbStub
5 /// implementation.
6 pub(super) trait TargetResultExt<V, T, C> {
7     /// Encapsulates the boilerplate associated with handling `TargetError`s,
8     /// such as bailing-out on Fatal errors, or returning response codes.
handle_error(self) -> Result<V, GdbStubError<T, C>>9     fn handle_error(self) -> Result<V, GdbStubError<T, C>>;
10 }
11 
12 impl<V, T, C> TargetResultExt<V, T, C> for Result<V, TargetError<T>> {
handle_error(self) -> Result<V, GdbStubError<T, C>>13     fn handle_error(self) -> Result<V, GdbStubError<T, C>> {
14         let code = match self {
15             Ok(v) => return Ok(v),
16             Err(TargetError::Fatal(e)) => return Err(GdbStubError::TargetError(e)),
17             // Recoverable errors:
18             // Error code 121 corresponds to `EREMOTEIO` lol
19             Err(TargetError::NonFatal) => 121,
20             Err(TargetError::Errno(code)) => code,
21             #[cfg(feature = "std")]
22             Err(TargetError::Io(e)) => e.raw_os_error().unwrap_or(121) as u8,
23         };
24 
25         Err(GdbStubError::NonFatalError(code))
26     }
27 }
28