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::{
28    validate_effective_from, Element, ElementMetadata, ElementReference, ElementTimestamp,
29    MAX_REASONABLE_MILLIS_TIMESTAMP,
30};
31pub use element_value::ElementPropertyMap;
32pub use element_value::ElementValue;
33pub use source_change::SourceChange;
34pub use timestamp_range::{TimestampBound, TimestampRange};
35
36#[derive(Debug, Error)]
37pub struct ConversionError {}
38
39impl Display for ConversionError {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        write!(f, "Cannot convert")
42    }
43}
44
45#[derive(Debug, Clone)]
46pub struct QuerySourceElement {
47    pub source_label: String,
48}
49
50#[derive(Debug, Clone)]
51pub struct QuerySubscription {
52    pub id: Arc<str>,
53    pub nodes: Vec<QuerySourceElement>,
54    pub relations: Vec<QuerySourceElement>,
55    pub pipeline: Vec<Arc<str>>,
56}
57
58#[derive(Debug, Hash, PartialEq, Eq, Clone)]
59pub struct QueryJoinKey {
60    pub label: String,
61    pub property: String,
62}
63
64#[derive(Debug, Hash, PartialEq, Eq, Clone)]
65pub struct QueryJoin {
66    pub id: String,
67    pub keys: Vec<QueryJoinKey>,
68}
69
70#[derive(Debug, Clone)]
71pub struct QueryConfig {
72    pub mode: String,
73    pub query: String,
74    pub sources: QuerySources,
75    pub storage_profile: Option<String>,
76}
77
78#[derive(Debug, Clone)]
79pub struct QuerySources {
80    pub subscriptions: Vec<QuerySubscription>,
81    pub joins: Vec<QueryJoin>,
82    pub middleware: Vec<SourceMiddlewareConfig>,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct SourceMiddlewareConfig {
87    #[serde(
88        serialize_with = "serialize_arc_str",
89        deserialize_with = "deserialize_arc_str"
90    )]
91    pub kind: Arc<str>,
92    #[serde(
93        serialize_with = "serialize_arc_str",
94        deserialize_with = "deserialize_arc_str"
95    )]
96    pub name: Arc<str>,
97    pub config: Map<String, Value>,
98}
99
100fn serialize_arc_str<S>(arc: &Arc<str>, serializer: S) -> Result<S::Ok, S::Error>
101where
102    S: serde::Serializer,
103{
104    serializer.serialize_str(arc)
105}
106
107fn deserialize_arc_str<'de, D>(deserializer: D) -> Result<Arc<str>, D::Error>
108where
109    D: serde::Deserializer<'de>,
110{
111    let s = String::deserialize(deserializer)?;
112    Ok(Arc::from(s.as_str()))
113}
114
115impl SourceMiddlewareConfig {
116    pub fn new(kind: &str, name: &str, config: Map<String, Value>) -> Self {
117        SourceMiddlewareConfig {
118            kind: Arc::from(kind),
119            name: Arc::from(name),
120            config,
121        }
122    }
123}