1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use serde::Serialize;
use crate::{FactId, Policy, PolicyAnchor, PolicyId, required_facts};
/// Immutable policy with its versioned identity and required facts computed once.
///
/// Preparation does not execute application checks or cache their results.
#[derive(Clone, Debug)]
pub struct PreparedPolicy<O> {
policy: Policy<O>,
anchor: PolicyAnchor,
required: Vec<FactId>,
}
impl<O: Serialize> PreparedPolicy<O> {
/// Prepares a policy for repeated evaluation.
///
/// # Errors
/// Returns a serialization error if the outcome cannot be hashed.
pub fn new(id: PolicyId, policy: Policy<O>) -> Result<Self, postcard::Error> {
let anchor = PolicyAnchor::new(id, policy.hash()?);
let required = required_facts(&policy).into_iter().collect();
Ok(Self {
policy,
anchor,
required,
})
}
}
impl<O> PreparedPolicy<O> {
/// Borrows the original policy; its semantics and hash format are unchanged.
#[must_use]
pub const fn policy(&self) -> &Policy<O> {
&self.policy
}
/// Borrows the prepared policy identity.
#[must_use]
pub const fn anchor(&self) -> &PolicyAnchor {
&self.anchor
}
/// Borrows required identities in deterministic order.
#[must_use]
pub fn required_facts(&self) -> &[FactId] {
&self.required
}
}