stylex_macros/conversion_macros.rs
1//! Conversion and type extraction macros for safe value unwrapping.
2//!
3//! These macros provide standardized patterns for converting between different
4//! types and extracting values with proper error handling.
5
6/// Macro to safely convert an expression to a string with error Result handling.
7/// Returns the string on success, or returns Err(anyhow::Error) on failure.
8///
9/// This macro is designed for use in functions that return Result<T, anyhow::Error>.
10///
11/// # Usage
12/// ```ignore
13/// let str_value = expr_to_str_or_err!(expr, traversal_state, fns, "Expression is not a string");
14/// ```
15///
16/// # Arguments
17/// - `$expr`: The expression to convert
18/// - `$traversal_state`: Mutable reference to StateManager
19/// - `$fns`: Reference to FunctionMap
20/// - `$error_msg`: Error message string literal
21#[macro_export]
22macro_rules! convert_expr_to_str_or_err {
23 ($expr:expr, $traversal_state:expr, $fns:expr, $error_msg:expr) => {
24 match convert_expr_to_str($expr, $traversal_state, $fns) {
25 Some(s) => s,
26 None => return Err(anyhow!($error_msg)),
27 }
28 };
29}
30
31/// Macro to unwrap an Option<EvaluateResultValue> to Expr or return an error.
32/// Returns the expression on success, or returns Err(anyhow::Error) on failure.
33///
34/// This macro is designed for use in functions that return Result<T, anyhow::Error>.
35///
36/// # Usage
37/// ```ignore
38/// let expr = as_expr_or_err!(result_value, "Argument not expression");
39/// ```
40///
41/// # Arguments
42/// - `$opt`: The EvaluateResultValue to unwrap
43/// - `$error_msg`: Error message string literal
44#[macro_export]
45macro_rules! as_expr_or_err {
46 ($opt:expr, $error_msg:expr) => {
47 match $opt.as_expr() {
48 Some(expr) => expr,
49 None => return Err(anyhow!($error_msg)),
50 }
51 };
52}
53
54/// Macro to unwrap an Option<EvaluateResultValue> to Expr for functions returning Option<Result>.
55/// Returns the expression on success, or returns Some(Err(anyhow::Error)) on failure.
56///
57/// This macro is designed for use in functions that return Option<Result<T, anyhow::Error>>.
58///
59/// # Usage
60/// ```ignore
61/// let expr = as_expr_or_opt_err!(result_value, "Argument not expression");
62/// ```
63///
64/// # Arguments
65/// - `$opt`: The EvaluateResultValue to unwrap
66/// - `$error_msg`: Error message string literal
67#[macro_export]
68macro_rules! as_expr_or_opt_err {
69 ($opt:expr, $error_msg:expr) => {
70 match $opt.as_expr() {
71 Some(expr) => expr,
72 None => return Some(Err(anyhow!($error_msg))),
73 }
74 };
75}
76
77/// Macro to unwrap an Option<EvaluateResultValue> to Expr for functions returning primitives.
78/// Returns the expression on success, or panics with the error message on failure.
79///
80/// This macro is designed for use in functions that return primitive types like f64
81/// where error handling must be done via panic.
82///
83/// # Usage
84/// ```ignore
85/// let expr = as_expr_or_panic!(result_value, "Argument not expression");
86/// ```
87///
88/// # Arguments
89/// - `$opt`: The EvaluateResultValue to unwrap
90/// - `$error_msg`: Error message string literal
91#[macro_export]
92macro_rules! as_expr_or_panic {
93 ($opt:expr, $error_msg:expr) => {
94 match $opt.as_expr() {
95 Some(expr) => expr,
96 None => $crate::panic_macros::__stylex_panic($crate::panic_macros::stylex_err(format!(
97 "{}",
98 $error_msg
99 ))),
100 }
101 };
102}