3 unstable releases

new 0.2.0 Jul 3, 2024
0.1.1 Mar 9, 2024
0.1.0 Mar 9, 2024

#134 in Procedural macros

Download history 310/week @ 2024-03-09 11/week @ 2024-03-16 1/week @ 2024-03-23 30/week @ 2024-03-30 8/week @ 2024-04-06 1/week @ 2024-05-18

135 downloads per month

Apache-2.0

15KB
153 lines

macrors

A simple custom macro library in Rust.

APIs Documents

1.Usage

Add this to your Cargo.toml:

[dependencies]
macrors = "0.1"

2.APIs

2.1.Operator

2.1.1.ternary

let seed = 10;

let pos = ternary!(seed % 2 == 0, 1, -1);
let neg = ternary!((seed - 1) % 2 == 0, 1, -1);
assert_eq!(1, pos);
assert_eq!(-1, neg);
// bool
let seed = 10;

let is_even = ternary!(seed % 2 == 0, true, false);
assert!(is_even);
// &str
let seed = 10;

let result = ternary!(seed > 5, "greater than 5", "less than or equal to 5");
assert_eq!("greater than 5", result);

2.1.2.ternary_eq

let eq = ternary_eq!(2, 2, 1, -1);
let ne = ternary_eq!(1, 2, 1, -1);

assert_eq!(1, eq);
assert_eq!(-1, ne);
// date-time
let now = SystemTime::now();
let now_add = now.add(Duration::from_millis(1_000));
let now_sub = now.sub(Duration::from_millis(1_000));

let eq = ternary_eq!(now, now, 1, -1);
let ne = ternary_eq!(now, now.add(Duration::from_millis(1_000)), now_add, now_sub);

assert_eq!(1, eq);
assert_eq!(now_sub, ne);

2.1.3.ternary_ne

let ne = ternary_ne!(3, 2, 1, -1);
let eq = ternary_ne!(2, 2, 1, -1);

assert_eq!(1, ne);
assert_eq!(-1, eq);
// date-time
let now = SystemTime::now();
let now_add = now.add(Duration::from_millis(1_000));
let now_sub = now.sub(Duration::from_millis(1_000));

let ne = ternary_ne!(now, now.add(Duration::from_millis(1_000)), 1, -1);
let eq = ternary_ne!(now, now, now_add,now_sub);

assert_eq!(1, ne);
assert_eq!(now_sub, eq);

2.2.Repeat

  • @since 0.2.0

2.2.1.repeat!

let repeat_1 = repeat!("A", 5);
assert_eq!(vec!["A", "A", "A", "A", "A"], repeat_1);

let repeat_2 = repeat!(101, 5);
assert_eq!(vec![101, 101, 101, 101, 101], repeat_2);

2.2.2.repeat_str!

// Item: Alphabet
// Default separator -> `""`
let repeat_str_1 = repeat_str!("A", 5);
assert_eq!("AAAAA", repeat_str_1);

// Item: Alphabet
// Custem separator -> `,`
let repeat_str_2 = repeat_str!("A", 5, ",");
assert_eq!("A,A,A,A,A", repeat_str_2);

// Item: Number 
let repeat_str_3 = repeat_str!(101, 5, ",");
assert_eq!("101,101,101,101,101", repeat_str_3);

No runtime deps