Skip to main content

x402_types/util/
lit_str.rs

1//! Compile-time string literal type generation.
2//!
3//! This module provides the [`lit_str!`](macro@lit_str) macro for creating types that
4//! represent specific string literals at compile time. These types are
5//! useful for ensuring type safety when working with fixed string values
6//! in protocol messages.
7//!
8//! # Example
9//!
10//! ```rust
11//! use x402_types::lit_str;
12//!
13//! lit_str!(ExactScheme, "exact");
14//!
15//! // The type only accepts the exact string
16//! let scheme: ExactScheme = "exact".parse().unwrap();
17//! assert_eq!(scheme.to_string(), "exact");
18//!
19//! // Other strings are rejected
20//! assert!("other".parse::<ExactScheme>().is_err());
21//! ```
22
23/// Creates a type that represents a specific string literal.
24///
25/// The generated type:
26/// - Has a `VALUE` constant with the string
27/// - Implements `FromStr` (only accepts the exact string)
28/// - Implements `Serialize`/`Deserialize` (as the string)
29/// - Implements `Display` (outputs the string)
30#[macro_export]
31macro_rules! lit_str {
32    ($struct_name:ident, $val:expr) => {
33        #[doc = concat!("A unit struct representing the string literal \"", $val, "\".")]
34        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35        pub struct $struct_name;
36
37        impl $struct_name {
38            pub const VALUE: &'static str = $val;
39        }
40
41        impl AsRef<str> for $struct_name {
42            fn as_ref(&self) -> &str {
43                Self::VALUE
44            }
45        }
46
47        impl std::str::FromStr for $struct_name {
48            type Err = String;
49            fn from_str(s: &str) -> Result<Self, Self::Err> {
50                if s == Self::VALUE {
51                    Ok($struct_name)
52                } else {
53                    Err(format!("expected '{}', got '{}'", Self::VALUE, s))
54                }
55            }
56        }
57
58        impl serde::Serialize for $struct_name {
59            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
60                serializer.serialize_str(Self::VALUE)
61            }
62        }
63
64        impl<'de> serde::Deserialize<'de> for $struct_name {
65            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
66                let s = String::deserialize(deserializer)?;
67                if s == Self::VALUE {
68                    Ok($struct_name)
69                } else {
70                    Err(serde::de::Error::custom(format!(
71                        "expected '{}', got '{}'",
72                        Self::VALUE,
73                        s
74                    )))
75                }
76            }
77        }
78
79        impl std::fmt::Display for $struct_name {
80            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81                write!(f, $val)
82            }
83        }
84    };
85}