Skip to main content

nestforge_data/
lib.rs

1use std::{future::Future, pin::Pin};
2
3use thiserror::Error;
4
5/** Type alias for async data operations that return a Future */
6pub type DataFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
7
8/**
9 * DataError
10 *
11 * Error types that can occur during data layer operations.
12 */
13#[derive(Debug, Error)]
14pub enum DataError {
15    #[error("Connection failed: {0}")]
16    Connection(String),
17    #[error("Query failed: {0}")]
18    Query(String),
19    #[error("Serialization failed: {0}")]
20    Serialization(String),
21    #[error("Not found")]
22    NotFound,
23}
24
25/**
26 * DocumentRepo Trait
27 *
28 * A trait for implementing document repositories in NestForge.
29 * Provides standard CRUD operations for entities.
30 *
31 * # Type Parameters
32 * - `T`: The document/entity type
33 * - `Id`: The ID type for the document
34 *
35 * # Methods
36 * - `find_all`: Retrieves all documents
37 * - `find_by_id`: Retrieves a single document by ID
38 * - `insert`: Creates a new document
39 * - `update`: Updates an existing document
40 * - `delete`: Removes a document
41 */
42pub trait DocumentRepo<T>: Send + Sync {
43    /** The ID type for the document */
44    type Id: Send + Sync + Clone + 'static;
45
46    /**
47     * Retrieves all documents of type T.
48     */
49    fn find_all(&self) -> DataFuture<'_, Result<Vec<T>, DataError>>;
50
51    /**
52     * Retrieves a single document by its ID.
53     */
54    fn find_by_id(&self, id: Self::Id) -> DataFuture<'_, Result<Option<T>, DataError>>;
55
56    /**
57     * Inserts a new document.
58     */
59    fn insert(&self, doc: T) -> DataFuture<'_, Result<T, DataError>>;
60
61    /**
62     * Updates an existing document by ID.
63     */
64    fn update(&self, id: Self::Id, doc: T) -> DataFuture<'_, Result<T, DataError>>;
65
66    /**
67     * Deletes a document by ID.
68     */
69    fn delete(&self, id: Self::Id) -> DataFuture<'_, Result<(), DataError>>;
70}
71
72/**
73 * CacheStore Trait
74 *
75 * A trait for implementing cache storage backends.
76 * Provides key-value caching operations with optional TTL.
77 *
78 * # Methods
79 * - `get`: Retrieves a value by key
80 * - `set`: Stores a value with optional expiration
81 * - `delete`: Removes a value by key
82 */
83pub trait CacheStore: Send + Sync {
84    /**
85     * Retrieves a cached value by key.
86     *
87     * Returns None if the key doesn't exist or has expired.
88     */
89    fn get(&self, key: &str) -> DataFuture<'_, Result<Option<String>, DataError>>;
90
91    /**
92     * Stores a value in the cache.
93     *
94     * # Arguments
95     * - `key`: The cache key
96     * - `value`: The value to store
97     * - `ttl_seconds`: Optional expiration time in seconds
98     */
99    fn set(
100        &self,
101        key: &str,
102        value: &str,
103        ttl_seconds: Option<u64>,
104    ) -> DataFuture<'_, Result<(), DataError>>;
105
106    /**
107     * Removes a value from the cache.
108     */
109    fn delete(&self, key: &str) -> DataFuture<'_, Result<(), DataError>>;
110}