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