Skip to main content

edc_web_components/components/
contraint_renderer.rs

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