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