Skip to main content

secure_gate/macros/
dynamic_alias.rs

1// <file_path>
2// secure-gate\src\macros\dynamic_alias.rs
3// </file_path>
4// <edit_description>
5// Fix the macro file and add cfg
6// </edit_description>
7
8/// Creates a type alias for a dynamic-sized heap-allocated secure secret.
9///
10/// This macro generates a type alias to `Dynamic<T>` with optional visibility and custom documentation.
11/// The generated type inherits all methods from `Dynamic`, including `.expose_secret()`.
12///
13/// # Syntax
14///
15/// - `dynamic_alias!(vis Name, Type);` — visibility required (e.g., `pub`, `pub(crate)`, or omit for private)
16/// - `dynamic_alias!(vis Name, Type, doc);` — with optional custom doc string
17///
18/// # Examples
19///
20/// Public alias:
21/// ```
22/// use secure_gate::{dynamic_alias, ExposeSecret};
23/// dynamic_alias!(pub Password, String);
24/// let pw: Password = "hunter2".into();
25/// assert_eq!(pw.expose_secret(), "hunter2");
26/// ```
27///
28/// Private alias:
29/// ```
30/// use secure_gate::{dynamic_alias, ExposeSecret};
31/// dynamic_alias!(SecretString, String); // No visibility modifier = private
32/// let secret: SecretString = "hidden".to_string().into();
33/// assert_eq!(secret.expose_secret(), "hidden");
34/// ```
35///
36/// With custom visibility:
37/// ```
38/// use secure_gate::dynamic_alias;
39/// dynamic_alias!(pub(crate) InternalSecret, String); // Crate-visible
40/// ```
41///
42/// With custom documentation:
43/// ```
44/// use secure_gate::{dynamic_alias, ExposeSecret};
45/// dynamic_alias!(pub Token, Vec<u8>, "OAuth token for API access");
46/// let token: Token = vec![1, 2, 3].into();
47/// assert_eq!(token.expose_secret(), &[1, 2, 3]);
48/// ```
49///
50/// The generated type is zero-cost and works with all features.
51/// For random initialization, use `Type::from_random(n)` (requires 'rand' feature).
52#[cfg(feature = "alloc")]
53#[macro_export]
54macro_rules! dynamic_alias {
55    ($vis:vis $name:ident, $inner:ty, $doc:literal) => {
56        #[doc = $doc]
57        $vis type $name = $crate::Dynamic<$inner>;
58    };
59    ($vis:vis $name:ident, $inner:ty) => {
60        #[doc = concat!("Secure heap-allocated ", stringify!($inner))]
61        $vis type $name = $crate::Dynamic<$inner>;
62    };
63    ($name:ident, $inner:ty, $doc:literal) => {
64        #[doc = $doc]
65        type $name = $crate::Dynamic<$inner>;
66    };
67    ($name:ident, $inner:ty) => {
68        #[doc = concat!("Secure heap-allocated ", stringify!($inner))]
69        type $name = $crate::Dynamic<$inner>;
70    };
71}