1#![cfg_attr(feature = "unstable", feature(non_exhaustive))]
5
6use std::fmt;
7
8pub mod io;
9pub mod packet;
10pub mod parser;
11
12#[derive(Debug)]
13#[cfg_attr(feature = "unstable", non_exhaustive)]
14pub enum Error {
15 InvalidChecksum,
16 IoError(std::io::Error),
17 NonNumber(String, std::num::ParseIntError),
18 NonUtf8(Vec<u8>, std::str::Utf8Error),
19}
20impl fmt::Display for Error {
21 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 match self {
23 Error::InvalidChecksum => write!(f, "a packet with invalid checksum was sent and denied"),
24 Error::IoError(err) => write!(f, "i/o error: {}", err),
25 Error::NonNumber(string, err) => {
26 write!(f, "expected number, found {:?}: {}", string, err)
27 }
28 Error::NonUtf8(bytes, err) => write!(
29 f,
30 "expected UTF-8 string in this context, found {:?}: {}",
31 bytes, err
32 ),
33 }
34 }
35}
36impl std::error::Error for Error {
37 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38 match self {
39 Error::IoError(err) => Some(err),
40 Error::NonNumber(_, err) => Some(err),
41 Error::NonUtf8(_, err) => Some(err),
42 _ => None,
43 }
44 }
45}
46impl From<std::io::Error> for Error {
47 fn from(err: std::io::Error) -> Self {
48 Error::IoError(err)
49 }
50}