From dc03601ac08248eab5defdb0bbee2df3585a21f6 Mon Sep 17 00:00:00 2001 From: IntoTheNight Date: Sun, 9 Jul 2023 16:30:16 +0530 Subject: [PATCH 1/3] add a shrimple macro --- kernel/src/device_tree.rs | 16 ++++++++-------- kernel/src/utils.rs | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/kernel/src/device_tree.rs b/kernel/src/device_tree.rs index cffa06980..51d822e62 100644 --- a/kernel/src/device_tree.rs +++ b/kernel/src/device_tree.rs @@ -53,26 +53,26 @@ impl DeviceTree { } } use crate::utils::TAB; - +use crate::tab; impl fmt::Display for DeviceTree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f)?; for (device_type, devices) in &self.devices { - writeln!(f, "\r{TAB}{}/\r", device_type)?; + writeln!(f, "\r{}{}/\r", tab!(1), device_type)?; for device in devices { - writeln!(f, "{TAB}{TAB}{}/\r", device.name)?; + writeln!(f, "{}{}/\r", tab!(2), device.name)?; for attr in &device.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(3), attr)?; } for child in &device.children { - writeln!(f, "{TAB}{TAB}{TAB}{}\r", child.name)?; + writeln!(f, "{}{}\r", tab!(4), child.name)?; for attr in &child.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(4), attr)?; } for child in &child.children { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{}\r", child.name)?; + writeln!(f, "{}{}\r", tab!(4), child.name)?; for attr in &child.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(5), attr)?; } } } diff --git a/kernel/src/utils.rs b/kernel/src/utils.rs index 405bf7e88..e62fbbc02 100644 --- a/kernel/src/utils.rs +++ b/kernel/src/utils.rs @@ -3,3 +3,14 @@ /// Used when tab `\t` in hardware is not known and we will default to two spaces pub const TAB: &str = " "; + + +// NOTE: this only reduces the code duplication in source code not in generated code! +// Written by Yours Truly: Munir +/// A simple macro to reduce code duplication when we use TAB internally +#[macro_export] +macro_rules! tab { + ($num:expr) => { + TAB.repeat($num) + } +} \ No newline at end of file From 87c3d0bb3ab4a4b2aa2ed98bef046ee0b6c4ddc6 Mon Sep 17 00:00:00 2001 From: IntoTheNight Date: Sun, 9 Jul 2023 16:30:16 +0530 Subject: [PATCH 2/3] add a shrimple macro add a shrimple macro --- kernel/src/device_tree.rs | 16 ++++++++-------- kernel/src/utils.rs | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/kernel/src/device_tree.rs b/kernel/src/device_tree.rs index cffa06980..5946a6433 100644 --- a/kernel/src/device_tree.rs +++ b/kernel/src/device_tree.rs @@ -53,26 +53,26 @@ impl DeviceTree { } } use crate::utils::TAB; - +use crate::tab; impl fmt::Display for DeviceTree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f)?; for (device_type, devices) in &self.devices { - writeln!(f, "\r{TAB}{}/\r", device_type)?; + writeln!(f, "\r{}{}/\r", tab!(1), device_type)?; for device in devices { - writeln!(f, "{TAB}{TAB}{}/\r", device.name)?; + writeln!(f, "{}{}/\r", tab!(2), device.name)?; for attr in &device.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(3), attr)?; } for child in &device.children { - writeln!(f, "{TAB}{TAB}{TAB}{}\r", child.name)?; + writeln!(f, "{}{}\r", tab!(3), child.name)?; for attr in &child.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(4), attr)?; } for child in &child.children { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{}\r", child.name)?; + writeln!(f, "{}{}\r", tab!(4), child.name)?; for attr in &child.attributes { - writeln!(f, "{TAB}{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; + writeln!(f, "{}{}\r", tab!(5), attr)?; } } } diff --git a/kernel/src/utils.rs b/kernel/src/utils.rs index 405bf7e88..e62fbbc02 100644 --- a/kernel/src/utils.rs +++ b/kernel/src/utils.rs @@ -3,3 +3,14 @@ /// Used when tab `\t` in hardware is not known and we will default to two spaces pub const TAB: &str = " "; + + +// NOTE: this only reduces the code duplication in source code not in generated code! +// Written by Yours Truly: Munir +/// A simple macro to reduce code duplication when we use TAB internally +#[macro_export] +macro_rules! tab { + ($num:expr) => { + TAB.repeat($num) + } +} \ No newline at end of file From 4252719a72a6b56d0d103d6b78be67bf3c734ea5 Mon Sep 17 00:00:00 2001 From: IntoTheNight Date: Sun, 9 Jul 2023 17:18:41 +0530 Subject: [PATCH 3/3] another shrimple macro! --- kernel/src/device_tree.rs | 44 +++++++++++++++------------------------ kernel/src/utils.rs | 12 +++++++++++ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/kernel/src/device_tree.rs b/kernel/src/device_tree.rs index 5946a6433..e02bd191a 100644 --- a/kernel/src/device_tree.rs +++ b/kernel/src/device_tree.rs @@ -23,36 +23,26 @@ impl DeviceTree { let mut dt = Self { devices: HashMap::new(), }; - - // Human input devices - { - dt.devices.insert("Mice".to_string(), Vec::new()); - dt.devices.insert("Keyboards".to_string(), Vec::new()); - dt.devices.insert("Controllers".to_string(), Vec::new()); - // Human Input Devices that do not fit into other catagories go in this spot - dt.devices.insert("Generic HIDs".to_string(), Vec::new()); - } - - { - dt.devices.insert("Disk Drives".to_string(), Vec::new()); - dt.devices.insert("CD Drives".to_string(), Vec::new()); - } - - dt.devices.insert("Batteries".to_string(), Vec::new()); - dt.devices.insert("Monitors".to_string(), Vec::new()); - dt.devices.insert("GPUs".to_string(), Vec::new()); - dt.devices.insert("CPUs".to_string(), Vec::new()); - - dt.devices.insert("USB".to_string(), Vec::new()); - dt.devices.insert("Serial Ports".to_string(), Vec::new()); - dt.devices.insert("Cameras".to_string(), Vec::new()); - dt.devices - .insert("Biometric Devices".to_string(), Vec::new()); - + device_tree!(dt, [ + "Mice", + "Keyboards", + "Controllers", + "Generic HIDs", + "Disk Drives", + "CD Drives", + "Batteries", + "Monitors", + "GPUs", + "CPUs", + "USB", + "Serial Ports", + "Cameras", + "Biometric Devices", + ]); dt } } -use crate::utils::TAB; +use crate::{utils::TAB, device_tree}; use crate::tab; impl fmt::Display for DeviceTree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/kernel/src/utils.rs b/kernel/src/utils.rs index e62fbbc02..f733eec84 100644 --- a/kernel/src/utils.rs +++ b/kernel/src/utils.rs @@ -13,4 +13,16 @@ macro_rules! tab { ($num:expr) => { TAB.repeat($num) } +} + +// NOTE: this only reduces the code duplication in source code not in generated code! +// Written by Yours Truly: Munir +/// A simple macro to reduce code duplication when we insert device types into the device tree +#[macro_export] +macro_rules! device_tree { + ($devtree:expr, $dev_type_vec:expr) => { + for each_device_type in $dev_type_vec { + $devtree.devices.insert(each_device_type.to_string(), Vec::new()); + } + }; } \ No newline at end of file