haprox_rs/
error.rs

1/*-
2 * haprox-rs - a HaProxy protocol parser.
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * The scram-rs crate can be redistributed and/or modified
6 * under the terms of either of the following licenses:
7 *
8 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
9 *                     
10 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
11 */
12
13use std::fmt;
14
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub enum HaProxErrType
17{
18    /// Argument is invalid.
19    ArgumentEinval,
20
21    /// Input/Output error.
22    IoError,
23
24    /// Any error related to reading data from packet and 
25    /// producing error.
26    MalformedData,
27
28    /// The protocol version is not supported.
29    ProtocolNotSuported,
30
31    /// The banner of the packet is unknown.
32    IncorrectBanner,
33
34    /// Unexpected EOF while reading.
35    ProtocolMsgIncomplete,
36
37    /// Received unknown data.
38    ProtocolUnknownData
39}
40
41impl fmt::Display for HaProxErrType
42{
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
44    {
45        match self
46        {
47            HaProxErrType::ArgumentEinval => 
48                write!(f, "EINVAL"),
49            HaProxErrType::IoError => 
50                write!(f, "IOERROR"),
51            HaProxErrType::MalformedData => 
52                write!(f, "MALFORMED_DATA"),
53            HaProxErrType::ProtocolNotSuported => 
54                write!(f, "PROTO_NOT_SUPPORTED"),
55            HaProxErrType::IncorrectBanner =>
56                write!(f, "INCORRECT_BANNER"),
57            HaProxErrType::ProtocolMsgIncomplete => 
58                write!(f, "MESSAGE_INCOMPLETE"),
59            HaProxErrType::ProtocolUnknownData => 
60                write!(f, "UNKNOWN_DATA"),
61        }
62    }
63}
64
65/// Error description.
66#[derive(Clone, Debug, PartialEq, Eq)]
67pub struct HaProxErr
68{
69    /// Error type
70    err_type: HaProxErrType, 
71
72    /// Error msg
73    msg: String,
74}
75
76impl fmt::Display for HaProxErr
77{
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
79    {
80        write!(f, "[{}]: {}", self.err_type, self.msg)
81    }
82}
83
84impl HaProxErr
85{
86    /// Creates new error message.
87    pub 
88    fn new(err_type: HaProxErrType, msg: String) -> Self
89    {
90        return Self{ err_type: err_type, msg: msg };
91    }
92
93    /// Returns error type.
94    pub 
95    fn get_err_type(&self) -> HaProxErrType
96    {
97        return self.err_type;
98    }
99
100    /// Returns error msg.
101    pub 
102    fn get_msg(&self) -> &str
103    {
104        return &self.msg;
105    }
106}
107
108pub type HaProxRes<R> = Result<R, HaProxErr>;
109
110
111#[macro_export]
112macro_rules! return_error 
113{
114    ($err_type:tt, $($arg:tt)*) => (
115        return std::result::Result::Err($crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*)))
116    )
117}
118
119#[macro_export]
120macro_rules! map_error 
121{
122    ($err_type:tt, $($arg:tt)*) => (
123        $crate::error::HaProxErr::new($crate::error::HaProxErrType::$err_type, format!($($arg)*))
124    )
125}