Skip to main content

crucible_wire/permissions/
check.rs

1use super::{Obligation, Origin, PermissionInfo, ResourceRef};
2
3/// One question for the Authorizer: does the caller hold `permission` on
4/// `resource`? `Operation::CHECKS` produces one per declared clause that
5/// applies to the request; handlers make their own.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Check {
8    pub permission: &'static PermissionInfo,
9    pub resource: ResourceRef,
10    pub origin: Origin,
11}
12
13impl Check {
14    /// The check of clause `index` of the operation's requirements, whose
15    /// permission is `permission`, bound to `resource` from the input.
16    pub fn declared(
17        index: u16,
18        permission: &'static PermissionInfo,
19        resource: ResourceRef,
20    ) -> Self {
21        Self {
22            permission,
23            resource,
24            origin: Origin::Declared(index),
25        }
26    }
27
28    /// A check the handler makes that the model does not declare.
29    pub fn handler(permission: impl Into<&'static PermissionInfo>, resource: ResourceRef) -> Self {
30        Self {
31            permission: permission.into(),
32            resource,
33            origin: Origin::Handler,
34        }
35    }
36
37    /// The check that discharges `obligation` against `resource`.
38    pub fn obligation(obligation: &Obligation, resource: ResourceRef) -> Self {
39        Self {
40            permission: obligation.permission,
41            resource,
42            origin: Origin::Obligation(obligation.index),
43        }
44    }
45}