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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#[macro_export]
#[doc(hidden)]
macro_rules! __rust_helpers_if_else__ {
($cond:expr, $if:expr, $else:expr) => {
if $cond {
$if
} else {
$else
}
};
}
/// Match if macro provides a match like syntax to use make if statements
/// ## Usage syntax:
/// There are two syntaxes for this macro.
/// ```
/// //Unbound syntax
/// let result = rust_helpers::match_if!(
/// 15<10 => 15;
/// 15>10 => {
/// println!("15 is greater than 10");
/// 10
/// };
/// _ =>{
/// println!("15 is equal to 10");
/// 0
/// };
/// );
/// assert_eq!(result, 10);
/// //Bound syntax
/// rust_helpers::match_if!(
/// with (15){
/// (<10) => {println!("15 is less than 10");15};
/// (>10) => {println!("15 is greater than 10");10};
/// (==10) => {println!("15 is equal to 10");0};
/// _ => panic!("This should not happen");
/// }
/// );
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! __rust_helpers_match_if__ {
(with ($base:expr) {
($($cond1:tt)+)=> $block:expr; $(($($conds:tt)+) => $blocks:expr;)* $(_ => $block3:expr$(;)?)?
}
)=>{
$crate::match_if!(
$base $($cond1)+ => $block;
$($base $($conds)+ => $blocks;)*
$(_ => $block3)?
)
};
($cond1:expr => $block:expr; $($conds:expr => $blocks:expr;)* $(_ => $block3:expr$(;)?)?) => {
if $cond1 {
$block
} $(
else if $conds{
$blocks
}
)*
$(
else {
$block3
}
)?
};
}
/// # Inherit Struct Fields
/// This macro is used to inherit common fields one struct to another.
/// You simply define the struct with different fields as you would normally do in rust and wrap it in this macro.
/// ## Usage syntax:
/// ```ignore
/// extends!(
/// [meta]*
/// [pub] base struct [name]{
/// [
/// [meta]*
/// [pub] [field]: [type]
/// ],*
/// }
/// [meta]*
/// [pub] struct [name]{
/// [
/// [meta]*
/// [pub] [field]: [type]
/// ],*
/// }
/// );
/// ```
/// ## Usage Example:
/// ```rust
/// rust_helpers::extends!{
/// pub base struct Base{
/// pub a: u32,
/// }
/// #[derive(Debug, Clone)]
/// pub struct Inherited{
/// pub b:String,
/// //#[serde(skip)]
/// pub others: Vec<usize>,
/// }
/// }
/// let a = Inherited{
/// a: 1,
/// b: "hello".to_string(),
/// others: vec![1,2,3],
/// };
/// ```
/// Both struct will have the common fields from the base struct.
///
#[macro_export]
#[doc(hidden)]
macro_rules! __rust_helpers_extends__ {
(
$(
$(#[$($base_child_meta:meta),*])*
$base_pub_i:vis $base_field:ident: $base_type:ty
),* $(,)? ;
$(#[$($top_meta:meta),*])* $pub:vis struct $name:ident$(<$l:lifetime>)? {
$(
$(#[$($child_meta:meta),*])*
$pub_i:vis $field:ident: $type:ty
),* $(,)?
}
)=>{
$(#[$($top_meta),*])*
$pub struct $name$(<$l>)?{
$(
$(#[$($base_child_meta),*])*
$base_pub_i $base_field : $base_type,
)*
$(
$(#[$($child_meta),*])*
$pub_i $field : $type
),*
}
};
(
$(#[$($base_top_meta:meta),*])* $base_pub:vis base struct $base_name:ident$(<$base_l:lifetime>)? {$(
$(#[$($base_child_meta:meta),*])*
$base_pub_i:vis $base_field:ident: $base_type:ty),* $(,)?
}
$(
$(#[$($top_meta:meta),*])* $pub:vis struct $name:ident$(<$l:lifetime>)? {
$(
$(#[$($child_meta:meta),*])*
$pub_i:vis $field:ident: $type:ty),* $(,)?
}
)*
) => {
$(#[$($base_top_meta),*])*
$base_pub struct $base_name$(<$base_l>)?{
$(
$(#[$($base_child_meta),*])*
$base_pub_i $base_field : $base_type
),*
}
$crate::extends!(
$(
$(#[$($base_child_meta),*])*
$base_pub_i $base_field: $base_type
),*;
$($(#[$($top_meta),*])* $pub struct $name$(<$l>)? {
$(
$(#[$($child_meta),*])*
$pub_i $field: $type
),*
})*
);
}
}
#[doc(inline)]
pub use __rust_helpers_extends__ as extends;
#[doc(inline)]
pub use __rust_helpers_if_else__ as if_else;
#[doc(inline)]
pub use __rust_helpers_match_if__ as match_if;
#[cfg(test)]
mod tests {
pub use super::{extends, if_else, match_if};
#[test]
fn test_extends() {
extends! {
#[allow(unused)]
pub base struct Base{
pub a: u32,
pub g: u32,
pub foo: u32,
}
#[derive(Debug, Clone,serde::Serialize)]
pub struct Inherited{
pub b:String,
#[serde(skip)]
pub others: Vec<usize>,
}
}
let a = Inherited {
a: 1,
g: 2,
foo: 3,
b: "hello".to_string(),
others: vec![1, 2, 3],
};
assert_eq!(a.a, 1);
assert_eq!(a.b, "hello");
assert_eq!(a.others, vec![1, 2, 3]);
}
#[test]
fn test_if_else() {
let a = 1;
let b = 2;
let c = if_else!(a > b, a, b);
assert_eq!(c, 2);
}
#[test]
fn test_match_if() {
let a = 1;
let b = 2;
let c = match_if!(
a > b => a;
a < b => b;
_ => 0
);
assert_eq!(c, b);
}
#[test]
fn test_match_if_with() {
let a = 1;
let b = 2;
let c = match_if!(
with (a){
(> b) => a;
(< b) => b;
_ => 0;
}
);
assert_eq!(c, b);
}
}