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"
  • Multiple units: "1m30s", "1h2m3s", "2s500ms"
  • 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 1 millisecond using a number
smart_sleep(1).unwrap();

// Sleep for 1 millisecond using text with units
smart_sleep("1ms").unwrap();

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

// Sleep for 1 millisecond with full unit name
smart_sleep("1 millisecond").unwrap();

// Sleep using combined units
smart_sleep("1s1ms").unwrap(); // 1 second 1 millisecond

// 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.