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
// Copyright (c) 2021 jmjoy
// Helper is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
#![warn(rust_2018_idioms, non_ascii_idents)]
#![warn(clippy::dbg_macro, clippy::print_stdout)]
#![doc = include_str!("../README.md")]
pub(crate) mod collections;
pub(crate) mod control;
pub(crate) mod utils;
use proc_macro::TokenStream;
/// Ternary operator in many C-like languages.
///
/// `either!(expr ? left : right)` expand to `if expr { left } else { right }`.
///
/// # Example
/// ```
/// use helper::either;
///
/// assert_eq!(either!(1 > 2 ? "1" : "2"), "2");
/// ```
#[proc_macro]
pub fn either(items: TokenStream) -> TokenStream {
control::either(items.into()).into()
}
/// The [`std::option::Option`] relative operations.
///
/// # Operations
///
/// ## unwrap .. or ..
///
/// `option!(unwrap .. or ..)` expand to `match .. { Some(x) => x, None => ..
/// }`.
///
/// ### Example
/// ```
/// use helper::option;
///
/// // Like Option::unwrap_or.
/// assert_eq!(option!(unwrap Some(true) or false), true);
/// assert_eq!(option!(unwrap None or false), false);
///
/// // Return in advance.
/// fn foo() -> bool {
/// let x = option!(unwrap Some("foo") or return false);
/// x == "foo"
/// }
/// assert!(foo());
///
/// // Break in advance.
/// let x = loop {
/// option!(unwrap None or break 1i32);
/// };
/// assert_eq!(x, 1);
/// ```
#[proc_macro]
#[deprecated = "For `unwrap` operation, use `try_option` instead."]
pub fn option(items: TokenStream) -> TokenStream {
control::option(items.into()).into()
}
/// Unwrap [`std::option::Option`], if none, return the alternative value.
///
/// `try_option!(..)` expand to `match .. { Some(x) => x, None => return, }`.
///
/// `try_option!(.. ? ..)` expand to `match .. { Some(x) => x, None => return ..
/// }`.
///
/// ### Example
/// ```
/// use helper::try_option;
///
/// fn foo(b: &mut bool) {
/// try_option!(Some(()));
/// *b = true
/// }
/// let mut b = false;
/// foo(&mut b);
/// assert!(b);
///
/// fn bar() -> bool {
/// let x = try_option!(Some("bar") ? false);
/// x == "bar"
/// }
/// assert!(bar());
/// ```
#[proc_macro]
pub fn try_option(items: TokenStream) -> TokenStream {
control::try_option(items.into()).into()
}
/// Create [`std::collections::HashMap`] from list of key-value pairs.
///
/// # Example
/// ```
/// use helper::hmap;
///
/// let m = hmap! {
/// "a": 1,
/// "b": 2,
/// };
/// assert_eq!(m["a"], 1);
/// assert_eq!(m["b"], 2);
/// ```
#[proc_macro]
pub fn hmap(items: TokenStream) -> TokenStream {
collections::hmap(items.into()).into()
}
/// Create [`std::collections::BTreeMap`] from list of key-value pairs.
///
/// # Example
/// ```
/// use helper::btmap;
///
/// let m = btmap! {
/// "a": 1,
/// "b": 2,
/// };
/// assert_eq!(m["a"], 1);
/// assert_eq!(m["b"], 2);
/// ```
#[proc_macro]
pub fn btmap(items: TokenStream) -> TokenStream {
collections::btmap(items.into()).into()
}
/// Create a [`std::collections::HashSet`] from a list of elements.
///
/// # Example
/// ```
/// use helper::hset;
///
/// let set = hset! {"a", "b"};
/// assert!(set.contains("a"));
/// assert!(set.contains("b"));
/// ```
#[proc_macro]
pub fn hset(items: TokenStream) -> TokenStream {
collections::hset(items.into()).into()
}
/// Create a [`std::collections::BTreeSet`] from a list of elements.
///
/// # Example
/// ```
/// use helper::btset;
///
/// let set = btset! {"a", "b"};
/// assert!(set.contains("a"));
/// assert!(set.contains("b"));
/// ```
#[proc_macro]
pub fn btset(items: TokenStream) -> TokenStream {
collections::btset(items.into()).into()
}