selinux/new_policy/
traits.rs1use super::NewPolicy;
6use super::error::{ParseError, SerializeError, ValidateError};
7use super::parser::PolicyCursor;
8
9pub trait Parse: Sized {
11 fn parse(cursor: &mut PolicyCursor<'_>) -> Result<Self, ParseError>;
12}
13
14pub trait Serialize {
16 fn serialize(&self, writer: &mut Vec<u8>) -> Result<(), SerializeError>;
17}
18
19pub trait Validate {
21 fn validate(&self, policy: &NewPolicy) -> Result<(), ValidateError>;
22}
23
24pub trait PolicyId:
30 Copy + Clone + std::fmt::Debug + Eq + std::hash::Hash + Ord + PartialOrd
31{
32 fn as_u32(&self) -> u32;
34
35 fn from_u32(value: u32) -> Option<Self>;
38}
39
40impl<T> Parse for T
42where
43 T: PolicyId,
44{
45 fn parse(cursor: &mut PolicyCursor<'_>) -> Result<Self, ParseError> {
46 let value = u32::parse(cursor)?;
47 T::from_u32(value).ok_or(ParseError::InvalidId { value })
48 }
49}
50
51impl<T> Serialize for T
52where
53 T: PolicyId,
54{
55 fn serialize(&self, writer: &mut Vec<u8>) -> Result<(), SerializeError> {
56 self.as_u32().serialize(writer)
57 }
58}
59
60pub trait HasName {
62 fn name(&self) -> &[u8];
63}
64
65pub trait HasPolicyId {
67 type Id: PolicyId;
68 fn id(&self) -> Self::Id;
69}
70
71impl Validate for Box<[u8]> {
72 fn validate(&self, _policy: &NewPolicy) -> Result<(), ValidateError> {
73 Ok(())
74 }
75}
76
77impl<T: Validate> Validate for Box<T> {
78 fn validate(&self, policy: &NewPolicy) -> Result<(), ValidateError> {
79 (**self).validate(policy)
80 }
81}
82
83impl Validate for bool {
84 fn validate(&self, _policy: &NewPolicy) -> Result<(), ValidateError> {
85 Ok(())
86 }
87}
88
89impl<T: Validate> Validate for Option<T> {
90 fn validate(&self, policy: &NewPolicy) -> Result<(), ValidateError> {
91 if let Some(value) = self {
92 value.validate(policy)?;
93 }
94 Ok(())
95 }
96}