non_empty_str/
empty.rs

1//! Checking for emptiness.
2
3use const_macros::const_early;
4
5#[cfg(feature = "diagnostics")]
6use miette::Diagnostic;
7
8use thiserror::Error;
9
10/// The message for errors returned on empty strings.
11pub const EMPTY: &str = "the string is empty";
12
13/// Represents errors that occur when the input string is empty.
14#[derive(Debug, Error)]
15#[error("the string is empty")]
16#[cfg_attr(
17    feature = "diagnostics",
18    derive(Diagnostic),
19    diagnostic(code(non_empty_str::empty), help("make sure the string is non-empty"))
20)]
21pub struct Empty;
22
23/// Checks whether the given string is non-empty.
24///
25/// # Errors
26///
27/// Returns [`Empty`] if the string is empty.
28pub const fn check_str(string: &str) -> Result<(), Empty> {
29    const_early!(string.is_empty() => Empty);
30
31    Ok(())
32}
33
34/// Similar to [`check_str`], but is generic over the input type.
35///
36/// # Errors
37///
38/// Returns [`Empty`] if the input is empty.
39pub fn check<S: AsRef<str>>(string: S) -> Result<(), Empty> {
40    check_str(string.as_ref())
41}