sleep_utils/lib.rs
1//! A smart sleep utilities library with flexible input formats and automatic zero-value handling.
2//!
3//! This library provides intelligent sleep functionality that supports multiple input formats
4//! and automatically handles zero and negative values without performing actual sleep.
5//!
6//! # Features
7//!
8//! - **Multiple input formats**: numbers, text, `Duration` objects
9//! - **Automatic zero/negative handling**: no sleep for zero or negative values
10//! - **Multiple time units**: milliseconds, seconds, minutes, hours
11//! - **Combined units**: support for formats like `"1m30s"`, `"1h2m3s"`
12//! - **Platform compatibility**: uses `isize` for cross-platform support
13//! - **High performance**: optimized regex parsing with lazy static patterns
14//!
15//! # Examples
16//!
17//! Basic usage with different input formats:
18//!
19//! ```
20//! use sleep_utils::smart_sleep;
21//! use std::time::Duration;
22//!
23//! // Sleep for 1 millisecond using a number
24//! smart_sleep(1).unwrap();
25//!
26//! // Sleep for 1 millisecond using text with units
27//! smart_sleep("1ms").unwrap();
28//!
29//! // Sleep for 0.001 seconds
30//! smart_sleep("0.001s").unwrap();
31//!
32//! // Sleep for 1 millisecond with full unit name
33//! smart_sleep("1 millisecond").unwrap();
34//!
35//! // Sleep using combined units
36//! smart_sleep("1s1ms").unwrap(); // 1 second 1 millisecond
37//!
38//! // No sleep for zero values
39//! smart_sleep(0).unwrap();
40//!
41//! // No sleep for negative values
42//! smart_sleep(-50).unwrap();
43//!
44//! // Sleep using Duration object
45//! smart_sleep(Duration::from_millis(1)).unwrap();
46//! ```
47//!
48//! # Errors
49//!
50//! Functions return [`Result<T, SleepError>`] and may return the following errors:
51//!
52//! - [`SleepError::InvalidDuration`] when parsing invalid duration strings
53//! - [`SleepError::ParseError`] when encountering parse errors
54//! - [`SleepError::NumberOutOfRange`] when numbers are out of valid range
55
56#![warn(missing_docs)]
57
58use std::time::Duration;
59
60mod duration_parser;
61mod error;
62mod smart_sleep;
63
64pub use duration_parser::parse_sleep_duration;
65pub use error::{Result, SleepError};
66pub use smart_sleep::{smart_sleep, SleepInput};
67
68/// Standard sleep function for backward compatibility with `std::thread::sleep`.
69///
70/// This function provides a simple wrapper around `std::thread::sleep` that returns
71/// a [`Result`] for consistency with other functions in this crate.
72///
73/// # Examples
74///
75/// ```
76/// use sleep_utils::sleep;
77/// use std::time::Duration;
78///
79/// // Sleep for 1 millisecond
80/// sleep(Duration::from_millis(1)).unwrap();
81/// ```
82///
83/// # Notes
84///
85/// Unlike [`smart_sleep`], this function does not support multiple input formats
86/// and will always sleep for the provided duration, even if it's zero.
87pub fn sleep(duration: Duration) -> Result<()> {
88 std::thread::sleep(duration);
89 Ok(())
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95 use std::time::Instant;
96
97 #[test]
98 fn test_basic_sleep() -> Result<()> {
99 let start = Instant::now();
100 smart_sleep(1)?; // Use minimal sleep time for tests
101 let elapsed = start.elapsed();
102 assert!(elapsed >= Duration::from_millis(1));
103 Ok(())
104 }
105}