edc-connector-client 0.4.0

A Rust client for EDC
Documentation
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
mod odrl;

use bon::Builder;
use serde::{Deserialize, Serialize};
use serde_with::{formats::PreferMany, serde_as, OneOrMany};

use crate::ConversionError;

use super::properties::{FromValue, Properties, PropertyValue, ToValue};

#[derive(Debug, Serialize, Deserialize, Clone, Builder)]
#[serde(rename_all = "camelCase")]
pub struct PolicyDefinition {
    #[builder(field)]
    #[serde(default)]
    private_properties: Properties,
    #[builder(into)]
    #[serde(rename = "@id")]
    id: String,
    policy: Policy,
}

impl<S: policy_definition_builder::State> PolicyDefinitionBuilder<S> {
    pub fn private_property<T>(mut self, property: &str, value: T) -> Self
    where
        T: ToValue,
    {
        self.private_properties.set(property, value);
        self
    }
}

impl PolicyDefinition {
    pub fn policy(&self) -> &Policy {
        &self.policy
    }

    pub fn private_property<T>(&self, property: &str) -> Result<Option<T>, ConversionError>
    where
        T: FromValue,
    {
        self.private_properties.get(property)
    }

    pub fn id(&self) -> &str {
        &self.id
    }
}

#[derive(Debug, Serialize, Deserialize, Builder)]
#[serde(rename_all = "camelCase")]
pub struct NewPolicyDefinition {
    #[builder(field)]
    #[serde(default)]
    private_properties: Properties,
    #[builder(into)]
    #[serde(rename = "@id")]
    id: Option<String>,
    policy: Policy,
}

impl<S: new_policy_definition_builder::State> NewPolicyDefinitionBuilder<S> {
    pub fn private_property<T>(mut self, property: &str, value: T) -> Self
    where
        T: ToValue,
    {
        self.private_properties.set(property, value);
        self
    }
}

impl Default for PolicyDefinition {
    fn default() -> Self {
        Self {
            id: String::default(),
            policy: Policy::builder().build(),
            private_properties: Properties::default(),
        }
    }
}

impl Default for NewPolicyDefinition {
    fn default() -> Self {
        Self {
            id: Option::default(),
            policy: Policy::builder().build(),
            private_properties: Properties::default(),
        }
    }
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Builder)]
pub struct Policy {
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "permission", alias = "odrl:permission", default)]
    permissions: Vec<Permission>,
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "obligation", alias = "odrl:obligation", default)]
    obligations: Vec<Obligation>,
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "prohibition", alias = "odrl:prohibition", default)]
    prohibitions: Vec<Prohibition>,
    #[builder(into)]
    #[serde(rename = "@id")]
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<String>,
    #[builder(default)]
    #[serde(rename = "@type")]
    kind: PolicyKind,
    #[builder(into)]
    #[serde(alias = "odrl:assignee")]
    assignee: Option<String>,
    #[builder(into)]
    #[serde(alias = "odrl:assigner")]
    assigner: Option<String>,
    #[builder(into)]
    #[serde(alias = "odrl:target")]
    target: Option<Target>,
}

impl Policy {
    pub fn kind(&self) -> &PolicyKind {
        &self.kind
    }

    pub fn id(&self) -> Option<&String> {
        self.id.as_ref()
    }

    pub fn assignee(&self) -> Option<&String> {
        self.assignee.as_ref()
    }

    pub fn assigner(&self) -> Option<&String> {
        self.assigner.as_ref()
    }

    pub fn target(&self) -> Option<&Target> {
        self.target.as_ref()
    }

    pub fn permissions(&self) -> &[Permission] {
        &self.permissions
    }

    pub fn obligations(&self) -> &[Obligation] {
        &self.obligations
    }

    pub fn prohibitions(&self) -> &[Prohibition] {
        &self.prohibitions
    }
}

impl<S: policy_builder::State> PolicyBuilder<S> {
    pub fn permissions(mut self, permissions: Vec<Permission>) -> Self {
        self.permissions = permissions;
        self
    }

    pub fn permission(mut self, permission: Permission) -> Self {
        self.permissions.push(permission);
        self
    }

    pub fn prohibitions(mut self, prohibitions: Vec<Prohibition>) -> Self {
        self.prohibitions = prohibitions;
        self
    }

    pub fn prohibition(mut self, prohibition: Prohibition) -> Self {
        self.prohibitions.push(prohibition);
        self
    }

    pub fn obligations(mut self, obligations: Vec<Obligation>) -> Self {
        self.obligations = obligations;
        self
    }

    pub fn obligation(mut self, obligation: Obligation) -> Self {
        self.obligations.push(obligation);
        self
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Default)]
pub enum PolicyKind {
    #[default]
    #[serde(alias = "odrl:Set")]
    Set,
    #[serde(alias = "odrl:Offer")]
    Offer,
    #[serde(alias = "odrl:Agreement")]
    Agreement,
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Builder)]
pub struct Permission {
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "constraint", alias = "odrl:constraint", default)]
    constraints: Vec<Constraint>,
    #[builder(default)]
    #[serde(alias = "odrl:action")]
    action: Action,
}

impl Permission {
    pub fn action(&self) -> &Action {
        &self.action
    }

    pub fn constraints(&self) -> &[Constraint] {
        &self.constraints
    }
}

impl<S: permission_builder::State> PermissionBuilder<S> {
    pub fn constraints(mut self, constraints: Vec<Constraint>) -> Self {
        self.constraints = constraints;
        self
    }

