non_empty_str/
macros.rs

1//! Macros for creating non-empty strings.
2
3/// Constructs [`NonEmptyStr`] from the given string, panicking if the it is empty.
4///
5/// # Examples
6///
7/// Simple usage:
8///
9/// ```
10/// use non_empty_str::non_empty_str;
11///
12/// let nekit = non_empty_str!("nekit");
13/// ```
14///
15/// Panicking if the string is empty:
16///
17/// ```should_panic
18/// use non_empty_str::non_empty_str;
19///
20/// let never = non_empty_str!("");
21/// ```
22///
23/// Compilation failure when in `const` contexts:
24///
25/// ```compile_fail
26/// use non_empty_str::non_empty_str;
27///
28/// let never = const { non_empty_str!("") };
29/// ```
30///
31/// [`NonEmptyStr`]: crate::str::NonEmptyStr
32#[macro_export]
33macro_rules! non_empty_str {
34    ($string: expr) => {
35        $crate::str::NonEmptyStr::from_str($string).expect($crate::str::EMPTY_STR)
36    };
37}
38
39/// Similar to [`non_empty_str!`] but for `const` contexts.
40///
41/// Note that the provided expression must be const-evaluatable, else the compilation will fail.
42///
43/// # Examples
44///
45/// ```
46/// use non_empty_str::const_non_empty_str;
47///
48/// let message = const_non_empty_str!("Hello, world!");
49/// ```
50///
51/// Failing compilation on empty strings:
52///
53/// ```compile_fail
54/// use non_empty_str::const_non_empty_str;
55///
56/// let never = const_non_empty_str!("");
57/// ```
58#[macro_export]
59macro_rules! const_non_empty_str {
60    ($string: expr) => {
61        const { $crate::non_empty_str!($string) }
62    };
63}