matrix_sdk_search/
error.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
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 tantivy::{
16    directory::error::OpenDirectoryError as TantivyOpenDirectoryError,
17    query::QueryParserError as TantivyQueryParserError,
18};
19use thiserror::Error;
20
21/// Internal representation of Search Index errors.
22#[derive(Error, Debug)]
23pub enum IndexError {
24    /// Tantivy Error
25    #[error(transparent)]
26    TantivyError(tantivy::TantivyError),
27
28    /// Open Directory Error
29    #[error(transparent)]
30    OpenDirectoryError(TantivyOpenDirectoryError),
31
32    /// Query Parse Error
33    #[error(transparent)]
34    QueryParserError(TantivyQueryParserError),
35
36    /// Schema Error
37    #[error(transparent)]
38    IndexSchemaError(IndexSchemaError),
39
40    /// Write Error
41    #[error(transparent)]
42    IndexWriteError(IndexWriteError),
43
44    /// Search Error
45    #[error(transparent)]
46    IndexSearchError(IndexSearchError),
47
48    /// Add Event Error
49    #[error("Failed to add event to index")]
50    EventNotAdded,
51
52    /// Message Type Error
53    #[error("Message type not supported")]
54    MessageTypeNotSupported,
55
56    /// Indexing Redacted Message Error
57    #[error("Cannot index redacted message")]
58    CannotIndexRedactedMessage,
59
60    /// Indexing Empty Message Error
61    #[error("Cannot index empty message")]
62    EmptyMessage,
63
64    /// IO error
65    #[error(transparent)]
66    IO(std::io::Error),
67}
68
69impl From<tantivy::TantivyError> for IndexError {
70    fn from(err: tantivy::TantivyError) -> IndexError {
71        IndexError::TantivyError(err)
72    }
73}
74
75impl From<TantivyOpenDirectoryError> for IndexError {
76    fn from(err: TantivyOpenDirectoryError) -> IndexError {
77        IndexError::OpenDirectoryError(err)
78    }
79}
80
81impl From<TantivyQueryParserError> for IndexError {
82    fn from(err: TantivyQueryParserError) -> IndexError {
83        IndexError::QueryParserError(err)
84    }
85}
86
87impl From<IndexSchemaError> for IndexError {
88    fn from(err: IndexSchemaError) -> IndexError {
89        IndexError::IndexSchemaError(err)
90    }
91}
92
93impl From<IndexWriteError> for IndexError {
94    fn from(err: IndexWriteError) -> IndexError {
95        IndexError::IndexWriteError(err)
96    }
97}
98
99impl From<IndexSearchError> for IndexError {
100    fn from(err: IndexSearchError) -> IndexError {
101        IndexError::IndexSearchError(err)
102    }
103}
104
105impl From<std::io::Error> for IndexError {
106    fn from(err: std::io::Error) -> IndexError {
107        IndexError::IO(err)
108    }
109}
110
111/// Internal representation of Schema errors.
112#[derive(Error, Debug)]
113pub enum IndexSchemaError {
114    /// Tantivy Error
115    #[error(transparent)]
116    TantivyError(tantivy::TantivyError),
117}
118
119impl From<tantivy::TantivyError> for IndexSchemaError {
120    fn from(err: tantivy::TantivyError) -> IndexSchemaError {
121        IndexSchemaError::TantivyError(err)
122    }
123}
124
125/// Internal representation of Writer errors.
126#[derive(Error, Debug)]
127pub enum IndexWriteError {
128    /// Tantivy Error
129    #[error(transparent)]
130    TantivyError(tantivy::TantivyError),
131}
132
133impl From<tantivy::TantivyError> for IndexWriteError {
134    fn from(err: tantivy::TantivyError) -> IndexWriteError {
135        IndexWriteError::TantivyError(err)
136    }
137}
138
139/// Internal representation of Search errors.
140#[derive(Error, Debug)]
141pub enum IndexSearchError {
142    /// Tantivy Error
143    #[error(transparent)]
144    TantivyError(tantivy::TantivyError),
145}
146
147impl From<tantivy::TantivyError> for IndexSearchError {
148    fn from(err: tantivy::TantivyError) -> IndexSearchError {
149        IndexSearchError::TantivyError(err)
150    }
151}