From b7b69883ce3d90311dcd275c62958261f78616dd Mon Sep 17 00:00:00 2001 From: Guilhem Date: Wed, 15 Jun 2022 19:24:10 +0200 Subject: [PATCH] refactor: Reuse macro between types --- src/lib.rs | 320 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 225 insertions(+), 95 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index add1d85..81494c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,59 +194,12 @@ mod test { }; } - to_iso!(to_iso_11, QuadrantKind::TopLeft, 1, true, "11"); - to_iso!(to_iso_18, QuadrantKind::TopLeft, 8, true, "18"); - to_iso!(to_iso_21, QuadrantKind::TopRight, 1, true, "21"); - to_iso!(to_iso_28, QuadrantKind::TopRight, 8, true, "28"); - to_iso!(to_iso_31, QuadrantKind::BottomRight, 1, true, "31"); - to_iso!(to_iso_38, QuadrantKind::BottomRight, 8, true, "38"); - to_iso!(to_iso_41, QuadrantKind::BottomLeft, 1, true, "41"); - to_iso!(to_iso_48, QuadrantKind::BottomLeft, 8, true, "48"); - to_iso!(to_iso_51, QuadrantKind::TopLeft, 1, false, "51"); - to_iso!(to_iso_55, QuadrantKind::TopLeft, 5, false, "55"); - to_iso!(to_iso_61, QuadrantKind::TopRight, 1, false, "61"); - to_iso!(to_iso_65, QuadrantKind::TopRight, 5, false, "65"); - to_iso!(to_iso_71, QuadrantKind::BottomRight, 1, false, "71"); - to_iso!(to_iso_75, QuadrantKind::BottomRight, 5, false, "75"); - to_iso!(to_iso_81, QuadrantKind::BottomLeft, 1, false, "81"); - to_iso!(to_iso_85, QuadrantKind::BottomLeft, 5, false, "85"); +#[cfg(test)] +mod test { + use super::*; - from_iso!(from_iso_11, "11", QuadrantKind::TopLeft, 1, true); - from_iso!(from_iso_18, "18", QuadrantKind::TopLeft, 8, true); - from_iso!(from_iso_21, "21", QuadrantKind::TopRight, 1, true); - from_iso!(from_iso_28, "28", QuadrantKind::TopRight, 8, true); - from_iso!(from_iso_31, "31", QuadrantKind::BottomRight, 1, true); - from_iso!(from_iso_38, "38", QuadrantKind::BottomRight, 8, true); - from_iso!(from_iso_41, "41", QuadrantKind::BottomLeft, 1, true); - from_iso!(from_iso_48, "48", QuadrantKind::BottomLeft, 8, true); - from_iso!(from_iso_51, "51", QuadrantKind::TopLeft, 1, false); - from_iso!(from_iso_55, "55", QuadrantKind::TopLeft, 5, false); - from_iso!(from_iso_61, "61", QuadrantKind::TopRight, 1, false); - from_iso!(from_iso_65, "65", QuadrantKind::TopRight, 5, false); - from_iso!(from_iso_71, "71", QuadrantKind::BottomRight, 1, false); - from_iso!(from_iso_75, "75", QuadrantKind::BottomRight, 5, false); - from_iso!(from_iso_81, "81", QuadrantKind::BottomLeft, 1, false); - from_iso!(from_iso_85, "85", QuadrantKind::BottomLeft, 5, false); - - from_iso_fail!(from_iso_fail_10, "10"); - from_iso_fail!(from_iso_fail_19, "19"); - from_iso_fail!(from_iso_fail_20, "20"); - from_iso_fail!(from_iso_fail_29, "29"); - from_iso_fail!(from_iso_fail_30, "30"); - from_iso_fail!(from_iso_fail_39, "39"); - from_iso_fail!(from_iso_fail_40, "40"); - from_iso_fail!(from_iso_fail_49, "49"); - from_iso_fail!(from_iso_fail_50, "50"); - from_iso_fail!(from_iso_fail_56, "56"); - from_iso_fail!(from_iso_fail_60, "60"); - from_iso_fail!(from_iso_fail_66, "66"); - from_iso_fail!(from_iso_fail_70, "70"); - from_iso_fail!(from_iso_fail_76, "76"); - from_iso_fail!(from_iso_fail_80, "80"); - from_iso_fail!(from_iso_fail_86, "86"); - - macro_rules! to_uns { - ($name:ident, $quadrant:expr,$number:expr, $permanent:expr, $uns:expr) => { + macro_rules! to { + ($name:ident, $func:ident, $quadrant:expr,$number:expr, $permanent:expr, $value:expr) => { #[test] fn $name() { let teeth = Teeth { @@ -254,16 +207,16 @@ mod test { number: $number, permanent: $permanent, }; - assert_eq!(teeth.to_uns(), $uns); + assert_eq!(teeth.$func(), $value); } }; } - macro_rules! from_uns { - ($name:ident, $uns:expr, $quadrant:expr,$number:expr, $permanent:expr) => { + macro_rules! from { + ($name:ident, $func:ident, $value:expr, $quadrant:expr,$number:expr, $permanent:expr) => { #[test] fn $name() { - let teeth = Teeth::from_uns($uns); + let teeth = Teeth::$func($value); assert_eq!(teeth.quadrant, $quadrant); assert_eq!(teeth.number, $number); assert_eq!(teeth.permanent, $permanent); @@ -271,52 +224,229 @@ mod test { }; } - macro_rules! from_uns_fail { - ($name:ident, $uns:expr) => { + macro_rules! from_fail { + ($name:ident, $func:ident, $value:expr) => { #[test] #[should_panic] fn $name() { - Teeth::from_uns($uns); + Teeth::$func($value); } }; } - to_uns!(to_uns_1, QuadrantKind::TopLeft, 1, true, "1"); - to_uns!(to_uns_8, QuadrantKind::TopLeft, 8, true, "8"); - to_uns!(to_uns_9, QuadrantKind::TopRight, 1, true, "9"); - to_uns!(to_uns_16, QuadrantKind::TopRight, 8, true, "16"); - to_uns!(to_uns_17, QuadrantKind::BottomRight, 1, true, "17"); - to_uns!(to_uns_24, QuadrantKind::BottomRight, 8, true, "24"); - to_uns!(to_uns_25, QuadrantKind::BottomLeft, 1, true, "25"); - to_uns!(to_uns_32, QuadrantKind::BottomLeft, 8, true, "32"); - to_uns!(to_uns_a, QuadrantKind::TopLeft, 1, false, "A"); - to_uns!(to_uns_e, QuadrantKind::TopLeft, 5, false, "E"); - to_uns!(to_uns_f, QuadrantKind::TopRight, 1, false, "F"); - to_uns!(to_uns_j, QuadrantKind::TopRight, 5, false, "J"); - to_uns!(to_uns_k, QuadrantKind::BottomRight, 1, false, "K"); - to_uns!(to_uns_o, QuadrantKind::BottomRight, 5, false, "O"); - to_uns!(to_uns_p, QuadrantKind::BottomLeft, 1, false, "P"); - to_uns!(to_uns_t, QuadrantKind::BottomLeft, 5, false, "T"); + to!(to_iso_11, to_iso, QuadrantKind::TopLeft, 1, true, "11"); + to!(to_iso_18, to_iso, QuadrantKind::TopLeft, 8, true, "18"); + to!(to_iso_21, to_iso, QuadrantKind::TopRight, 1, true, "21"); + to!(to_iso_28, to_iso, QuadrantKind::TopRight, 8, true, "28"); + to!(to_iso_31, to_iso, QuadrantKind::BottomRight, 1, true, "31"); + to!(to_iso_38, to_iso, QuadrantKind::BottomRight, 8, true, "38"); + to!(to_iso_41, to_iso, QuadrantKind::BottomLeft, 1, true, "41"); + to!(to_iso_48, to_iso, QuadrantKind::BottomLeft, 8, true, "48"); + to!(to_iso_51, to_iso, QuadrantKind::TopLeft, 1, false, "51"); + to!(to_iso_55, to_iso, QuadrantKind::TopLeft, 5, false, "55"); + to!(to_iso_61, to_iso, QuadrantKind::TopRight, 1, false, "61"); + to!(to_iso_65, to_iso, QuadrantKind::TopRight, 5, false, "65"); + to!(to_iso_71, to_iso, QuadrantKind::BottomRight, 1, false, "71"); + to!(to_iso_75, to_iso, QuadrantKind::BottomRight, 5, false, "75"); + to!(to_iso_81, to_iso, QuadrantKind::BottomLeft, 1, false, "81"); + to!(to_iso_85, to_iso, QuadrantKind::BottomLeft, 5, false, "85"); - from_uns!(from_uns_8, "8", QuadrantKind::TopLeft, 1, true); - from_uns!(from_uns_1, "1", QuadrantKind::TopLeft, 8, true); - from_uns!(from_uns_9, "9", QuadrantKind::TopRight, 1, true); - from_uns!(from_uns_16, "16", QuadrantKind::TopRight, 8, true); - from_uns!(from_uns_24, "24", QuadrantKind::BottomRight, 1, true); - from_uns!(from_uns_17, "17", QuadrantKind::BottomRight, 8, true); - from_uns!(from_uns_25, "25", QuadrantKind::BottomLeft, 1, true); - from_uns!(from_uns_32, "32", QuadrantKind::BottomLeft, 8, true); - from_uns!(from_uns_e, "E", QuadrantKind::TopLeft, 1, false); - from_uns!(from_uns_a, "A", QuadrantKind::TopLeft, 5, false); - from_uns!(from_uns_f, "F", QuadrantKind::TopRight, 1, false); - from_uns!(from_uns_j, "J", QuadrantKind::TopRight, 5, false); - from_uns!(from_uns_o, "O", QuadrantKind::BottomRight, 1, false); - from_uns!(from_uns_k, "K", QuadrantKind::BottomRight, 5, false); - from_uns!(from_uns_p, "P", QuadrantKind::BottomLeft, 1, false); - from_uns!(from_uns_t, "T", QuadrantKind::BottomLeft, 5, false); + from!(from_iso_11, from_iso, "11", QuadrantKind::TopLeft, 1, true); + from!(from_iso_18, from_iso, "18", QuadrantKind::TopLeft, 8, true); + from!(from_iso_21, from_iso, "21", QuadrantKind::TopRight, 1, true); + from!(from_iso_28, from_iso, "28", QuadrantKind::TopRight, 8, true); + from!( + from_iso_31, + from_iso, + "31", + QuadrantKind::BottomRight, + 1, + true + ); + from!( + from_iso_38, + from_iso, + "38", + QuadrantKind::BottomRight, + 8, + true + ); + from!( + from_iso_41, + from_iso, + "41", + QuadrantKind::BottomLeft, + 1, + true + ); + from!( + from_iso_48, + from_iso, + "48", + QuadrantKind::BottomLeft, + 8, + true + ); + from!(from_iso_51, from_iso, "51", QuadrantKind::TopLeft, 1, false); + from!(from_iso_55, from_iso, "55", QuadrantKind::TopLeft, 5, false); + from!( + from_iso_61, + from_iso, + "61", + QuadrantKind::TopRight, + 1, + false + ); + from!( + from_iso_65, + from_iso, + "65", + QuadrantKind::TopRight, + 5, + false + ); + from!( + from_iso_71, + from_iso, + "71", + QuadrantKind::BottomRight, + 1, + false + ); + from!( + from_iso_75, + from_iso, + "75", + QuadrantKind::BottomRight, + 5, + false + ); + from!( + from_iso_81, + from_iso, + "81", + QuadrantKind::BottomLeft, + 1, + false + ); + from!( + from_iso_85, + from_iso, + "85", + QuadrantKind::BottomLeft, + 5, + false + ); - from_uns_fail!(from_uns_fail_0, "0"); - from_uns_fail!(from_uns_fail_33, "33"); - from_uns_fail!(from_uns_fail_at, "@"); - from_uns_fail!(from_uns_fail_u, "U"); + from_fail!(from_iso_fail_10, from_iso, "10"); + from_fail!(from_iso_fail_19, from_iso, "19"); + from_fail!(from_iso_fail_20, from_iso, "20"); + from_fail!(from_iso_fail_29, from_iso, "29"); + from_fail!(from_iso_fail_30, from_iso, "30"); + from_fail!(from_iso_fail_39, from_iso, "39"); + from_fail!(from_iso_fail_40, from_iso, "40"); + from_fail!(from_iso_fail_49, from_iso, "49"); + from_fail!(from_iso_fail_50, from_iso, "50"); + from_fail!(from_iso_fail_56, from_iso, "56"); + from_fail!(from_iso_fail_60, from_iso, "60"); + from_fail!(from_iso_fail_66, from_iso, "66"); + from_fail!(from_iso_fail_70, from_iso, "70"); + from_fail!(from_iso_fail_76, from_iso, "76"); + from_fail!(from_iso_fail_80, from_iso, "80"); + from_fail!(from_iso_fail_86, from_iso, "86"); + + to!(to_uns_1, to_uns, QuadrantKind::TopLeft, 1, true, "1"); + to!(to_uns_8, to_uns, QuadrantKind::TopLeft, 8, true, "8"); + to!(to_uns_9, to_uns, QuadrantKind::TopRight, 1, true, "9"); + to!(to_uns_16, to_uns, QuadrantKind::TopRight, 8, true, "16"); + to!(to_uns_17, to_uns, QuadrantKind::BottomRight, 1, true, "17"); + to!(to_uns_24, to_uns, QuadrantKind::BottomRight, 8, true, "24"); + to!(to_uns_25, to_uns, QuadrantKind::BottomLeft, 1, true, "25"); + to!(to_uns_32, to_uns, QuadrantKind::BottomLeft, 8, true, "32"); + to!(to_uns_a, to_uns, QuadrantKind::TopLeft, 1, false, "A"); + to!(to_uns_e, to_uns, QuadrantKind::TopLeft, 5, false, "E"); + to!(to_uns_f, to_uns, QuadrantKind::TopRight, 1, false, "F"); + to!(to_uns_j, to_uns, QuadrantKind::TopRight, 5, false, "J"); + to!(to_uns_k, to_uns, QuadrantKind::BottomRight, 1, false, "K"); + to!(to_uns_o, to_uns, QuadrantKind::BottomRight, 5, false, "O"); + to!(to_uns_p, to_uns, QuadrantKind::BottomLeft, 1, false, "P"); + to!(to_uns_t, to_uns, QuadrantKind::BottomLeft, 5, false, "T"); + + from!(from_uns_8, from_uns, "8", QuadrantKind::TopLeft, 1, true); + from!(from_uns_1, from_uns, "1", QuadrantKind::TopLeft, 8, true); + from!(from_uns_9, from_uns, "9", QuadrantKind::TopRight, 1, true); + from!(from_uns_16, from_uns, "16", QuadrantKind::TopRight, 8, true); + from!( + from_uns_24, + from_uns, + "24", + QuadrantKind::BottomRight, + 1, + true + ); + from!( + from_uns_17, + from_uns, + "17", + QuadrantKind::BottomRight, + 8, + true + ); + from!( + from_uns_25, + from_uns, + "25", + QuadrantKind::BottomLeft, + 1, + true + ); + from!( + from_uns_32, + from_uns, + "32", + QuadrantKind::BottomLeft, + 8, + true + ); + from!(from_uns_e, from_uns, "E", QuadrantKind::TopLeft, 1, false); + from!(from_uns_a, from_uns, "A", QuadrantKind::TopLeft, 5, false); + from!(from_uns_f, from_uns, "F", QuadrantKind::TopRight, 1, false); + from!(from_uns_j, from_uns, "J", QuadrantKind::TopRight, 5, false); + from!( + from_uns_o, + from_uns, + "O", + QuadrantKind::BottomRight, + 1, + false + ); + from!( + from_uns_k, + from_uns, + "K", + QuadrantKind::BottomRight, + 5, + false + ); + from!( + from_uns_p, + from_uns, + "P", + QuadrantKind::BottomLeft, + 1, + false + ); + from!( + from_uns_t, + from_uns, + "T", + QuadrantKind::BottomLeft, + 5, + false + ); + + from_fail!(from_uns_fail_0, from_uns, "0"); + from_fail!(from_uns_fail_33, from_uns, "33"); + from_fail!(from_uns_fail_at, from_uns, "@"); + from_fail!(from_uns_fail_u, from_uns, "U"); }