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
60impl<T> Validate for T
61where
62 T: PolicyId,
63{
64 fn validate(&self, _policy: &NewPolicy) -> Result<(), ValidateError> {
65 Ok(())
66 }
67}
68
69pub trait HasName {
71 fn name(&self) -> &[u8];
72}
73
74pub trait HasPolicyId {
76 type Id: PolicyId;
77 fn id(&self) -> Self::Id;
78}
79
80impl Validate for Box<[u8]> {
81 fn validate(&self, _policy: &NewPolicy) -> Result<(), ValidateError> {
82 Ok(())
83 }
84}