or_rs_macros/lib.rs
1//! Macros for use with [Or](../or/trait.Or.html) type.
2
3// when compile with nightly, you need `proc_macro_span`
4#![cfg_attr(feature = "macro_error_debugging", feature(proc_macro_span))]
5
6#[allow(unused_extern_crates)]
7extern crate proc_macro;
8
9mod error;
10mod parser;
11use proc_macro::TokenStream;
12
13/// A proc macro that converts `if` or `match` expressions that return multiple types
14/// into [Or](../or/trait.Or.html) types.
15///
16/// Note that this feature depends on the unstable feature `proc_macro_hygiene`,
17/// so you must add `#![feature(proc_macro_hygiene)]` annotation when you use it.
18///
19///
20/// ## Example: usage for if expression
21///
22/// ```rust
23/// #![feature(proc_macro_hygiene)]
24///
25/// use or_rs_macros::or_gen;
26/// use or_rs::enums::Or3;
27///
28/// #[or_gen]
29/// // add a type annotation explicitly
30/// let s: Or3<i32, String, f32> = if true {
31/// 3
32/// } else if false {
33/// "hello".to_string()
34/// } else {
35/// 3.0
36/// };
37/// ```
38///
39/// ## Example: usage for match expression
40///
41/// ```rust
42/// #![feature(proc_macro_hygiene)]
43///
44/// use or_rs_macros::or_gen;
45/// use or_rs::enums::Or3;
46///
47/// #[or_gen]
48/// // add a type annotation explicitly
49/// let s: Or3<i32, f32, String> = match 42 {
50/// 1 => 22,
51/// 10 => 3.2,
52/// _ => "hello".to_string(),
53/// };
54/// ```
55///
56///
57#[proc_macro_attribute]
58pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream {
59 parser::MacroParser::parse(item)
60}