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