Skip to main content

presolve_compiler/
action_authority.rs

1//! Adapter-fed V2 action authority.
2
3use serde::Serialize;
4
5pub const ACTION_AUTHORITY_SCHEMA_VERSION: u32 = 1;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
8#[serde(rename_all = "snake_case")]
9pub enum ActionCaptureCoverageV1 {
10    Complete,
11    Unavailable,
12}
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
14#[serde(rename_all = "snake_case")]
15pub enum ActionEnvironmentV1 {
16    Browser,
17    RejectedServerImport,
18}
19#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
20pub struct ActionFactV1 {
21    pub id: String,
22    pub component: String,
23    pub asynchronous: bool,
24    pub accepts_abort_signal: bool,
25    pub capture_coverage: ActionCaptureCoverageV1,
26    pub environment: ActionEnvironmentV1,
27}
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
29#[serde(rename_all = "snake_case")]
30pub enum ActionAdmissionV1 {
31    Admissible,
32    RejectedCaptureCoverage,
33    RejectedServerImport,
34}
35#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
36pub struct ActionRecordV1 {
37    pub id: String,
38    pub component: String,
39    pub asynchronous: bool,
40    pub accepts_abort_signal: bool,
41    pub cancellation_is_explicit: bool,
42    pub admission: ActionAdmissionV1,
43}
44#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
45pub struct ActionAuthorityV1 {
46    pub schema_version: u32,
47    pub actions: Vec<ActionRecordV1>,
48}
49#[must_use]
50pub fn build_action_authority_v1(facts: &[ActionFactV1]) -> ActionAuthorityV1 {
51    let mut actions = facts
52        .iter()
53        .map(|fact| ActionRecordV1 {
54            id: fact.id.clone(),
55            component: fact.component.clone(),
56            asynchronous: fact.asynchronous,
57            accepts_abort_signal: fact.accepts_abort_signal,
58            cancellation_is_explicit: fact.accepts_abort_signal,
59            admission: match (fact.environment, fact.capture_coverage) {
60                (ActionEnvironmentV1::RejectedServerImport, _) => {
61                    ActionAdmissionV1::RejectedServerImport
62                }
63                (_, ActionCaptureCoverageV1::Unavailable) => {
64                    ActionAdmissionV1::RejectedCaptureCoverage
65                }
66                _ => ActionAdmissionV1::Admissible,
67            },
68        })
69        .collect::<Vec<_>>();
70    actions.sort_by(|a, b| a.id.cmp(&b.id));
71    ActionAuthorityV1 {
72        schema_version: ACTION_AUTHORITY_SCHEMA_VERSION,
73        actions,
74    }
75}
76#[cfg(test)]
77mod tests {
78    use super::*;
79    #[test]
80    fn rejects_unproven_capture_and_server_imports() {
81        let result = build_action_authority_v1(&[ActionFactV1 {
82            id: "a".into(),
83            component: "c".into(),
84            asynchronous: true,
85            accepts_abort_signal: true,
86            capture_coverage: ActionCaptureCoverageV1::Unavailable,
87            environment: ActionEnvironmentV1::Browser,
88        }]);
89        assert_eq!(
90            result.actions[0].admission,
91            ActionAdmissionV1::RejectedCaptureCoverage
92        );
93    }
94}