Skip to main content

drasi_core/interface/
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
15mod checkpoint_store;
16mod element_index;
17mod future_queue;
18mod index_backend;
19mod live_results_writer;
20mod outbox_writer;
21mod query_clock;
22mod result_index;
23mod session_control;
24mod source_middleware;
25
26use std::error::Error;
27use std::fmt::Display;
28
29pub use checkpoint_store::CheckpointStore;
30pub use checkpoint_store::SourceCheckpoint;
31use drasi_query_ast::api::QueryParseError;
32pub use element_index::ElementArchiveIndex;
33pub use element_index::ElementIndex;
34pub use element_index::ElementResult;
35pub use element_index::ElementStream;
36pub use future_queue::FutureElementRef;
37pub use future_queue::FutureQueue;
38pub use future_queue::FutureQueueConsumer;
39pub use future_queue::PushType;
40pub use index_backend::CreatedIndexes;
41pub use index_backend::IndexBackendPlugin;
42pub use index_backend::IndexSet;
43pub use live_results_writer::LiveResultsWriter;
44pub use live_results_writer::RowMutation;
45pub use outbox_writer::OutboxWriter;
46pub use query_clock::QueryClock;
47pub use result_index::AccumulatorIndex;
48pub use result_index::LazySortedSetStore;
49pub use result_index::ResultIndex;
50pub use result_index::ResultKey;
51pub use result_index::ResultOwner;
52pub use result_index::ResultSequence;
53pub use result_index::ResultSequenceCounter;
54pub use session_control::NoOpSessionControl;
55pub use session_control::SessionControl;
56pub use session_control::SessionGuard;
57pub use source_middleware::MiddlewareError;
58pub use source_middleware::MiddlewareSetupError;
59pub use source_middleware::SourceMiddleware;
60pub use source_middleware::SourceMiddlewareFactory;
61use thiserror::Error;
62
63use crate::evaluation::EvaluationError;
64
65#[derive(Debug)]
66pub enum IndexError {
67    IOError,
68    NotSupported,
69    CorruptedData,
70    ConnectionFailed(Box<dyn std::error::Error + Send + Sync>),
71    UnknownStore(String),
72    Other(Box<dyn std::error::Error + Send + Sync>),
73}
74
75impl PartialEq for IndexError {
76    fn eq(&self, other: &Self) -> bool {
77        match (self, other) {
78            (IndexError::IOError, IndexError::IOError) => true,
79            (IndexError::NotSupported, IndexError::NotSupported) => true,
80            (IndexError::CorruptedData, IndexError::CorruptedData) => true,
81            (IndexError::ConnectionFailed(a), IndexError::ConnectionFailed(b)) => {
82                a.to_string() == b.to_string()
83            }
84            (IndexError::UnknownStore(a), IndexError::UnknownStore(b)) => a == b,
85            (IndexError::Other(a), IndexError::Other(b)) => a.to_string() == b.to_string(),
86            _ => false,
87        }
88    }
89}
90// impl<E: std::error::Error + 'static> From<E> for IndexError {
91//   fn from(e: E) -> Self {
92//     IndexError::Other(Box::new(e))
93//   }
94// }
95
96impl Display for IndexError {
97    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98        format!("{self:?}").fmt(f)
99    }
100}
101
102impl Error for IndexError {
103    fn source(&self) -> Option<&(dyn Error + 'static)> {
104        match self {
105            IndexError::Other(e) => Some(e.as_ref()),
106            _ => None,
107        }
108    }
109}
110
111impl IndexError {
112    pub fn other<E: std::error::Error + Send + Sync + 'static>(e: E) -> Self {
113        IndexError::Other(Box::new(e))
114    }
115
116    pub fn connection_failed<E: std::error::Error + Send + Sync + 'static>(e: E) -> Self {
117        IndexError::ConnectionFailed(Box::new(e))
118    }
119}
120
121#[derive(Error, Debug)]
122pub enum QueryBuilderError {
123    #[error("Middleware setup error: {0}")]
124    MiddlewareSetupError(MiddlewareSetupError),
125
126    #[error("Parser error: {0}")]
127    ParserError(QueryParseError),
128
129    #[error("Evaluation error: {0}")]
130    EvaluationError(EvaluationError),
131}
132
133impl From<MiddlewareSetupError> for QueryBuilderError {
134    fn from(e: MiddlewareSetupError) -> Self {
135        QueryBuilderError::MiddlewareSetupError(e)
136    }
137}
138
139impl From<QueryParseError> for QueryBuilderError {
140    fn from(e: QueryParseError) -> Self {
141        QueryBuilderError::ParserError(e)
142    }
143}
144
145impl From<EvaluationError> for QueryBuilderError {
146    fn from(e: EvaluationError) -> Self {
147        QueryBuilderError::EvaluationError(e)
148    }
149}