Skip to main content

edc_web_components/components/
contraint_renderer.rs

1use crate::contexts::use_policy_constraint_renderer_context;
2use edc_connector_client::types::policy::{Constraint, LeftOperand, Operator};
3use patternfly_yew::prelude::*;
4use yew::prelude::*;
5
6#[derive(Clone, Debug, PartialEq, Properties)]
7pub struct ConstraintRendererProps {
8  pub action: edc_connector_client::types::policy::Action,
9  pub constraints: Vec<Constraint>,
10}
11
12#[component]
13pub fn ConstraintRenderer(props: &ConstraintRendererProps) -> Html {
14  use edc_connector_client::types::policy::Action;
15
16  let policy_constraint_renderer_context = use_policy_constraint_renderer_context();
17
18  let action = match &props.action {
19    Action::Simple(simple) => simple.to_string(),
20    Action::Id { id } => id.to_string(),
21  };
22
23  let constraints = props.constraints.iter().map(|constraint| {
24    let (title, rendered_context) = if let Some((title, rendered_context)) =
25      policy_constraint_renderer_context
26        .as_ref()
27        .and_then(|policy_constraint_renderer_context| {
28          policy_constraint_renderer_context.renderer_constraint(&action, constraint)
29        }) {
30      (title, rendered_context)
31    } else {
32      match constraint {
33        Constraint::Atomic(atomic_constraint) => {
34          let left_operand = match &atomic_constraint.left_operand {
35            LeftOperand::Id { id } => id.to_string(),
36            LeftOperand::Simple(simple) => simple.to_string(),
37          };
38
39          let operator = match &atomic_constraint.operator {
40            Operator::Id { id } => id.to_string(),
41            Operator::Simple(simple) => simple.to_string(),
42          };
43
44          let operator = match operator
45            .replace("http://www.w3.org/ns/odrl/2/", "")
46            .as_str()
47          {
48            "eq" => "Equal".to_string(),
49            "neq" => "Not equal".to_string(),
50            "gt" => "Greater than".to_string(),
51            "gteq" => "Greater than or equal".to_string(),
52            "lt" => "Less than".to_string(),
53            "lteq" => "Less than or equal".to_string(),
54            "term-lteq" => "Term less than or equal".to_string(),
55            "hasPart" => "Has part".to_string(),
56            "isA" => "Is a".to_string(),
57            "isAllOf" => "Is all of".to_string(),
58            "isAnyOf" => "Is any of".to_string(),
59            "isNoneOf" => "Is none of".to_string(),
60            "isPartOf" => "Is part of".to_string(),
61            _ => operator,
62          };
63
64          let right_operand = match &atomic_constraint.right_operand.0 {
65            serde_json::Value::String(content) => content.to_string(),
66            value => value.to_string(),
67          };
68
69          let action = action.replace("http://www.w3.org/ns/odrl/2/", "");
70
71          let inner = html!(
72            <Flex>
73              <FlexItem>{ left_operand }</FlexItem>
74              <FlexItem>
75                <Label label={operator} color={Color::Blue} />
76              </FlexItem>
77              <FlexItem>{ right_operand }</FlexItem>
78            </Flex>
79          );
80
81          (format!("{action} Constraint"), inner)
82        }
83        Constraint::MultiplicityConstraint(multiplicity_constraint) => (
84          format!("{action} Multiplicity Constraint"),
85          html! { format!("{multiplicity_constraint:?}") },
86        ),
87      }
88    };
89
90    html_nested!(
91      <StackItem>
92        <Alert {title} r#type={AlertType::Info}>{ rendered_context }</Alert>
93      </StackItem>
94    )
95  });
96
97  html! { <Stack gutter=true>{ for constraints }</Stack> }
98}