Skip to main content

edc_web_components/models/
policy_definition.rs

1use crate::models::PolicyKind;
2use edc_connector_client::types::policy::{
3  Obligation, Permission, Policy, PolicyDefinition, Prohibition,
4};
5use std::collections::HashMap;
6
7#[derive(Clone, Debug, PartialEq)]
8pub struct PolicyDefinitionItem {
9  pub id: String,
10  pub name: String,
11  pub kind: String,
12  pub permissions: Vec<Permission>,
13  pub obligations: Vec<Obligation>,
14  pub prohibitions: Vec<Prohibition>,
15  pub extensible_properties: HashMap<String, serde_json::Value>,
16  pub assignee: Option<String>,
17  pub assigner: Option<String>,
18}
19
20impl From<PolicyDefinition> for PolicyDefinitionItem {
21  fn from(policy_definition: PolicyDefinition) -> Self {
22    PolicyDefinitionItem {
23      id: policy_definition.id().to_string(),
24      name: policy_definition
25        .private_property("name")
26        .ok()
27        .and_then(|name| name)
28        .unwrap_or(policy_definition.id().to_string()),
29      kind: PolicyKind::from(policy_definition.policy().kind()).to_string(),
30      permissions: policy_definition.policy().permissions().to_vec(),
31      obligations: policy_definition.policy().obligations().to_vec(),
32      prohibitions: policy_definition.policy().prohibitions().to_vec(),
33      extensible_properties: policy_definition.policy().extensible_properties().clone(),
34      assignee: policy_definition
35        .policy()
36        .assignee()
37        .map(|assignee| assignee.to_string()),
38      assigner: policy_definition
39        .policy()
40        .assigner()
41        .map(|assigner| assigner.to_string()),
42    }
43  }
44}
45
46impl From<&Policy> for PolicyDefinitionItem {
47  fn from(policy: &Policy) -> Self {
48    PolicyDefinitionItem {
49      id: policy.id().cloned().unwrap_or_default(),
50      name: policy.id().cloned().unwrap_or_default(),
51      kind: PolicyKind::from(policy.kind()).to_string(),
52      permissions: policy.permissions().to_vec(),
53      obligations: policy.obligations().to_vec(),
54      prohibitions: policy.prohibitions().to_vec(),
55      extensible_properties: policy.extensible_properties().clone(),
56      assignee: policy.assignee().cloned(),
57      assigner: policy.assigner().cloned(),
58    }
59  }
60}