vrc_get_litedb/
lib.rs

1//! # LiteDB in Rust
2//! This is a reimplementation of [LiteDB] in Rust.
3//!
4//! This implementation (currently?) only supports single-threaded operation.
5//!
6//! [LiteDB]: <https://www.litedb.org/>
7
8#![allow(clippy::too_many_arguments)]
9
10use crate::bson::Value;
11use crate::engine::{BasePage, PageType};
12use std::fmt::Display;
13
14#[macro_use]
15pub mod bson;
16pub mod engine;
17pub mod expression;
18mod utils;
19
20#[cfg(all(feature = "shared-mutex", windows))]
21pub mod shared_mutex;
22#[cfg(feature = "tokio-fs")]
23pub mod tokio_fs;
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27#[derive(Debug)]
28pub struct Error {
29    message: String,
30}
31
32impl Error {
33    pub(crate) fn invalid_database() -> Error {
34        Error::err("Invalid database file")
35    }
36
37    pub(crate) fn invalid_page() -> Error {
38        Error::err("Invalid database file")
39    }
40
41    pub(crate) fn datetime_overflow() -> Self {
42        Self::err("DateTime overflow")
43    }
44
45    pub(crate) fn encrypted_no_password() -> Self {
46        Self::err("Encrypted database without password")
47    }
48
49    pub(crate) fn collation_not_match() -> Error {
50        Error::err("Collation not match")
51    }
52
53    pub(crate) fn invalid_page_type(expected: PageType, page: BasePage) -> Error {
54        Error::err(format!(
55            "Invalid page type: expected {:?}, got {:?}",
56            expected,
57            page.page_type()
58        ))
59    }
60
61    pub(crate) fn collection_index_limit_reached() -> Error {
62        Error::err("Collection index limit reached")
63    }
64
65    pub(crate) fn name_length_header_space(name: &str) -> Error {
66        Error::err(format!(
67            "Name length exceeds available header space: {}",
68            name
69        ))
70    }
71
72    pub(crate) fn invalid_collection_name(name: &str) -> Error {
73        Error::err(format!("Invalid collection name: {}", name))
74    }
75
76    pub(crate) fn invalid_bson() -> Error {
77        Error::err("Invalid BSON")
78    }
79
80    pub(crate) fn size_limit_reached() -> Self {
81        Self::err("Size limit reached")
82    }
83
84    pub(crate) fn transaction_limit() -> Error {
85        Self::err("Maximum number of transactions reached")
86    }
87
88    pub(crate) fn invalid_index_key(message: &str) -> Error {
89        Error::err(format!("Invalid index key: {}", message))
90    }
91
92    pub(crate) fn index_duplicate_key(index: &str, key: Value) -> Error {
93        Error::err(format!("Duplicate key in index {index}: {key:?}"))
94    }
95
96    pub(crate) fn already_exists_collection_name(name: &str) -> Error {
97        Error::err(format!("Already exists collection name: {}", name))
98    }
99
100    pub(crate) fn document_size_exceed_limit() -> Self {
101        Error::err("DocumentSize exceed limit")
102    }
103
104    pub(crate) fn index_already_exists(name: &str) -> Error {
105        Error::err(format!("Index already exists: {}", name))
106    }
107
108    pub(crate) fn drop_id_index() -> Error {
109        Error::err("Drop _id index is forbidden")
110    }
111
112    #[cfg(feature = "sequential-index")]
113    pub(crate) fn bad_auto_id(
114        auto_id: engine::BsonAutoId,
115        collection_name: &str,
116        last_id: Value,
117    ) -> Self {
118        Error::err(format!(
119            "It's not possible use AutoId={auto_id:?} because '{collection_name}' collection contains not only numbers in _id index ({last_id:?})."
120        ))
121    }
122
123    pub(crate) fn invalid_data_type(field: &str, value: &Value) -> Error {
124        Error::err(format!(
125            "Invalid data type for field {}: {:?}",
126            field, value
127        ))
128    }
129}
130
131impl Error {
132    pub fn err(message: impl Display) -> Self {
133        Error {
134            message: message.to_string(),
135        }
136    }
137}
138
139impl From<std::io::Error> for Error {
140    fn from(err: std::io::Error) -> Self {
141        Error {
142            message: err.to_string(),
143        }
144    }
145}
146
147impl From<bson::ParseError> for Error {
148    fn from(err: bson::ParseError) -> Self {
149        Error {
150            message: err.to_string(),
151        }
152    }
153}
154
155impl From<expression::ParseError> for Error {
156    fn from(err: expression::ParseError) -> Self {
157        Error {
158            message: err.to_string(),
159        }
160    }
161}
162
163impl From<std::string::FromUtf8Error> for Error {
164    fn from(err: std::string::FromUtf8Error) -> Self {
165        Self::err(err)
166    }
167}
168
169impl Display for Error {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        write!(f, "{}", self.message)
172    }
173}
174
175impl std::error::Error for Error {}