#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]
#![allow(clippy::module_name_repetitions)]
use thiserror::Error;
#[cfg(feature = "database")]
pub mod database;
#[cfg(feature = "rusqlite")]
pub mod rusqlite;
#[cfg(feature = "serde_json")]
pub mod serde_json;
#[cfg(feature = "tantivy")]
pub mod tantivy;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ParseError {
#[error("Failed to parse property: {0:?}")]
Parse(String),
#[error("Failed to convert to type: {0:?}")]
ConvertType(String),
#[error("Missing required value: {0:?}")]
MissingValue(String),
}
pub trait ToValueType<T> {
fn to_value_type(self) -> Result<T, ParseError>;
fn missing_value(&self, error: ParseError) -> Result<T, ParseError> {
Err(error)
}
}
pub trait MissingValue<Type> {
fn missing_value(&self, error: ParseError) -> Result<Type, ParseError> {
Err(error)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test_log::test]
fn test_parse_error_display() {
let err = ParseError::Parse("test property".to_string());
assert_eq!(
err.to_string(),
"Failed to parse property: \"test property\""
);
let err = ParseError::ConvertType("u64".to_string());
assert_eq!(err.to_string(), "Failed to convert to type: \"u64\"");
let err = ParseError::MissingValue("field_name".to_string());
assert_eq!(err.to_string(), "Missing required value: \"field_name\"");
}
#[test_log::test]
fn test_parse_error_eq() {
assert_eq!(
ParseError::Parse("test".to_string()),
ParseError::Parse("test".to_string())
);
assert_ne!(
ParseError::Parse("test".to_string()),
ParseError::Parse("other".to_string())
);
assert_ne!(
ParseError::Parse("test".to_string()),
ParseError::ConvertType("test".to_string())
);
}
struct TestValue(i32);
impl ToValueType<i32> for TestValue {
fn to_value_type(self) -> Result<i32, ParseError> {
Ok(self.0)
}
}
struct TestRow;
impl MissingValue<i32> for TestRow {
}
#[test_log::test]
fn test_to_value_type_default_missing_value_returns_error() {
let value = TestValue(42);
let error = ParseError::MissingValue("test_field".to_string());
let result = value.missing_value(error);
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
ParseError::MissingValue("test_field".to_string())
);
}
#[test_log::test]
fn test_missing_value_trait_default_implementation() {
let row = TestRow;
let error = ParseError::MissingValue("column_name".to_string());
let result: Result<i32, ParseError> = row.missing_value(error);
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
ParseError::MissingValue("column_name".to_string())
);
}
}