tinykv/lib.rs
1//! # TinyKV
2//!
3//! A minimal JSON-based persistent key-value store for Rust projects.
4//! Supports optional TTL (expiration), auto-saving, backup, and serialization via `serde` or `nanoserde`.
5//! Works in `no_std` environments with `alloc`.
6//!
7//! ## Features
8//! - File-based storage using pretty-formatted JSON
9//! - Optional TTL expiration support per key
10//! - Auto-saving changes on modification
11//! - Backup support with `.bak` files
12//! - Simple interface with `serde` (default) or `nanoserde` (feature flag)
13//! - `no_std` support with `alloc`
14//!
15//! ## Feature Flags
16//! - `default`: Uses `serde` for serialization (maximum compatibility) and `std`
17//! - `nanoserde`: Uses `nanoserde` for minimal binary size and faster compilation
18//! - `std`: Enables `std` library (enabled by default)
19//!
20//! ## Example
21//!
22//! ```rust
23//! use tinykv::TinyKV;
24//!
25//! # #[cfg(feature = "std")]
26//! fn main() -> Result<(), Box<dyn std::error::Error>> {
27//! let mut kv = TinyKV::open("mydata.json")?.with_auto_save();
28//! kv.set("username", "hasan".to_string())?;
29//! kv.set_with_ttl("session_token", "abc123".to_string(), 60)?; // 60 seconds TTL
30//! let user: Option<String> = kv.get("username")?;
31//! println!("User: {:?}", user);
32//! Ok(())
33//! }
34//!
35//! # #[cfg(not(feature = "std"))]
36//! # fn main() -> Result<(), tinykv::TinyKVError> {
37//! # let mut kv = TinyKV::new();
38//! # kv.set("username", "hasan".to_string())?;
39//! # let user: Option<String> = kv.get("username")?;
40//! # println!("User: {:?}", user);
41//! # Ok(())
42//! # }
43//! ```
44
45#![cfg_attr(not(feature = "std"), no_std)]
46
47#[cfg(not(feature = "std"))]
48extern crate alloc;
49
50// Module declarations
51mod entry;
52mod error;
53mod store;
54
55// WASM bindings module
56#[cfg(feature = "wasm")]
57mod wasm;
58
59#[cfg(test)]
60mod tests;
61
62// Public exports - only the essential ones from original
63pub use error::TinyKVError;
64pub use store::TinyKV;
65
66// Re-export WASM types for convenience
67#[cfg(feature = "wasm")]
68pub use wasm::WebStorageBackend;
69
70// Re-export important traits for users - exactly as in original
71#[cfg(feature = "nanoserde")]
72pub use nanoserde::{DeJson, SerJson};
73
74#[cfg(all(not(feature = "nanoserde"), feature = "std"))]
75pub use serde::{Deserialize, Serialize};