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/// Represents errors that occur when the input string is empty.
11#[derive(Debug, Error)]
12#[error("received an empty string")]
13#[cfg_attr(
14    feature = "diagnostics",
15    derive(Diagnostic),
16    diagnostic(code(non_empty_str::empty), help("make sure the string is non-empty"))
17)]
18pub struct Empty;
19
20/// Checks whether the given string is non-empty.
21///
22/// # Errors
23///
24/// Returns [`Empty`] if the string is empty.
25pub const fn check_str(string: &str) -> Result<(), Empty> {
26    const_early!(string.is_empty() => Empty);
27
28    Ok(())
29}
30
31/// Similar to [`check_str`], but is generic over the input type.
32///
33/// # Errors
34///
35/// Returns [`Empty`] if the input is empty.
36pub fn check<S: AsRef<str>>(string: S) -> Result<(), Empty> {
37    check_str(string.as_ref())
38}