refinement_types/
type_str.rs

1//! Type-level strings.
2
3use crate::static_str::StaticStr;
4
5/// Represents type-level strings.
6pub trait TypeStr {
7    /// The string value.
8    const VALUE: StaticStr;
9}
10
11/// Lifts static strings to type-level strings.
12///
13/// # Examples
14///
15/// ```
16/// use refinement_types::type_str;
17///
18/// type_str!(HelloWorld = "Hello, world!");
19/// ```
20///
21/// Is equivalent to:
22///
23/// ```
24/// use refinement_types::{StaticStr, TypeStr};
25///
26/// #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
27/// struct HelloWorld;
28///
29/// impl TypeStr for HelloWorld {
30///     const VALUE: StaticStr = "Hello, world!";
31/// }
32/// ```
33#[macro_export]
34macro_rules! type_str {
35    ($vis: vis $name: ident = $value: expr $(=> $doc: expr)?) => {
36        $(
37            #[doc = $doc]
38        )?
39        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
40        $vis struct $name;
41
42        impl $crate::type_str::TypeStr for $name {
43            const VALUE: $crate::static_str::StaticStr = $value;
44        }
45    };
46}