Skip to main content

drasi_core/models/
mod.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16use std::sync::Arc;
17
18use serde::{Deserialize, Serialize};
19use serde_json::{Map, Value};
20use thiserror::Error;
21
22mod element;
23mod element_value;
24mod source_change;
25mod timestamp_range;
26
27pub use element::{Element, ElementMetadata, ElementReference, ElementTimestamp};
28pub use element_value::ElementPropertyMap;
29pub use element_value::ElementValue;
30pub use source_change::SourceChange;
31pub use timestamp_range::{TimestampBound, TimestampRange};
32
33#[derive(Debug, Error)]
34pub struct ConversionError {}
35
36impl Display for ConversionError {
37    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38        write!(f, "Cannot convert")
39    }
40}
41
42#[derive(Debug, Clone)]
43pub struct QuerySourceElement {
44    pub source_label: String,
45}
46
47#[derive(Debug, Clone)]
48pub struct QuerySubscription {
49    pub id: Arc<str>,
50    pub nodes: Vec<QuerySourceElement>,
51    pub relations: Vec<QuerySourceElement>,
52    pub pipeline: Vec<Arc<str>>,
53}
54
55#[derive(Debug, Hash, PartialEq, Eq, Clone)]
56pub struct QueryJoinKey {
57    pub label: String,
58    pub property: String,
59}
60
61#[derive(Debug, Hash, PartialEq, Eq, Clone)]
62pub struct QueryJoin {
63    pub id: String,
64    pub keys: Vec<QueryJoinKey>,
65}
66
67#[derive(Debug, Clone)]
68pub struct QueryConfig {
69    pub mode: String,
70    pub query: String,
71    pub sources: QuerySources,
72    pub storage_profile: Option<String>,
73}
74
75#[derive(Debug, Clone)]
76pub struct QuerySources {
77    pub subscriptions: Vec<QuerySubscription>,
78    pub joins: Vec<QueryJoin>,
79    pub middleware: Vec<SourceMiddlewareConfig>,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct SourceMiddlewareConfig {
84    #[serde(
85        serialize_with = "serialize_arc_str",
86        deserialize_with = "deserialize_arc_str"
87    )]
88    pub kind: Arc<str>,
89    #[serde(
90        serialize_with = "serialize_arc_str",
91        deserialize_with = "deserialize_arc_str"
92    )]
93    pub name: Arc<str>,
94    pub config: Map<String, Value>,
95}
96
97fn serialize_arc_str<S>(arc: &Arc<str>, serializer: S) -> Result<S::Ok, S::Error>
98where
99    S: serde::Serializer,
100{
101    serializer.serialize_str(arc)
102}
103
104fn deserialize_arc_str<'de, D>(deserializer: D) -> Result<Arc<str>, D::Error>
105where
106    D: serde::Deserializer<'de>,
107{
108    let s = String::deserialize(deserializer)?;
109    Ok(Arc::from(s.as_str()))
110}
111
112impl SourceMiddlewareConfig {
113    pub fn new(kind: &str, name: &str, config: Map<String, Value>) -> Self {
114        SourceMiddlewareConfig {
115            kind: Arc::from(kind),
116            name: Arc::from(name),
117            config,
118        }
119    }
120}