option_like/
lib.rs

1//! Create your own enum type that behaves like Rust's `Option` but with custom names.
2//!
3//! # Example
4//!
5//! ```
6//! use option_like::option_like;
7//!
8//! option_like!(
9//!     #[derive(Debug, PartialEq)]
10//!     pub enum Cached<T> {
11//!         Hit(T),
12//!         Miss,
13//!     }
14//!
15//!     is_some => is_hit
16//!     is_none => is_miss
17//! );
18//!
19//! // Create instances
20//! let c1 = Cached::<u32>::Hit(42);
21//! let c2 = Cached::<u32>::Miss;
22//!
23//! // Boolean tests
24//! assert!(c1.is_hit());
25//! assert!(c2.is_miss());
26//!
27//! // Convert to Option
28//! assert_eq!(Option::<u32>::from(c1), Some(42));
29//! assert_eq!(Option::<u32>::from(c2), None);
30//!
31//! // Convert from Option
32//! assert_eq!(Cached::<u32>::from(Some(42)), Cached::Hit(42));
33//! assert_eq!(Cached::<u32>::from(None), Cached::Miss);
34//! ```
35
36#![no_std]
37
38/// Creates a new enum type that behaves like Rust's `Option<T>` but with custom names.
39///
40/// This macro allows you to create your own Option-like enum with customized names for the variants
41/// and boolean test methods, while providing automatic conversions to and from the standard Option type.
42///
43/// # Parameters
44///
45/// - `$(#[$meta:meta])*`: Optional attributes to apply to the enum (e.g., `#[derive(...)]`)
46/// - `$vis`: Visibility of the enum (e.g., `pub`)
47/// - `$name`: Name of the enum (e.g., `Cached`)
48/// - `$some`: Name of the variant that holds a value (e.g., `Hit`)
49/// - `$none`: Name of the empty variant (e.g., `Miss`)
50/// - `is_some => $is_some`: Name of the method that checks if the enum holds a value (e.g., `is_hit`)
51/// - `is_none => $is_none`: Name of the method that checks if the enum is empty (e.g., `is_miss`)
52#[macro_export]
53macro_rules! option_like {
54    (
55        $(#[$meta:meta])*
56        $vis:vis enum $name:ident<T> {
57            $(#[$some_meta:meta])*
58            $some:ident(T),
59            $(#[$none_meta:meta])*
60            $none:ident,
61        }
62
63        is_some => $is_some:ident
64        is_none => $is_none:ident
65    ) => {
66        $(#[$meta])*
67        $vis enum $name<T> {
68            $(#[$some_meta])*
69            $some(T),
70            $(#[$none_meta])*
71            $none,
72        }
73
74        use $name::*;
75
76        impl<T> $name<T> {
77            pub fn $is_some(&self) -> bool {
78                match self {
79                    $some(_) => true,
80                    $none => false,
81                }
82            }
83
84            pub fn $is_none(&self) -> bool {
85                match self {
86                    $some(_) => false,
87                    $none => true,
88                }
89            }
90        }
91
92        impl<T> From<Option<T>> for $name<T> {
93            fn from(value: Option<T>) -> Self {
94                match value {
95                    Some(inner) => $some(inner),
96                    None => $none
97                }
98            }
99        }
100
101        impl<T> From<$name<T>> for Option<T> {
102            fn from(value: $name<T>) -> Option<T> {
103                match value {
104                    $some(inner) => Some(inner),
105                    $none => None
106                }
107            }
108        }
109    };
110}
111
112#[cfg(test)]
113mod tests {
114    option_like!(
115        #[derive(Ord, PartialOrd, Eq, PartialEq, Default, Clone, Debug)]
116        enum Cached<T> {
117            Hit(T),
118            #[default]
119            Miss,
120        }
121
122        is_some => is_hit
123        is_none => is_miss
124    );
125
126    const HIT: Cached<bool> = Hit(true);
127    const MISS: Cached<bool> = Miss;
128
129    #[test]
130    fn test_boolean_methods() {
131        assert!(HIT.is_hit());
132        assert!(MISS.is_miss());
133    }
134
135    #[test]
136    fn test_from() {
137        assert_eq!(Option::<bool>::from(HIT.clone()), Some(true));
138        assert_eq!(Option::<bool>::from(MISS.clone()), None);
139        assert_eq!(Cached::<bool>::from(Some(true)), Hit(true));
140        assert_eq!(Cached::<bool>::from(None), Miss);
141    }
142}