summavy/directory/
error.rs1use std::path::PathBuf;
2use std::sync::Arc;
3use std::{fmt, io};
4
5use crate::Version;
6
7#[derive(Debug, Clone, Error)]
11pub enum LockError {
12 #[error("Could not acquire lock as it is already held, possibly by a different process.")]
19 LockBusy,
20 #[error("Failed to acquire the lock due to an io:Error.")]
22 IoError(Arc<io::Error>),
23}
24
25impl LockError {
26 pub fn wrap_io_error(io_error: io::Error) -> Self {
28 Self::IoError(Arc::new(io_error))
29 }
30}
31
32#[derive(Debug, Clone, Error)]
34pub enum OpenDirectoryError {
35 #[error("Directory does not exist: '{0}'.")]
37 DoesNotExist(PathBuf),
38 #[error("Path exists but is not a directory: '{0}'.")]
40 NotADirectory(PathBuf),
41 #[error("Failed to create a temporary directory: '{0}'.")]
43 FailedToCreateTempDir(Arc<io::Error>),
44 #[error("IoError '{io_error:?}' while create directory in: '{directory_path:?}'.")]
46 IoError {
47 io_error: Arc<io::Error>,
49 directory_path: PathBuf,
51 },
52}
53
54impl OpenDirectoryError {
55 pub fn wrap_io_error(io_error: io::Error, directory_path: PathBuf) -> Self {
57 Self::IoError {
58 io_error: Arc::new(io_error),
59 directory_path,
60 }
61 }
62}
63
64#[derive(Debug, Clone, Error)]
66pub enum OpenWriteError {
67 #[error("File already exists: '{0}'")]
70 FileAlreadyExists(PathBuf),
71 #[error("IoError '{io_error:?}' while opening file for write: '{filepath}'.")]
74 IoError {
75 io_error: Arc<io::Error>,
77 filepath: PathBuf,
79 },
80}
81
82impl OpenWriteError {
83 pub fn wrap_io_error(io_error: io::Error, filepath: PathBuf) -> Self {
85 Self::IoError {
86 io_error: Arc::new(io_error),
87 filepath,
88 }
89 }
90}
91#[derive(Clone)]
94pub enum Incompatibility {
95 CompressionMismatch {
97 library_compression_format: String,
99 index_compression_format: String,
101 },
102 IndexMismatch {
104 library_version: Version,
106 index_version: Version,
108 },
109}
110
111impl fmt::Debug for Incompatibility {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
113 match self {
114 Incompatibility::CompressionMismatch {
115 library_compression_format,
116 index_compression_format,
117 } => {
118 let err = format!(
119 "Library was compiled with {:?} compression, index was compressed with {:?}",
120 library_compression_format, index_compression_format
121 );
122 let advice = format!(
123 "Change the feature flag to {:?} and rebuild the library",
124 index_compression_format
125 );
126 write!(f, "{}. {}", err, advice)?;
127 }
128 Incompatibility::IndexMismatch {
129 library_version,
130 index_version,
131 } => {
132 let err = format!(
133 "Library version: {}, index version: {}",
134 library_version.index_format_version, index_version.index_format_version
135 );
136 let advice = format!(
139 "Change tantivy to a version compatible with index format {} (e.g. {}.{}.x) \
140 and rebuild your project.",
141 index_version.index_format_version, index_version.major, index_version.minor
142 );
143 write!(f, "{}. {}", err, advice)?;
144 }
145 }
146
147 Ok(())
148 }
149}
150
151#[derive(Debug, Clone, Error)]
153pub enum OpenReadError {
154 #[error("Files does not exist: {0:?}")]
156 FileDoesNotExist(PathBuf),
157 #[error(
159 "IoError: '{io_error:?}' happened while opening the following file for Read: {filepath}."
160 )]
161 IoError {
162 io_error: Arc<io::Error>,
164 filepath: PathBuf,
166 },
167 #[error("Index version unsupported: {0:?}")]
169 IncompatibleIndex(Incompatibility),
170}
171
172impl OpenReadError {
173 pub fn wrap_io_error(io_error: io::Error, filepath: PathBuf) -> Self {
175 Self::IoError {
176 io_error: Arc::new(io_error),
177 filepath,
178 }
179 }
180}
181#[derive(Debug, Clone, Error)]
183pub enum DeleteError {
184 #[error("File does not exist: '{0}'.")]
186 FileDoesNotExist(PathBuf),
187 #[error("The following IO error happened while deleting file '{filepath}': '{io_error:?}'.")]
190 IoError {
191 io_error: Arc<io::Error>,
193 filepath: PathBuf,
195 },
196}
197
198impl From<Incompatibility> for OpenReadError {
199 fn from(incompatibility: Incompatibility) -> Self {
200 OpenReadError::IncompatibleIndex(incompatibility)
201 }
202}