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
//! An implementation of the GDB Remote Serial Protocol, following
//! https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html

#![cfg_attr(feature = "unstable", feature(non_exhaustive))]

use std::fmt;

pub mod io;
pub mod packet;
pub mod parser;

#[derive(Debug)]
#[cfg_attr(feature = "unstable", non_exhaustive)]
pub enum Error {
    InvalidChecksum,
    IoError(std::io::Error),
    NonNumber(String, std::num::ParseIntError),
    NonUtf8(Vec<u8>, std::str::Utf8Error),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::InvalidChecksum => write!(f, "a packet with invalid checksum was sent and denied"),
            Error::IoError(err) => write!(f, "i/o error: {}", err),
            Error::NonNumber(string, err) => {
                write!(f, "expected number, found {:?}: {}", string, err)
            }
            Error::NonUtf8(bytes, err) => write!(
                f,
                "expected UTF-8 string in this context, found {:?}: {}",
                bytes, err
            ),
        }
    }
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::IoError(err) => Some(err),
            Error::NonNumber(_, err) => Some(err),
            Error::NonUtf8(_, err) => Some(err),
            _ => None,
        }
    }
}
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::IoError(err)
    }
}