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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use serde::{Deserialize, Serialize};
use std::fmt;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Error {
    Disconnected,
    AlreadyConnected,
    ChannelIdentifierError(String, ChannelIdentifierError),
    InvalidServerAddr(String, u16),
    InvalidUsername(String),
    InvalidServerPassword,
    Unimplemented,
    NotConnectedToChannel,
    ServerCertReject,
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Disconnected => write!(f, "Not connected to a server"),
            Error::AlreadyConnected => write!(f, "Already connected to a server"),
            Error::ChannelIdentifierError(id, kind) => write!(f, "{}: {}", kind, id),
            Error::InvalidServerAddr(addr, port) => {
                write!(f, "Invalid server address: {}: {}", addr, port)
            }
            Error::InvalidUsername(username) => write!(f, "Invalid username: {}", username),
            Error::InvalidServerPassword => write!(f, "Invalid server password"),
            Error::Unimplemented => write!(f, "Unimplemented"),
            Error::NotConnectedToChannel => write!(f, "Not connected to a channel"),
            Error::ServerCertReject => write!(f, "Invalid server certificate"),
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum ChannelIdentifierError {
    Invalid,
    Ambiguous,
}

impl fmt::Display for ChannelIdentifierError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ChannelIdentifierError::Invalid => write!(f, "Invalid channel identifier"),
            ChannelIdentifierError::Ambiguous => write!(f, "Ambiguous channel identifier"),
        }
    }
}

impl std::error::Error for ChannelIdentifierError {}

#[derive(Debug)]
pub enum ConfigError {
    InvalidConfig,
    TomlError(toml_edit::TomlError),
    TomlErrorSer(toml_edit::ser::Error),
    TomlErrorDe(toml_edit::de::Error),

    WontCreateFile,
    IOError(std::io::Error),
}

impl std::error::Error for ConfigError {}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConfigError::InvalidConfig => write!(f, "Invalid configuration"),
            ConfigError::TomlError(e) => write!(f, "Invalid TOML: {}", e),
            ConfigError::TomlErrorSer(e) => write!(f, "Invalid TOML when serializing: {}", e),
            ConfigError::TomlErrorDe(e) => write!(f, "Invalid TOML when deserializing: {}", e),
            ConfigError::WontCreateFile => {
                write!(f, "File does not exist but caller didn't allow creation")
            }
            ConfigError::IOError(e) => write!(f, "IO error: {}", e),
        }
    }
}

impl From<std::io::Error> for ConfigError {
    fn from(e: std::io::Error) -> Self {
        ConfigError::IOError(e)
    }
}

impl From<toml_edit::ser::Error> for ConfigError {
    fn from(e: toml_edit::ser::Error) -> Self {
        ConfigError::TomlErrorSer(e)
    }
}

impl From<toml_edit::de::Error> for ConfigError {
    fn from(e: toml_edit::de::Error) -> Self {
        ConfigError::TomlErrorDe(e)
    }
}

impl From<toml_edit::TomlError> for ConfigError {
    fn from(e: toml_edit::TomlError) -> Self {
        ConfigError::TomlError(e)
    }
}