leetcode_rust/
macros.rs

1//! A module defining several useful macros
2//!
3//! The `new_case!` macro is very useful to simplify TestCase generation
4//! process and you should definitely try it out.
5//!
6//! All macros have been exported to crate level. You might want to use it via
7//! `leetcode_rust::macro_name!`
8
9////////////////////////////////////////////////////////////////////////////////
10
11/// Helper macro to call `create` method of CaseGroup<T, G, P> instance
12///
13/// # Note
14/// You must pass in the casegroup.create method as first parameter of the
15/// macro.
16#[macro_export]
17macro_rules! new_case {
18  ($self:ident.$func:ident, $input:expr, $($output:expr),+) => {
19      $self.$func($input,vec![$($output),+]);
20  };
21}
22
23/// Helper macro to call `create_param` method of CaseGroup<T, G, P> instance
24///
25/// # Note
26/// You must pass in the casegroup.create method as first parameter of the
27/// macro.
28#[macro_export]
29macro_rules! new_case_param {
30    ($self:ident.$func:ident, $input:expr, $output:expr, $param:expr) => {
31        $self.$func($input, $output, $param);
32    };
33}
34
35/// Generate `create` & `create_param` implementation for different types
36///
37/// # Note
38/// The `<String, G, P>` generics combination has been implemented individually,
39/// so you probably should not implement on your own.
40#[macro_export]
41macro_rules! codegen_case_create_impl {
42    ($t:ty, $g:ty, $p:ty) => {
43        /// Implement two handy methods on CaseGroup struct.
44        impl CaseGroup<$t, $g, $p> {
45            /// Create a new test case (no input parameters) matching selected
46            /// generic types.
47            pub fn create(&mut self, ipt: $t, exp: Vec<$g>) {
48                self.add(Case::new(ipt, exp));
49            }
50
51            /// Create a new test case (with input parameters) matching
52            /// selected generic types.
53            pub fn create_param(&mut self, ipt: $t, exp: Vec<$g>, params: Vec<$p>) {
54                self.add(Case::new_params(ipt, params, exp));
55            }
56
57            /// Create a new test case (no input parameters but multi-inputs)
58            /// matching selected generic types.
59            pub fn create_multi(&mut self, ipts: Vec<$t>, exp: Vec<$g>) {
60                self.add(Case::new_multi(ipts, exp));
61            }
62
63            /// Create a new test case (with input parameters and multi-inputs)
64            /// matching selected generic types.
65            pub fn create_param_multi(&mut self, ipts: Vec<$t>, exp: Vec<$g>, params: Vec<$p>) {
66                self.add(Case::new_params_multi(ipts, params, exp));
67            }
68        }
69    };
70}
71
72/// Use a simplified syntax to create nested Vectors (as per needed by several
73/// LeetCode problems.)
74///
75/// # Examples:
76///
77/// ```rust
78/// use leetcode_rust::vec2d;
79///
80/// let v = vec2d![[1,2,3],[2,3,4]];
81/// assert_eq!(v[0], vec![1,2,3]);
82/// ```
83#[macro_export]
84macro_rules! vec2d {
85    ($([$($x:expr),* $(,)*]),+ $(,)*) => {{
86        vec![$(vec![$($x,)*],)*]
87    }};
88}
89
90#[allow(unused_imports)]
91pub(crate) use new_case;
92
93#[allow(unused_imports)]
94pub(crate) use new_case_param;
95
96#[allow(unused_imports)]
97pub(crate) use vec2d;
98
99#[allow(unused_imports)]
100pub(crate) use codegen_case_create_impl;