haprox_rs/
error.rs

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