Skip to main content

commons/
lib.rs

1//! # Commons
2//!
3//! Shared Rust utilities and common patterns for the Sebastien Rousseau ecosystem.
4//!
5//! This crate provides reusable components, traits, and utilities used across
6//! multiple Rust projects in the ecosystem.
7//!
8//! ## Features
9//!
10//! - `config` - Configuration file loading and management (TOML)
11//! - `error` - Common error types and Result aliases
12//! - `logging` - Simple structured logging
13//! - `time` - Date/time utilities and formatting
14//! - `collections` - Extended collection utilities (LRU cache)
15//! - `validation` - Input validation utilities
16//! - `retry` - Retry logic with backoff strategies
17//! - `id` - ID generation (timestamp, random, UUID-like)
18//! - `env` - Environment variable helpers
19//!
20//! ## Quick Start
21//!
22//! ```rust
23//! use commons::prelude::*;
24//!
25//! // Use the LRU cache
26//! let mut cache = LruCache::new(100);
27//! cache.insert("key", "value");
28//! ```
29//!
30//! ## Feature Flags
31//!
32//! Enable only what you need:
33//!
34//! ```toml
35//! [dependencies]
36//! commons = { version = "0.0.1", default-features = false, features = ["error", "time"] }
37//! ```
38
39#![cfg_attr(docsrs, feature(doc_cfg))]
40#![deny(missing_docs)]
41#![deny(unsafe_code)]
42#![warn(clippy::all)]
43
44#[cfg(feature = "config")]
45#[cfg_attr(docsrs, doc(cfg(feature = "config")))]
46pub mod config;
47
48#[cfg(feature = "error")]
49#[cfg_attr(docsrs, doc(cfg(feature = "error")))]
50pub mod error;
51
52#[cfg(feature = "logging")]
53#[cfg_attr(docsrs, doc(cfg(feature = "logging")))]
54pub mod logging;
55
56#[cfg(feature = "time")]
57#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
58pub mod time;
59
60#[cfg(feature = "collections")]
61#[cfg_attr(docsrs, doc(cfg(feature = "collections")))]
62pub mod collections;
63
64#[cfg(feature = "validation")]
65#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
66pub mod validation;
67
68#[cfg(feature = "retry")]
69#[cfg_attr(docsrs, doc(cfg(feature = "retry")))]
70pub mod retry;
71
72#[cfg(feature = "id")]
73#[cfg_attr(docsrs, doc(cfg(feature = "id")))]
74pub mod id;
75
76#[cfg(feature = "env")]
77#[cfg_attr(docsrs, doc(cfg(feature = "env")))]
78pub mod env;
79
80/// Prelude module for convenient imports.
81///
82/// Import everything commonly needed:
83///
84/// ```rust
85/// use commons::prelude::*;
86/// ```
87pub mod prelude {
88    #[cfg(feature = "error")]
89    pub use crate::error::{CommonError, CommonResult};
90
91    #[cfg(feature = "config")]
92    pub use crate::config::{Config, ConfigBuilder, ConfigError};
93
94    #[cfg(feature = "logging")]
95    pub use crate::logging::{LogLevel, Logger};
96
97    #[cfg(feature = "time")]
98    pub use crate::time::{format_duration, parse_duration, unix_timestamp, unix_timestamp_millis};
99
100    #[cfg(feature = "collections")]
101    pub use crate::collections::LruCache;
102
103    #[cfg(feature = "validation")]
104    pub use crate::validation::{
105        Validator, is_valid_email, is_valid_url, validate_length, validate_range,
106    };
107
108    #[cfg(feature = "retry")]
109    pub use crate::retry::{BackoffStrategy, RetryConfig, retry};
110
111    #[cfg(feature = "id")]
112    pub use crate::id::{IdFormat, IdGenerator, generate_id, generate_prefixed_id};
113
114    #[cfg(feature = "env")]
115    pub use crate::env::{get_env, get_env_or, is_development, is_production, require_env};
116}
117
118/// Crate version.
119pub const VERSION: &str = env!("CARGO_PKG_VERSION");
120
121/// Returns the crate version.
122#[must_use]
123pub fn version() -> &'static str {
124    VERSION
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn test_version() {
133        assert_eq!(version(), "0.0.1");
134    }
135}