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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//  ERRORS.rs
//    by Lut99
//
//  Created:
//    04 Oct 2022, 11:09:56
//  Last edited:
//    07 Jun 2023, 16:27:48
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines errors that occur in the `brane-cfg` crate.
//

use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FResult};
use std::path::PathBuf;


/***** LIBRARY *****/
/// Errors that relate to certificate loading and such.
#[derive(Debug)]
pub enum CertsError {
    /// A given certificate file could not be parsed.
    ClientCertParseError { err: x509_parser::nom::Err<x509_parser::error::X509Error> },
    /// A given certificate did not have the `CN`-field specified.
    ClientCertNoCN { subject: String },

    /// Failed to open a given file.
    FileOpenError { what: &'static str, path: PathBuf, err: std::io::Error },
    /// Failed to read a given file.
    FileReadError { what: &'static str, path: PathBuf, err: std::io::Error },
    /// Encountered unknown item in the given file.
    UnknownItemError { what: &'static str, path: PathBuf },

    /// Failed to parse the certificate file.
    CertFileParseError { path: PathBuf, err: std::io::Error },
    /// Failed to parse the key file.
    KeyFileParseError { path: PathBuf, err: std::io::Error },

    /// The given certificate file was empty.
    EmptyCertFile { path: PathBuf },
    /// The given keyfile was empty.
    EmptyKeyFile { path: PathBuf },
}
impl Display for CertsError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use CertsError::*;
        match self {
            ClientCertParseError { err } => write!(f, "Failed to parse given client certificate file: {err}"),
            ClientCertNoCN { subject } => write!(f, "Certificate subject field '{subject}' does not specify a CN"),

            FileOpenError { what, path, err } => write!(f, "Failed to open {} file '{}': {}", what, path.display(), err),
            FileReadError { what, path, err } => write!(f, "Failed to read {} file '{}': {}", what, path.display(), err),
            UnknownItemError { what, path } => write!(f, "Encountered non-certificate, non-key item in {} file '{}'", what, path.display()),

            CertFileParseError { path, err } => write!(f, "Failed to parse certificates in '{}': {}", path.display(), err),
            KeyFileParseError { path, err } => write!(f, "Failed to parse keys in '{}': {}", path.display(), err),

            EmptyCertFile { path } => write!(f, "No certificates found in file '{}'", path.display()),
            EmptyKeyFile { path } => write!(f, "No keys found in file '{}'", path.display()),
        }
    }
}
impl Error for CertsError {}



/// Errors that relate to a NodeConfig.
#[derive(Debug)]
pub enum NodeConfigError {
    /// Failed to open the given config path.
    FileOpenError { path: PathBuf, err: std::io::Error },
    /// Failed to read from the given config path.
    FileReadError { path: PathBuf, err: std::io::Error },
    /// Failed to parse the given file.
    FileParseError { path: PathBuf, err: serde_yaml::Error },

    /// Failed to open the given config path.
    FileCreateError { path: PathBuf, err: std::io::Error },
    /// Failed to write to the given config path.
    FileWriteError { path: PathBuf, err: std::io::Error },
    /// Failed to serialze the NodeConfig.
    ConfigSerializeError { err: serde_yaml::Error },

    /// Failed to write to the given writer.
    WriterWriteError { err: std::io::Error },
}
impl Display for NodeConfigError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use NodeConfigError::*;
        match self {
            FileOpenError { path, err } => write!(f, "Failed to open the node config file '{}': {}", path.display(), err),
            FileReadError { path, err } => write!(f, "Failed to read the ndoe config file '{}': {}", path.display(), err),
            FileParseError { path, err } => write!(f, "Failed to parse node config file '{}' as YAML: {}", path.display(), err),

            FileCreateError { path, err } => write!(f, "Failed to create the node config file '{}': {}", path.display(), err),
            FileWriteError { path, err } => write!(f, "Failed to write to the ndoe config file '{}': {}", path.display(), err),
            ConfigSerializeError { err } => write!(f, "Failed to serialize node config to YAML: {err}"),

            WriterWriteError { err } => write!(f, "Failed to write to given writer: {err}"),
        }
    }
}
impl Error for NodeConfigError {}

/// Defines errors that may occur when parsing proxy protocol strings.
#[derive(Debug)]
pub enum ProxyProtocolParseError {
    /// The protocol (version) is unknown to us.
    UnknownProtocol { raw: String },
}
impl Display for ProxyProtocolParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use ProxyProtocolParseError::*;
        match self {
            UnknownProtocol { raw } => write!(f, "Unknown proxy protocol '{raw}'"),
        }
    }
}
impl Error for ProxyProtocolParseError {}

/// Defines errors that may occur when parsing node kind strings.
#[derive(Debug)]
pub enum NodeKindParseError {
    /// The given NodeKind was unknown to us.
    UnknownNodeKind { raw: String },
}
impl Display for NodeKindParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use NodeKindParseError::*;
        match self {
            UnknownNodeKind { raw } => write!(f, "Unknown node kind '{raw}'"),
        }
    }
}
impl Error for NodeKindParseError {}