steam_tui/util/
error.rs

1use crate::client::Command;
2
3use shellexpand::LookupError;
4use std::error;
5use std::fmt;
6use std::fmt::Debug;
7use std::io;
8use std::str::Utf8Error;
9use std::sync::mpsc::{RecvError, SendError};
10use std::sync::PoisonError;
11
12#[derive(Debug)]
13pub enum STError {
14    Io(io::Error),
15    Process(io::Error),
16    Recv(RecvError),
17    Problem(String),
18}
19
20impl From<io::Error> for STError {
21    fn from(err: io::Error) -> STError {
22        STError::Io(err)
23    }
24}
25
26impl From<RecvError> for STError {
27    fn from(err: RecvError) -> STError {
28        STError::Recv(err)
29    }
30}
31
32impl From<SendError<Command>> for STError {
33    fn from(err: SendError<Command>) -> STError {
34        STError::Problem(format!("{:?}", err))
35    }
36}
37
38impl From<SendError<String>> for STError {
39    fn from(err: SendError<String>) -> STError {
40        STError::Problem(format!("{:?}", err))
41    }
42}
43
44impl From<Box<dyn error::Error>> for STError {
45    fn from(err: Box<dyn error::Error>) -> STError {
46        STError::Problem(format!("{:?}", err))
47    }
48}
49
50impl From<serde_json::Error> for STError {
51    fn from(err: serde_json::Error) -> STError {
52        STError::Problem(format!("{:?}", err))
53    }
54}
55
56impl From<Utf8Error> for STError {
57    fn from(err: Utf8Error) -> STError {
58        STError::Problem(format!("{:?}", err))
59    }
60}
61
62impl<T> From<PoisonError<T>> for STError {
63    fn from(err: PoisonError<T>) -> STError {
64        STError::Problem(format!("{:?}", err))
65    }
66}
67
68impl<T: Debug> From<LookupError<T>> for STError {
69    fn from(err: LookupError<T>) -> STError {
70        STError::Problem(format!("{:?}", err))
71    }
72}
73
74impl fmt::Display for STError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match &*self {
77            STError::Process(e) => write!(
78                f,
79                "An error occured spawning the steamcmd process. Do you have it installed?\n{:?}",
80                e
81            ),
82            _ => write!(f, "{:?}", self),
83        }
84    }
85}
86
87impl error::Error for STError {
88    fn description(&self) -> &str {
89        "woosp"
90    }
91
92    fn cause(&self) -> Option<&dyn error::Error> {
93        // Pass on reference
94        None
95    }
96}