pf_rs/
runtime_exception.rs

1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021 Aleksandr Morozov, RELKOM s.r.o
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 *    - Redistributions of source code must retain the above copyright
12 *      notice, this list of conditions and the following disclaimer.
13 *    - Redistributions in binary form must reproduce the above
14 *      copyright notice, this list of conditions and the following
15 *      disclaimer in the documentation and/or other materials provided
16 *      with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33use std::fmt;
34use nix::libc;
35
36#[derive(Copy, Clone, Eq, PartialEq)]
37pub enum PfErrCode
38{
39    None,
40    RuntimeAssertion,
41    LibcGetAddrInfo(i32),
42    UnknownAddressFamily(i32),
43    CanNotParseAddress,
44    /// IOCTL error: title, code
45    IoCtlError(&'static str, libc::c_ulong),
46    /// Some error while processing input or probably errors in code
47    InternalError,
48    /// Incorrect input data
49    Einval,
50    /// Io errors,
51    IOErr,
52    /// tokenizer error
53    Tokenizer,
54}
55
56pub struct PfError 
57{
58    /// Error code
59    err_code: PfErrCode,
60    /// Description
61    message: String,
62}
63
64impl PfError
65{
66    /// Creates new instance without any code
67    pub fn new(msg: String) -> Self
68    {
69        return PfError{err_code: PfErrCode::None, message: msg};
70    }
71
72    /// Creates new instance with code number and message
73    pub fn new_code(code: PfErrCode, msg: String) -> Self
74    {
75        return PfError{err_code: code, message: msg};
76    }
77
78    /// returns the kind of the error as [`PfErrCode`]
79    pub fn kind(&self) -> PfErrCode
80    {
81        return self.err_code;
82    }
83}
84
85impl fmt::Display for PfError 
86{
87    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
88    {
89        write!(f, "{}", self.message)
90    }
91}
92impl fmt::Debug for PfError 
93{
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
95    {
96        write!(f, "{}", self.message)
97    }
98}
99
100
101pub type PfResult<T> = Result<T, PfError>;
102
103
104#[macro_export]
105macro_rules! error 
106{
107    ($($arg:tt)*) => (
108        return std::result::Result::Err(PfError::new(format!($($arg)*)))
109    )
110}
111
112#[macro_export]
113macro_rules! map_error 
114{
115    ($($arg:tt)*) => (
116        return PfError::new(format!($($arg)*))
117    )
118}
119
120
121#[macro_export]
122macro_rules! error_coded 
123{
124    ($code:expr,$($arg:tt)*) => (
125        return std::result::Result::Err($crate::runtime_exception::PfError::new_code($code, format!($($arg)*)))
126    )
127}
128
129#[macro_export]
130macro_rules! map_error_coded 
131{
132    ($code:expr,$($arg:tt)*) => (
133        return $crate::runtime_exception::PfError::new_code($code, format!($($arg)*))
134    )
135}
136