tws_rs/
errors.rs

1use std::{num::ParseIntError, string::FromUtf8Error};
2
3#[derive(Debug)]
4#[non_exhaustive]
5pub enum Error {
6    // Errors from external libraries
7    Io(std::io::Error),
8    ParseInt(ParseIntError),
9    FromUtf8(FromUtf8Error),
10    ParseTime(time::error::Parse),
11
12    // Errors from by IBAPI library
13    NotImplemented,
14    Parse(usize, String, String),
15    ServerVersion(i32, i32, String),
16    Simple(String),
17}
18
19impl std::error::Error for Error {}
20
21impl std::fmt::Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23        match self {
24            Error::Io(ref err) => err.fmt(f),
25            Error::ParseInt(ref err) => err.fmt(f),
26            Error::FromUtf8(ref err) => err.fmt(f),
27            Error::ParseTime(ref err) => err.fmt(f),
28
29            Error::NotImplemented => write!(f, "not implemented"),
30            Error::Parse(i, value, message) => write!(f, "parse error: {i} - {value} - {message}"),
31            Error::ServerVersion(wanted, have, message) => write!(f, "server version {wanted} required, got {have}: {message}"),
32
33            Error::Simple(ref err) => write!(f, "error occurred: {err}"),
34        }
35    }
36}
37
38impl From<std::io::Error> for Error {
39    fn from(err: std::io::Error) -> Error {
40        Error::Io(err)
41    }
42}
43
44impl From<ParseIntError> for Error {
45    fn from(err: ParseIntError) -> Error {
46        Error::ParseInt(err)
47    }
48}
49
50impl From<FromUtf8Error> for Error {
51    fn from(err: FromUtf8Error) -> Error {
52        Error::FromUtf8(err)
53    }
54}
55
56impl From<time::error::Parse> for Error {
57    fn from(err: time::error::Parse) -> Error {
58        Error::ParseTime(err)
59    }
60}