    pub fn constraint(mut self, constraint: Constraint) -> Self {
        self.constraints.push(constraint);
        self
    }
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Builder)]
pub struct Obligation {
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "constraint", alias = "odrl:constraint", default)]
    constraints: Vec<Constraint>,
    #[serde(alias = "odrl:action")]
    action: Action,
}

impl Obligation {
    pub fn action(&self) -> &Action {
        &self.action
    }

    pub fn constraints(&self) -> &[Constraint] {
        &self.constraints
    }
}

impl<S: obligation_builder::State> ObligationBuilder<S> {
    pub fn constraints(mut self, constraints: Vec<Constraint>) -> Self {
        self.constraints = constraints;
        self
    }

    pub fn constraint(mut self, constraint: Constraint) -> Self {
        self.constraints.push(constraint);
        self
    }
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Builder)]
pub struct Prohibition {
    #[builder(field)]
    #[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
    #[serde(rename = "constraint", alias = "odrl:constraint", default)]
    constraints: Vec<Constraint>,
    #[serde(alias = "odrl:action")]
    action: Action,
}

impl Prohibition {
    pub fn action(&self) -> &Action {
        &self.action
    }

    pub fn constraints(&self) -> &[Constraint] {
        &self.constraints
    }
}

impl<S: prohibition_builder::State> ProhibitionBuilder<S> {
    pub fn constraints(mut self, constraints: Vec<Constraint>) -> Self {
        self.constraints = constraints;
        self
    }

    pub fn constraint(mut self, constraint: Constraint) -> Self {
        self.constraints.push(constraint);
        self
    }
}

#[derive(Debug, Serialize, PartialEq, Clone, Deserialize)]
#[serde(untagged)]
pub enum Action {
    Simple(String),
    Id {
        #[serde(rename = "@id")]
        id: String,
    },
}

#[derive(Debug, Serialize, PartialEq, Clone, Deserialize)]
#[serde(untagged)]
pub enum Target {
    Simple(String),
    Id {
        #[serde(rename = "@id")]
        id: String,
    },
}

impl Target {
    pub fn simple(target: &str) -> Target {
        Target::Simple(target.to_string())
    }

    pub fn id(target: &str) -> Target {
        Target::Id {
            id: target.to_string(),
        }
    }

    pub fn get_id(&self) -> &str {
        match self {
            Target::Simple(target) => target,
            Target::Id { id } => id,
        }
    }
}

impl Default for Action {
    fn default() -> Self {
        Action::new("http://www.w3.org/ns/odrl/2/use".to_string())
    }
}

impl Action {
    pub fn id(&self) -> &String {
        match self {
            Action::Simple(id) => id,
            Action::Id { id } => id,
        }
    }
}

impl Action {
    pub fn new(kind: String) -> Self {
        Action::Id { id: kind }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(untagged)]
pub enum Constraint {
    Atomic(AtomicConstraint),
    MultiplicityConstraint(MultiplicityConstraint),
}

impl Constraint {
    pub fn atomic(atomic: AtomicConstraint) -> Self {
        Constraint::Atomic(atomic)
    }

    pub fn or(constraints: Vec<Constraint>) -> Self {
        Constraint::MultiplicityConstraint(MultiplicityConstraint::Or(constraints))
    }

    pub fn and(constraints: Vec<Constraint>) -> Self {
        Constraint::MultiplicityConstraint(MultiplicityConstraint::And(constraints))
    }

    pub fn xone(constraints: Vec<Constraint>) -> Self {
        Constraint::MultiplicityConstraint(MultiplicityConstraint::Xone(constraints))
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(untagged)]
pub enum LeftOperand {
    Simple(String),
    Id {
        #[serde(rename = "@id")]
        id: String,
    },
}

impl LeftOperand {
    pub fn simple(op: &str) -> LeftOperand {
        LeftOperand::Simple(op.to_string())
    }

    pub fn id(op: &str) -> LeftOperand {
        LeftOperand::Id { id: op.to_string() }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct AtomicConstraint {
    #[serde(rename = "leftOperand", alias = "odrl:leftOperand")]
    left_operand: LeftOperand,
    #[serde(alias = "odrl:operator")]
    operator: Operator,
    #[serde(rename = "rightOperand", alias = "odrl:rightOperand")]
    right_operand: PropertyValue,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum MultiplicityConstraint {
    Or(Vec<Constraint>),
    And(Vec<Constraint>),
    Xone(Vec<Constraint>),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(untagged)]
pub enum Operator {
    Simple(String),
    Id {
        #[serde(rename = "@id")]
        id: String,
    },
}

impl Operator {
    pub fn simple(op: &str) -> Operator {
        Operator::Simple(op.to_string())
    }

    pub fn id(op: &str) -> Operator {
        Operator::Id { id: op.to_string() }
    }
}

impl AtomicConstraint {
    pub fn new<T: ToValue>(left_operand: &str, operator: &str, right_operand: T) -> Self {
        AtomicConstraint::new_with_operator(
            LeftOperand::Simple(left_operand.to_string()),
            Operator::Simple(operator.to_string()),
            right_operand,
        )
    }

    pub fn new_with_operator<T: ToValue>(
        left_operand: impl Into<LeftOperand>,
        operator: Operator,
        right_operand: T,
    ) -> Self {
        Self {
            left_operand: left_operand.into(),
            operator,
            right_operand: PropertyValue(right_operand.into_value()),
        }
    }
}

impl From<&str> for LeftOperand {
    fn from(value: &str) -> Self {
        LeftOperand::Id {
            id: value.to_string(),
        }
    }
}