PolicyRuleBuilder

Struct PolicyRuleBuilder 

Source
pub struct PolicyRuleBuilder { /* private fields */ }
Expand description

Builder for creating PolicyRule instances with a fluent API

§Examples

use core_policy::builder::PolicyRuleBuilder;
use core_policy::{Action, Resource};

// Basic RBAC rule
let rule = PolicyRuleBuilder::new()
    .for_peer("12D3KooWXYZ...")
    .allow(Action::Read)
    .on(Resource::File("/docs/*".into()))
    .build()
    .unwrap();

// ABAC rule with expiration
let rule = PolicyRuleBuilder::new()
    .for_peer("technician")
    .allow(Action::Read)
    .on(Resource::File("/logs/*".into()))
    .expires_at(1762348800)
    .build()
    .unwrap();

// ABAC rule with attributes
let rule = PolicyRuleBuilder::new()
    .for_peer("alice")
    .allow(Action::Write)
    .on(Resource::File("/shared/*".into()))
    .with_attribute("location", "office")
    .with_attribute("security_level", "high")
    .build()
    .unwrap();

// ABAC rule with context expression (advanced)
let rule = PolicyRuleBuilder::new()
    .for_peer("alice")
    .allow(Action::Read)
    .on(Resource::File("/sensitive/*".into()))
    .with_context_expr("role == \"admin\" AND department == \"IT\"")?  // Returns Result
    .build()
    .unwrap();

Implementations§

Source§

impl PolicyRuleBuilder

Source

pub fn new() -> Self

Create a new builder

Source

pub fn for_peer(self, peer_id: impl Into<String>) -> Self

Set the peer ID that this rule applies to

Source

pub fn allow(self, action: Action) -> Self

Set the action allowed by this rule

Source

pub fn on(self, resource: Resource) -> Self

Set the resource this rule applies to

Source

pub const fn expires_at(self, timestamp: u64) -> Self

Set the expiration timestamp (Unix seconds) - ABAC

Source

pub fn with_attribute( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Add an attribute for contextual access control - ABAC (legacy)

Source

pub fn with_context_expr(self, expr: impl AsRef<str>) -> Result<Self>

Add a context expression for advanced ABAC (boolean logic)

§Arguments
  • expr - Expression string to parse (e.g., “role == "admin" AND department == "IT"”)
§Errors

Returns PolicyError::InvalidExpression if the expression syntax is invalid

§Example
use core_policy::builder::PolicyRuleBuilder;
use core_policy::{Action, Resource};

let rule = PolicyRuleBuilder::new()
    .for_peer("alice")
    .allow(Action::Read)
    .on(Resource::All)
    .with_context_expr("role == \"admin\" AND active == \"true\"")?  // Returns Result
    .build()
    .unwrap();
Source

pub fn build(self) -> Result<PolicyRule>

Build the PolicyRule, returning an error if required fields are missing

§Errors

Returns PolicyError::InvalidRule if any required field is missing:

  • peer_id
  • action
  • resource

Trait Implementations§

Source§

impl Debug for PolicyRuleBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PolicyRuleBuilder

Source§

fn default() -> PolicyRuleBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.