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
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()?;
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()?;
if input.peek(Token![?]) {
input.parse::<Token![?]>()?;
let true_branch = input.parse()?;
input.parse::<Token![:]>()?;
let false_branch = input.parse()?;
Ok(Self::Ternary(Box::new(Ternary {
condition: expr,
true_branch,
false_branch,
})))
}
else {
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.
///
/// # 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()
}