use bincode::ErrorKind as BincodeError;
use sled::Error as SledError;
use std::error::Error as StandardError;
use std::io::Error as IOError;
#[cfg(feature = "py")]
use super::*;
#[cfg(feature = "py")]
use pyo3::exceptions::PyValueError;
#[derive(Debug)]
pub struct Error(String);
impl Error {
pub fn new(message: &str) -> Self {
message.into()
}
pub fn message(&self) -> &str {
&self.0
}
pub fn collection_not_found() -> Self {
let message = "The collection is not found.";
message.into()
}
pub fn collection_limit() -> Self {
let max = u32::MAX;
let brief = "The collection limit is reached.";
let detail = format!("The max number of records is {max}.");
let message = format!("{brief} {detail}");
message.into()
}
pub fn record_not_found() -> Self {
let message = "The vector record is not found.";
message.into()
}
pub fn invalid_dimension(found: usize, expected: usize) -> Self {
let brief = "Invalid vector dimension.";
let detail = format!("Expected {expected}, found {found}.");
let message = format!("{brief} {detail}");
message.into()
}
}
impl From<String> for Error {
fn from(err: String) -> Self {
Error(err)
}
}
impl From<&str> for Error {
fn from(err: &str) -> Self {
Error(err.to_string())
}
}
impl From<Box<dyn StandardError>> for Error {
fn from(err: Box<dyn StandardError>) -> Self {
Error(err.to_string())
}
}
impl From<SledError> for Error {
fn from(err: SledError) -> Self {
Error(err.to_string())
}
}
impl From<IOError> for Error {
fn from(err: IOError) -> Self {
Error(err.to_string())
}
}
impl From<Box<BincodeError>> for Error {
fn from(err: Box<BincodeError>) -> Self {
Error(err.to_string())
}
}
#[cfg(feature = "py")]
impl From<Error> for PyErr {
fn from(err: Error) -> Self {
PyErr::new::<PyValueError, String>(err.0)
}
}