pub struct Policy { /* private fields */ }Expand description
A policy containing multiple rules
§Security
Fields are private to enforce validation through deserialization.
Use Policy::new() or deserialize from TOML/JSON to create instances.
The #[serde(try_from)] attribute ensures all deserialized policies
are validated against T20 limits (max rules, max name length, etc.).
Implementations§
Source§impl Policy
impl Policy
Sourcepub const fn valid_until(&self) -> u64
pub const fn valid_until(&self) -> u64
Get the expiration timestamp
Sourcepub fn rules(&self) -> &[PolicyRule]
pub fn rules(&self) -> &[PolicyRule]
Get a reference to the policy rules
Sourcepub fn new(
name: impl Into<String>,
valid_duration_secs: u64,
current_time: u64,
) -> Result<Self>
pub fn new( name: impl Into<String>, valid_duration_secs: u64, current_time: u64, ) -> Result<Self>
Sourcepub fn new_unversioned(name: impl Into<String>) -> Result<Self>
pub fn new_unversioned(name: impl Into<String>) -> Result<Self>
Create a policy without timestamps (for testing/legacy)
§Errors
Returns PolicyError::NameTooLong if name exceeds MAX_POLICY_NAME_LENGTH
Sourcepub fn add_rule(self, rule: PolicyRule) -> Result<Self>
pub fn add_rule(self, rule: PolicyRule) -> Result<Self>
Add a rule to this policy
§Errors
Returns PolicyError::TooManyRules if adding this rule would exceed MAX_RULES_PER_POLICY
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Add metadata to this policy
Sourcepub fn is_allowed(
&self,
peer_id: &str,
action: &Action,
resource: &Resource,
) -> bool
pub fn is_allowed( &self, peer_id: &str, action: &Action, resource: &Resource, ) -> bool
Check if a peer is allowed to perform an action on a resource
This method delegates to PolicyAuthorizer (SRP - Single Responsibility Principle).
The Policy struct focuses on construction and management, while authorization
logic is handled by the dedicated PolicyAuthorizer.
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validate policy (check for conflicts, invalid rules, etc.)
§Errors
Returns PolicyError::InvalidRule if:
- Policy name is empty
- Policy has no rules
- Any rule has an empty peer ID