stcp_scraper/
error.rs

1use std::fmt::Display;
2
3use serde::{Serialize, Serializer};
4
5#[derive(Debug)]
6pub enum Error {
7    Request(reqwest::Error),
8    Parse(String),
9    InvalidStop,
10    NoBuses,
11    ServerError,
12}
13
14impl PartialEq for Error {
15    fn eq(&self, other: &Self) -> bool {
16        match (self, other) {
17            (Error::Request(req), Error::Request(other_req)) => req.to_string() == other_req.to_string(),
18            (Error::Request(_), _) => false,
19            (_, Error::Request(_)) => false,
20            (Error::Parse(s1), Error::Parse(s2)) => s1 == s2,
21            (Error::NoBuses, Error::NoBuses) => true,
22            (Error::InvalidStop, Error::InvalidStop) => true,
23            (Error::ServerError, Error::ServerError) => true,
24            _ => false
25        }
26    }
27}
28
29impl Serialize for Error {
30    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
31        S: Serializer {
32        match self {
33            Error::Request(req) => serializer.serialize_str(&req.to_string()),
34            Error::Parse(string) => serializer.serialize_str(string),
35            Error::NoBuses => serializer.serialize_str("NO_BUSES_FOUND"),
36            Error::InvalidStop => serializer.serialize_str("INVALID_STOP_CODE"),
37            Error::ServerError => serializer.serialize_str("SERVER_ERROR"),
38        }
39    }
40}
41
42impl From<String> for Error {
43    fn from(error: String) -> Self {
44        Error::Parse(error)
45    }
46}
47
48impl From<reqwest::Error> for Error {
49    fn from(error: reqwest::Error) -> Self {
50        Error::Request(error)
51    }
52}
53
54impl Display for Error {
55    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
56        match *self {
57            Error::Request(ref inner) => ::std::fmt::Display::fmt(inner, f),
58            Error::Parse(ref inner) => ::std::fmt::Display::fmt(inner, f),
59            Error::NoBuses => ::std::fmt::Display::fmt("NoBuses", f),
60            Error::InvalidStop => ::std::fmt::Display::fmt("InvalidStop", f),
61            Error::ServerError => ::std::fmt::Display::fmt("ServerError", f),
62        }
63    }
64}
65
66impl std::error::Error for Error {}