Skip to main content

GateBuilder

Struct GateBuilder 

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

Builds a Gate by registering checks in evaluation order.

Implementations§

Source§

impl GateBuilder

Source

pub fn new() -> Self

A builder with no checks, in Mode::Open.

Source

pub fn check<C: Check + 'static>(self, check: C) -> Self

Register a check. Checks evaluate in the order they are registered.

Source

pub fn check_boxed(self, check: Box<dyn Check>) -> Self

Register a pre-boxed check (for dynamically-assembled check sets).

Source

pub fn closed(self) -> Self

Evaluate in Mode::Closed: deny when no check decides.

use action_gate_core::{closed, ActionContext, Check, Decision, Gate};

struct AllowRead;
impl Check for AllowRead {
    fn id(&self) -> &str { "allow-read" }
    fn evaluate(&self, ctx: &ActionContext) -> Option<Decision> {
        (ctx.action == "read").then(Decision::allow)
    }
}

let gate = Gate::builder().check(AllowRead).closed().build();
assert!(gate.evaluate(&ActionContext::new("read")).is_allow());
let d = gate.evaluate(&ActionContext::new("write"));
assert_eq!(d.reason, closed::NO_CHECK_DECIDED);
assert!(d.blocking);
Source

pub fn require(self, id: impl Into<String>) -> Self

Require the check with id: it must be registered and must return Some on every evaluation, or the gate denies. Requiring a check puts the gate in Mode::Closed; there is no open gate with requirements. The requirement may be stated before or after the check is registered.

use action_gate_core::{closed, ActionContext, Check, Decision, Gate};

struct Scan;
impl Check for Scan {
    fn id(&self) -> &str { "scan" }
    fn evaluate(&self, _ctx: &ActionContext) -> Option<Decision> {
        Some(Decision::allow())
    }
}

// Required but never registered: every action is denied.
let gate = Gate::builder().require("scan").build();
let d = gate.evaluate(&ActionContext::new("write"));
assert_eq!(d.reason, closed::REQUIRED_UNREGISTERED);
assert_eq!(d.check_ids, vec!["scan"]);

let gate = Gate::builder().check(Scan).require("scan").build();
assert!(gate.evaluate(&ActionContext::new("write")).is_allow());
Source

pub fn require_all( self, ids: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Require every id in ids; see require.

Source

pub fn build(self) -> Gate

Finish building.

Source

pub fn build_deny_by_default(self) -> Gate

Register DenyByDefault as the terminal check and finish building: the gate denies any action no earlier check decides. An open-mode shorthand; see DenyByDefault on combining it with closed.

Trait Implementations§

Source§

impl Default for GateBuilder

Source§

fn default() -> Self

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.