|
| 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 | + |
| 15 | +//! The event cache is an abstraction layer, sitting between the Rust SDK and a |
| 16 | +//! final client, that acts as a global observer of all the rooms, gathering and |
| 17 | +//! inferring some extra useful information about each room. In particular, this |
| 18 | +//! doesn't require subscribing to a specific room to get access to this |
| 19 | +//! information. |
| 20 | +//! |
| 21 | +//! It's intended to be fast, robust and easy to maintain, having learned from |
| 22 | +//! previous endeavours at implementing middle to high level features elsewhere |
| 23 | +//! in the SDK, notably in the UI's Timeline object. |
| 24 | +//! |
| 25 | +//! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more |
| 26 | +//! details about the historical reasons that led us to start writing this. |
| 27 | +
|
| 28 | +use tantivy::{ |
| 29 | + directory::error::OpenDirectoryError as TantivyOpenDirectoryError, |
| 30 | + query::QueryParserError as TantivyQueryParserError, |
| 31 | +}; |
| 32 | +use thiserror::Error; |
| 33 | + |
| 34 | +/// Internal representation of Search Index errors. |
| 35 | +#[derive(Error, Debug)] |
| 36 | +pub enum IndexError { |
| 37 | + /// Tantivy Error |
| 38 | + #[error(transparent)] |
| 39 | + TantivyError(tantivy::TantivyError), |
| 40 | + |
| 41 | + /// Open Directory Error |
| 42 | + #[error(transparent)] |
| 43 | + OpenDirectoryError(TantivyOpenDirectoryError), |
| 44 | + |
| 45 | + /// Query Parse Error |
| 46 | + #[error(transparent)] |
| 47 | + QueryParserError(TantivyQueryParserError), |
| 48 | + |
| 49 | + /// Schema Error |
| 50 | + #[error(transparent)] |
| 51 | + IndexSchemaError(IndexSchemaError), |
| 52 | + |
| 53 | + /// Write Error |
| 54 | + #[error(transparent)] |
| 55 | + IndexWriteError(IndexWriteError), |
| 56 | + |
| 57 | + /// Search Error |
| 58 | + #[error(transparent)] |
| 59 | + IndexSearchError(IndexSearchError), |
| 60 | + |
| 61 | + /// Add Event Error |
| 62 | + #[error("Failed to add event to index")] |
| 63 | + EventNotAdded, |
| 64 | + |
| 65 | + /// Message Type Error |
| 66 | + #[error("Message type not supported")] |
| 67 | + MessageTypeNotSupported, |
| 68 | + |
| 69 | + /// Indexing Redacted Message Error |
| 70 | + #[error("Cannot index redacted message")] |
| 71 | + CannotIndexRedactedMessage, |
| 72 | + |
| 73 | + /// Indexing Empty Message Error |
| 74 | + #[error("Cannot index empty message")] |
| 75 | + EmptyMessage, |
| 76 | + |
| 77 | + /// IO error |
| 78 | + #[error(transparent)] |
| 79 | + IO(std::io::Error), |
| 80 | +} |
| 81 | + |
| 82 | +impl From<tantivy::TantivyError> for IndexError { |
| 83 | + fn from(err: tantivy::TantivyError) -> IndexError { |
| 84 | + IndexError::TantivyError(err) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl From<TantivyOpenDirectoryError> for IndexError { |
| 89 | + fn from(err: TantivyOpenDirectoryError) -> IndexError { |
| 90 | + IndexError::OpenDirectoryError(err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl From<TantivyQueryParserError> for IndexError { |
| 95 | + fn from(err: TantivyQueryParserError) -> IndexError { |
| 96 | + IndexError::QueryParserError(err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl From<IndexSchemaError> for IndexError { |
| 101 | + fn from(err: IndexSchemaError) -> IndexError { |
| 102 | + IndexError::IndexSchemaError(err) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl From<IndexWriteError> for IndexError { |
| 107 | + fn from(err: IndexWriteError) -> IndexError { |
| 108 | + IndexError::IndexWriteError(err) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl From<IndexSearchError> for IndexError { |
| 113 | + fn from(err: IndexSearchError) -> IndexError { |
| 114 | + IndexError::IndexSearchError(err) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl From<std::io::Error> for IndexError { |
| 119 | + fn from(err: std::io::Error) -> IndexError { |
| 120 | + IndexError::IO(err) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/// Internal representation of Schema errors. |
| 125 | +#[derive(Error, Debug)] |
| 126 | +pub enum IndexSchemaError { |
| 127 | + /// Tantivy Error |
| 128 | + #[error(transparent)] |
| 129 | + TantivyError(tantivy::TantivyError), |
| 130 | +} |
| 131 | + |
| 132 | +impl From<tantivy::TantivyError> for IndexSchemaError { |
| 133 | + fn from(err: tantivy::TantivyError) -> IndexSchemaError { |
| 134 | + IndexSchemaError::TantivyError(err) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Internal representation of Writer errors. |
| 139 | +#[derive(Error, Debug)] |
| 140 | +pub enum IndexWriteError { |
| 141 | + /// Tantivy Error |
| 142 | + #[error(transparent)] |
| 143 | + TantivyError(tantivy::TantivyError), |
| 144 | +} |
| 145 | + |
| 146 | +impl From<tantivy::TantivyError> for IndexWriteError { |
| 147 | + fn from(err: tantivy::TantivyError) -> IndexWriteError { |
| 148 | + IndexWriteError::TantivyError(err) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Internal representation of Search errors. |
| 153 | +#[derive(Error, Debug)] |
| 154 | +pub enum IndexSearchError { |
| 155 | + /// Tantivy Error |
| 156 | + #[error(transparent)] |
| 157 | + TantivyError(tantivy::TantivyError), |
| 158 | +} |
| 159 | + |
| 160 | +impl From<tantivy::TantivyError> for IndexSearchError { |
| 161 | + fn from(err: tantivy::TantivyError) -> IndexSearchError { |
| 162 | + IndexSearchError::TantivyError(err) |
| 163 | + } |
| 164 | +} |
0 commit comments