add a shrimple macro

This commit is contained in:
IntoTheNight 2023-07-09 16:30:16 +05:30
parent 8e8289f5ba
commit dc03601ac0
2 changed files with 19 additions and 8 deletions

View file

@ -53,26 +53,26 @@ impl DeviceTree {
} }
} }
use crate::utils::TAB; use crate::utils::TAB;
use crate::tab;
impl fmt::Display for DeviceTree { impl fmt::Display for DeviceTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f)?; writeln!(f)?;
for (device_type, devices) in &self.devices { 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 { for device in devices {
writeln!(f, "{TAB}{TAB}{}/\r", device.name)?; writeln!(f, "{}{}/\r", tab!(2), device.name)?;
for attr in &device.attributes { for attr in &device.attributes {
writeln!(f, "{TAB}{TAB}{TAB}{}\r", attr)?; writeln!(f, "{}{}\r", tab!(3), attr)?;
} }
for child in &device.children { 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 { for attr in &child.attributes {
writeln!(f, "{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; writeln!(f, "{}{}\r", tab!(4), attr)?;
} }
for child in &child.children { 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 { for attr in &child.attributes {
writeln!(f, "{TAB}{TAB}{TAB}{TAB}{TAB}{}\r", attr)?; writeln!(f, "{}{}\r", tab!(5), attr)?;
} }
} }
} }

View file

@ -3,3 +3,14 @@
/// Used when tab `\t` in hardware is not known and we will default to two spaces /// Used when tab `\t` in hardware is not known and we will default to two spaces
pub const TAB: &str = " "; 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)
}
}