Skip to main content

wasm4pm_types/
ocel.rs

1use chrono::{DateTime, FixedOffset};
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4use crate::event_log::AttributeValue;
5
6#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
7pub struct OCEL {
8    #[serde(rename = "eventTypes")]
9    pub event_types: Vec<OCELType>,
10    #[serde(rename = "objectTypes")]
11    pub object_types: Vec<OCELType>,
12    #[serde(default)]
13    pub events: Vec<OCELEvent>,
14    #[serde(default)]
15    pub objects: Vec<OCELObject>,
16}
17
18#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
19pub struct OCELType {
20    pub name: String,
21    #[serde(default)]
22    pub attributes: Vec<OCELTypeAttribute>,
23}
24
25#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
26pub struct OCELTypeAttribute {
27    pub name: String,
28    #[serde(rename = "type")]
29    pub value_type: String,
30}
31
32#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
33pub struct OCELEventAttribute {
34    pub name: String,
35    pub value: OCELAttributeValue,
36}
37
38#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
39pub struct OCELEvent {
40    pub id: String,
41    #[serde(rename = "type")]
42    pub event_type: String,
43    pub time: DateTime<FixedOffset>,
44    #[serde(default)]
45    pub attributes: Vec<OCELEventAttribute>,
46    #[serde(default)]
47    pub relationships: Vec<OCELRelationship>,
48}
49
50#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
51pub struct OCELRelationship {
52    #[serde(rename = "objectId")]
53    pub object_id: String,
54    pub qualifier: String,
55}
56
57#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
58pub struct OCELObject {
59    pub id: String,
60    #[serde(rename = "type")]
61    pub object_type: String,
62    #[serde(default)]
63    pub attributes: Vec<OCELObjectAttribute>,
64    #[serde(default)]
65    pub relationships: Vec<OCELRelationship>,
66}
67
68#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
69pub struct OCELObjectAttribute {
70    pub name: String,
71    pub value: OCELAttributeValue,
72    pub time: DateTime<FixedOffset>,
73}
74
75#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
76#[serde(untagged)]
77pub enum OCELAttributeValue {
78    Integer(i64),
79    Float(f64),
80    Boolean(bool),
81    Time(DateTime<FixedOffset>),
82    String(String),
83    #[default]
84    Null,
85}
86
87impl Display for OCELAttributeValue {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        let s = match self {
90            OCELAttributeValue::Time(dt) => dt.to_rfc3339(),
91            OCELAttributeValue::Integer(i) => i.to_string(),
92            OCELAttributeValue::Float(f) => f.to_string(),
93            OCELAttributeValue::Boolean(b) => b.to_string(),
94            OCELAttributeValue::String(s) => s.clone(),
95            OCELAttributeValue::Null => String::default(),
96        };
97        write!(f, "{s}")
98    }
99}
100
101impl From<AttributeValue> for OCELAttributeValue {
102    fn from(value: AttributeValue) -> Self {
103        match value {
104            AttributeValue::String(s) => Self::String(s),
105            AttributeValue::Date(date_time) => Self::Time(date_time),
106            AttributeValue::Int(i) => Self::Integer(i),
107            AttributeValue::Float(f) => Self::Float(f),
108            AttributeValue::Boolean(b) => Self::Boolean(b),
109            AttributeValue::ID(uuid) => Self::String(uuid.to_string()),
110            AttributeValue::List(attributes) => Self::String(format!("{:?}", attributes)),
111            AttributeValue::Container(attributes) => Self::String(format!("{:?}", attributes)),
112            AttributeValue::None() => Self::Null,
113        }
114    }
115}
116
117impl From<OCELAttributeValue> for AttributeValue {
118    fn from(value: OCELAttributeValue) -> AttributeValue {
119        match value {
120            OCELAttributeValue::String(s) => AttributeValue::String(s),
121            OCELAttributeValue::Integer(i) => AttributeValue::Int(i),
122            OCELAttributeValue::Float(f) => AttributeValue::Float(f),
123            OCELAttributeValue::Boolean(b) => AttributeValue::Boolean(b),
124            OCELAttributeValue::Time(date_time) => AttributeValue::Date(date_time),
125            OCELAttributeValue::Null => AttributeValue::None(),
126        }
127    }
128}