aboutsummaryrefslogtreecommitdiff
path: root/src/expr/log.rs
blob: cc2728e9e35e4347e40eeb02ae3f7a42d702ff06 (plain) (blame)
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
use rustables_macros::nfnetlink_struct;

use super::Expression;
use crate::{
    error::BuilderError,
    sys::{NFTA_LOG_GROUP, NFTA_LOG_PREFIX},
};

#[derive(Clone, PartialEq, Eq, Default, Debug)]
#[nfnetlink_struct]
/// A Log expression will log all packets that match the rule.
pub struct Log {
    #[field(NFTA_LOG_GROUP)]
    group: u16,
    #[field(NFTA_LOG_PREFIX)]
    prefix: String,
}

impl Log {
    pub fn new(group: Option<u16>, prefix: Option<impl Into<String>>) -> Result<Log, BuilderError> {
        let mut res = Log::default();
        if let Some(group) = group {
            res.set_group(group);
        }
        if let Some(prefix) = prefix {
            let prefix = prefix.into();

            if prefix.bytes().count() > 127 {
                return Err(BuilderError::TooLongLogPrefix);
            }
            res.set_prefix(prefix);
        }
        Ok(res)
    }
}

impl Expression for Log {
    fn get_name() -> &'static str {
        "log"
    }
}