use serde::{Deserialize, Serialize};
use std::time::Duration;
#[cfg(not(feature = "mongodb"))]
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NoSQLType {
Document,
KeyValue,
Column,
Graph,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NoSQLBackendType {
#[cfg(feature = "mongodb")]
MongoDB,
#[cfg(feature = "redis")]
Redis,
#[cfg(feature = "cassandra")]
Cassandra,
#[cfg(feature = "dynamodb")]
DynamoDB,
#[cfg(feature = "neo4j")]
Neo4j,
#[doc(hidden)]
#[cfg(not(any(
feature = "mongodb",
feature = "redis",
feature = "cassandra",
feature = "dynamodb",
feature = "neo4j"
)))]
__UnusedPlaceholder,
}
impl NoSQLBackendType {
pub fn nosql_type(&self) -> NoSQLType {
match self {
#[cfg(feature = "mongodb")]
NoSQLBackendType::MongoDB => NoSQLType::Document,
#[cfg(feature = "redis")]
NoSQLBackendType::Redis => NoSQLType::KeyValue,
#[cfg(feature = "cassandra")]
NoSQLBackendType::Cassandra => NoSQLType::Column,
#[cfg(feature = "dynamodb")]
NoSQLBackendType::DynamoDB => NoSQLType::KeyValue,
#[cfg(feature = "neo4j")]
NoSQLBackendType::Neo4j => NoSQLType::Graph,
#[cfg(not(any(
feature = "mongodb",
feature = "redis",
feature = "cassandra",
feature = "dynamodb",
feature = "neo4j"
)))]
NoSQLBackendType::__UnusedPlaceholder => {
unreachable!("__UnusedPlaceholder should never be constructed")
}
}
}
}
#[cfg(feature = "mongodb")]
pub use bson::Document;
#[cfg(not(feature = "mongodb"))]
pub type Document = HashMap<String, serde_json::Value>;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UpdateResult {
pub matched_count: u64,
pub modified_count: u64,
pub upserted_count: u64,
pub upserted_id: Option<String>,
}
impl UpdateResult {
pub fn new(
matched_count: u64,
modified_count: u64,
upserted_count: u64,
upserted_id: Option<String>,
) -> Self {
Self {
matched_count,
modified_count,
upserted_count,
upserted_id,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub struct FindOptions {
pub limit: Option<i64>,
pub skip: Option<u64>,
pub sort: Option<Document>,
pub projection: Option<Document>,
pub batch_size: Option<u32>,
}
impl FindOptions {
pub fn new() -> Self {
Self::default()
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn skip(mut self, skip: u64) -> Self {
self.skip = Some(skip);
self
}
pub fn sort(mut self, sort: Document) -> Self {
self.sort = Some(sort);
self
}
pub fn projection(mut self, projection: Document) -> Self {
self.projection = Some(projection);
self
}
pub fn batch_size(mut self, batch_size: u32) -> Self {
self.batch_size = Some(batch_size);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
Bytes(Vec<u8>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ttl(pub Duration);
impl From<Duration> for Ttl {
fn from(duration: Duration) -> Self {
Ttl(duration)
}
}
impl From<Ttl> for Duration {
fn from(ttl: Ttl) -> Self {
ttl.0
}
}
impl Ttl {
pub fn from_secs(secs: u64) -> Self {
Ttl(Duration::from_secs(secs))
}
pub fn from_millis(millis: u64) -> Self {
Ttl(Duration::from_millis(millis))
}
pub fn as_secs(&self) -> u64 {
self.0.as_secs()
}
pub fn as_millis(&self) -> u128 {
self.0.as_millis()
}
}