1
0
Fork 0
forked from koniifer/ableos

Merge pull request 'master' (#4) from IntoTheNight/ableos:master into master

Reviewed-on: https://git.ablecorp.us/AbleOS/ableos/pulls/4
This commit is contained in:
able 2023-07-09 12:46:49 +00:00
commit a2a2b509bb
2 changed files with 48 additions and 35 deletions

View file

@ -23,56 +23,46 @@ impl DeviceTree {
let mut dt = Self { let mut dt = Self {
devices: HashMap::new(), devices: HashMap::new(),
}; };
device_tree!(dt, [
// Human input devices "Mice",
{ "Keyboards",
dt.devices.insert("Mice".to_string(), Vec::new()); "Controllers",
dt.devices.insert("Keyboards".to_string(), Vec::new()); "Generic HIDs",
dt.devices.insert("Controllers".to_string(), Vec::new()); "Disk Drives",
// Human Input Devices that do not fit into other catagories go in this spot "CD Drives",
dt.devices.insert("Generic HIDs".to_string(), Vec::new()); "Batteries",
} "Monitors",
"GPUs",
{ "CPUs",
dt.devices.insert("Disk Drives".to_string(), Vec::new()); "USB",
dt.devices.insert("CD Drives".to_string(), Vec::new()); "Serial Ports",
} "Cameras",
"Biometric Devices",
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());
dt dt
} }
} }
use crate::utils::TAB; use crate::{utils::TAB, device_tree};
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!(3), 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,26 @@
/// 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)
}
}
// 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());
}
};
}