smart_sleep

Function smart_sleep 

Source
pub fn smart_sleep<S>(input: S) -> Result<()>
where S: Into<SleepInput>,
Expand description

Smart sleep function that supports multiple input formats.

This function accepts various input types and automatically converts them to appropriate sleep durations. Zero and negative values result in no sleep.

§Supported Formats

  • Numbers: 100, 500 (interpreted as milliseconds)
  • Text with units: "100ms", "2s", "1.5s", "2 minutes"
  • Plain text: "100" (interpreted as milliseconds)
  • Duration objects: Duration::from_millis(100)
  • Zero/negative: 0, -100 (no sleep performed)

§Examples

use sleep_utils::smart_sleep;

// Sleep for 100 milliseconds using a number
smart_sleep(100).unwrap();

// Sleep for 200 milliseconds using text with units
smart_sleep("200ms").unwrap();

// Sleep for 1.5 seconds
smart_sleep("1.5s").unwrap();

// Sleep for 2 seconds with full unit name
smart_sleep("2 seconds").unwrap();

// No sleep for zero values
smart_sleep(0).unwrap();

// No sleep for negative values
smart_sleep(-100).unwrap();

§Errors

Returns [SleepError::InvalidDuration] if the input string cannot be parsed as a valid duration.

§Panics

This function does not panic. All errors are returned as Result.