reqwest_drive/
lib.rs

1#[cfg(doctest)]
2doc_comment::doctest!("../README.md");
3
4use std::path::Path;
5
6mod cache_middleware;
7pub use cache_middleware::{CachePolicy, DriveCache};
8
9mod throttle_middleware;
10pub use throttle_middleware::{DriveThrottleBackoff, ThrottlePolicy};
11
12use simd_r_drive::DataStore;
13use std::sync::Arc;
14
15// TODO: Add usage examples here
16
17/// Initializes only the cache middleware with a file-based data store.
18///
19/// This function creates a new `DriveCache` instance backed by a `DataStore` file.
20///
21/// # Arguments
22///
23/// * `cache_storage_file` - Path to the file where cached responses are stored.
24/// * `policy` - The cache expiration policy.
25///
26/// # Returns
27///
28/// An `Arc<DriveCache>` instance managing the cache.
29pub fn init_cache(cache_storage_file: &Path, policy: CachePolicy) -> Arc<DriveCache> {
30    Arc::new(DriveCache::new(cache_storage_file, policy))
31}
32
33/// Initializes both cache and throttle middleware with a file-based data store.
34///
35/// This function creates:
36/// - A `DriveCache` instance for response caching.
37/// - A `DriveThrottleBackoff` instance for rate-limiting and retrying failed requests.
38///
39/// # Arguments
40///
41/// * `cache_storage_file` - Path to the file where cached responses are stored.
42/// * `cache_policy` - The cache expiration policy.
43/// * `throttle_policy` - The throttling and backoff policy.
44///
45/// # Returns
46///
47/// A tuple containing:
48/// - `Arc<DriveCache>` for caching.
49/// - `Arc<DriveThrottleBackoff>` for throttling.
50pub fn init_cache_with_throttle(
51    cache_storage_file: &Path,
52    cache_policy: CachePolicy,
53    throttle_policy: ThrottlePolicy,
54) -> (Arc<DriveCache>, Arc<DriveThrottleBackoff>) {
55    let cache = Arc::new(DriveCache::new(cache_storage_file, cache_policy));
56    let throttle = Arc::new(DriveThrottleBackoff::new(
57        throttle_policy,
58        Arc::clone(&cache),
59    ));
60    (cache, throttle)
61}
62
63/// Initializes only the cache middleware using an **existing** `Arc<DataStore>`.
64///
65/// This function is useful if a shared `DataStore` instance already exists
66/// and should be reused instead of creating a new one.
67///
68/// # Arguments
69///
70/// * `store` - A shared `Arc<DataStore>` instance.
71/// * `policy` - The cache expiration policy.
72///
73/// # Returns
74///
75/// An `Arc<DriveCache>` instance managing the cache.
76pub fn init_cache_with_drive(store: Arc<DataStore>, policy: CachePolicy) -> Arc<DriveCache> {
77    Arc::new(DriveCache::with_drive_arc(store, policy))
78}
79
80/// Initializes both cache and throttle middleware using an **existing** `Arc<DataStore>`.
81///
82/// This function is useful if a shared `DataStore` instance already exists
83/// and should be reused instead of creating a new one.
84///
85/// # Arguments
86///
87/// * `store` - A shared `Arc<DataStore>` instance.
88/// * `cache_policy` - The cache expiration policy.
89/// * `throttle_policy` - The throttling and backoff policy.
90///
91/// # Returns
92///
93/// A tuple containing:
94/// - `Arc<DriveCache>` for caching.
95/// - `Arc<DriveThrottleBackoff>` for throttling.
96pub fn init_cache_with_drive_and_throttle(
97    store: Arc<DataStore>,
98    cache_policy: CachePolicy,
99    throttle_policy: ThrottlePolicy,
100) -> (Arc<DriveCache>, Arc<DriveThrottleBackoff>) {
101    let cache = Arc::new(DriveCache::with_drive_arc(store, cache_policy));
102    let throttle = Arc::new(DriveThrottleBackoff::new(
103        throttle_policy,
104        Arc::clone(&cache),
105    ));
106    (cache, throttle)
107}