simple_ternary/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
extern crate proc_macro;
use quote::quote;
use syn::{self, parse::{Parse, ParseStream}, parse_macro_input, Token};
// ternary: expression '?' expression ':' expression
// expression: ternary | simple
struct Ternary {
condition: syn::Expr,
true_branch: Expression,
false_branch: Expression,
}
impl Parse for Ternary {
fn parse(input: ParseStream) -> syn::Result<Self> {
let condition = input.parse()?;
#[cfg(not(feature = "try"))]
{
input.parse::<Token![?]>()?;
}
#[cfg(feature = "try")]
{
input.parse::<Token![=>]>()?;
}
let true_branch = input.parse()?;
input.parse::<Token![:]>()?;
let false_branch = input.parse()?;
Ok(Self {
condition,
true_branch,
false_branch,
})
}
}
impl quote::ToTokens for Ternary {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let condition = &self.condition;
let true_branch = match &self.true_branch {
Expression::Ternary(ternary) => quote! { #ternary },
Expression::Simple(simple) => quote! { #simple },
};
let false_branch = match &self.false_branch {
Expression::Ternary(ternary) => quote! { #ternary },
Expression::Simple(simple) => quote! { #simple },
};
tokens.extend(quote! { if #condition { #true_branch } else { #false_branch } });
}
}
struct SimpleExpression {
expr: syn::Expr,
}
impl Parse for SimpleExpression {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
expr: input.parse()?,
})
}
}
impl quote::ToTokens for SimpleExpression {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let expr = &self.expr;
tokens.extend(quote! { #expr });
}
}
enum Expression {
Ternary(Box<Ternary>),
Simple(SimpleExpression),
}
impl Parse for Expression {
fn parse(input: ParseStream) -> syn::Result<Self> {
let expr: syn::Expr = input.parse()?;
#[cfg(not(feature = "try"))]
{
if input.peek(Token![?]) {
input.parse::<Token![?]>()?;
let true_branch = input.parse()?;
input.parse::<Token![:]>()?;
let false_branch = input.parse()?;
return Ok(Self::Ternary(Box::new(Ternary {
condition: expr,
true_branch,
false_branch,
})))
}
}
#[cfg(feature = "try")]
{
if input.peek(Token![=>]) {
input.parse::<Token![=>]>()?;
let true_branch = input.parse()?;
input.parse::<Token![:]>()?;
let false_branch = input.parse()?;
return Ok(Self::Ternary(Box::new(Ternary {
condition: expr,
true_branch,
false_branch,
})))
}
}
Ok(Self::Simple(SimpleExpression { expr }))
}
}
/// A macro that evaluates a ternary expression, similar to the ternary operator in other languages.
///
/// # Syntax
///
/// ```rust
/// tnr!{ condition ? true_expr : false_expr }
/// ```
///
/// - `condition`: An expression that evaluates to a boolean.
/// - `true_expr`: The expression to return if the condition is `true`.
/// - `false_expr`: The expression to return if the condition is `false`.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust
/// let age = 20;
/// let category = tnr!{ age < 18 ? "child" : "adult" };
/// assert_eq!(category, "adult");
/// ```
///
/// ## Chaining Ternary Expressions
///
/// Ternary expressions can be chained to handle multiple conditions:
///
/// ```rust
/// let score = 85;
/// let grade = tnr! {
/// score >= 90 ? "A" :
/// score >= 80 ? "B" :
/// score >= 70 ? "C" :
/// score >= 60 ? "D" : "F"
/// };
/// assert_eq!(grade, "B");
/// ```
///
/// This macro evaluates the condition and returns the corresponding expression based on its truth value.
///
/// ## Important!!!
/// The `?` operator must be replaced with the `=>` operator if the `try` feature is enabled.
/// The `try` feature allows the `?` operator to be used in the macro's expressions. For example:
///
/// ```rust
/// let age = Ok(5);
/// let category = tnr!{ age? < 18 => "child" : "adult" };
/// ```
/// This syntax change is necessary because the ? operator is reserved for error handling in Rust.
/// Future versions of this crate may switch to use the => operator only.
/// For now, the ? operator is used to maintain syntax compatibility with C/C++.
///
/// # Note
///
/// Ensure that both `true_expr` and `false_expr` are of the same type or can be converted to a common type
/// to avoid type mismatches.
#[proc_macro]
pub fn tnr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let tern = parse_macro_input!(input as Ternary);
quote! { #tern }.into()
}