pfctl/rule/
direction.rs

1// Copyright 2025 Mullvad VPN AB.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::{Error, ErrorInternal, Result, ffi};
10
11/// Enum describing matching of rule towards packet flow direction.
12#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
13#[repr(u8)]
14pub enum Direction {
15    #[default]
16    Any = ffi::pfvar::PF_INOUT as u8,
17    In = ffi::pfvar::PF_IN as u8,
18    Out = ffi::pfvar::PF_OUT as u8,
19}
20
21impl From<Direction> for u8 {
22    fn from(direction: Direction) -> Self {
23        direction as u8
24    }
25}
26
27impl TryFrom<u8> for Direction {
28    type Error = crate::Error;
29
30    fn try_from(direction: u8) -> Result<Self> {
31        match direction {
32            v if v == Direction::Any as u8 => Ok(Direction::Any),
33            v if v == Direction::In as u8 => Ok(Direction::In),
34            v if v == Direction::Out as u8 => Ok(Direction::Out),
35            other => Err(Error::from(ErrorInternal::InvalidDirection(other))),
36        }
37    }
38}