1
0
Fork 0
forked from koniifer/ableos

CAPS: Changing the printing of caps

This commit is contained in:
able 2023-07-12 06:03:29 -05:00
parent b7da4f17c2
commit 340d76fd13
2 changed files with 55 additions and 42 deletions

View file

@ -1,8 +1,8 @@
//! AbleOS capability tree implementation //! AbleOS capability tree implementation
use { use {
crate::{tab, utils::TAB},
alloc::{ alloc::{
format,
string::{String, ToString}, string::{String, ToString},
vec, vec,
}, },
@ -20,7 +20,7 @@ struct Argument {
struct Function { struct Function {
name: String, name: String,
args: Vec<Argument>, args: Vec<Argument>,
ret: Option<String>, ret: String,
} }
struct Capability { struct Capability {
@ -41,49 +41,57 @@ impl Capability {
impl fmt::Display for Capability { impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Capability: {}\n", self.name)?; write!(f, "Capability: {}\r\n", self.name)?;
for function in &self.functions { for function in &self.functions {
write!(f, " Function: {}\n", function.name)?; write!(f, "{}Function: {}\r\n", tab!(1), function.name)?;
for arg in &function.args { for arg in &function.args {
write!(f, " Argument: {} (Type: {})\n", arg.name, arg.type_)?; write!(
} f,
if let Some(ret_type) = &function.ret { "{}Argument: {} (Type: {})\r\n",
write!(f, " Return Type: {}\n", ret_type)?; tab!(2),
} else { arg.name,
write!(f, " Return Type: None\n")?; arg.type_
)?;
} }
write!(f, "{}Return Type: {}\r\n", tab!(2), &function.ret)?;
} }
for sub_capability in &self.sub_capabilities { for sub_capability in &self.sub_capabilities {
write!(f, "{}", sub_capability.to_string_with_indentation(2))?; write!(f, "{}{}\r\n", tab!(1), sub_capability)?;
} }
Ok(()) Ok(())
} }
} }
impl Capability { // impl Capability {
fn to_string_with_indentation(&self, level: usize) -> String { // fn to_string_with_indentation(&self, level: usize) -> String {
let indent = " ".repeat(level); // let indent = tab!(level);
let mut result = format!("{}Capability: {}\n", indent, self.name); // let mut result = format!("{}Capability: {}\n", indent, self.name);
for function in &self.functions { // for function in &self.functions {
result.push_str(&format!("{} Function: {}\n", indent, function.name)); // result.push_str(&format!(
for arg in &function.args { // "{}Function: {}\n",
result.push_str(&format!( // tab!(indent + 1),
"{} Argument: {} (Type: {})\n", // function.name
indent, arg.name, arg.type_ // ));
)); // for arg in &function.args {
} // result.push_str(&format!(
if let Some(ret_type) = &function.ret { // "{}Argument: {} (Type: {})\n",
result.push_str(&format!("{} Return Type: {}\n", indent, ret_type)); // tab!(indent + 2),
} else { // arg.name,
result.push_str(&format!("{} Return Type: None\n", indent)); // arg.type_
} // ));
} // }
for sub_capability in &self.sub_capabilities { // result.push_str(&format!(
result.push_str(&sub_capability.to_string_with_indentation(level + 1)); // "{}Return Type: {}\n",
} // tab!(indent + 2),
result // &function.ret
} // ));
} // }
// for sub_capability in &self.sub_capabilities {
// result.push_str(&sub_capability.to_string_with_indentation(level + 1));
// }
// result
// }
// }
struct CapabilityTree { struct CapabilityTree {
capabilities: Vec<Capability>, capabilities: Vec<Capability>,
@ -97,7 +105,8 @@ impl CapabilityTree {
} }
} }
fn example() { /// A super simple capabilities example
pub fn example() {
let mut capability_tree = Capability::new("VFS".to_string()); let mut capability_tree = Capability::new("VFS".to_string());
let mut file_management_capability = Capability::new("File Management".to_string()); let mut file_management_capability = Capability::new("File Management".to_string());
@ -113,7 +122,7 @@ fn example() {
type_: "String".to_string(), type_: "String".to_string(),
}, },
], ],
ret: Some("FileHandle".to_string()), ret: "FileHandle".to_string(),
}); });
file_management_capability.functions.push(Function { file_management_capability.functions.push(Function {
name: "ReadFile".to_string(), name: "ReadFile".to_string(),
@ -131,7 +140,7 @@ fn example() {
type_: "usize".to_string(), type_: "usize".to_string(),
}, },
], ],
ret: Some("usize".to_string()), ret: "usize".to_string(),
}); });
file_management_capability.functions.push(Function { file_management_capability.functions.push(Function {
name: "WriteFile".to_string(), name: "WriteFile".to_string(),
@ -145,7 +154,7 @@ fn example() {
type_: "&[u8]".to_string(), type_: "&[u8]".to_string(),
}, },
], ],
ret: None, ret: "None".to_string(),
}); });
let mut directory_management_capability = Capability::new("Directory Management".to_string()); let mut directory_management_capability = Capability::new("Directory Management".to_string());
@ -155,7 +164,7 @@ fn example() {
name: "path".to_string(), name: "path".to_string(),
type_: "String".to_string(), type_: "String".to_string(),
}], }],
ret: Some("bool".to_string()), ret: "bool".to_string(),
}); });
directory_management_capability.functions.push(Function { directory_management_capability.functions.push(Function {
name: "ListDirectory".to_string(), name: "ListDirectory".to_string(),
@ -163,7 +172,7 @@ fn example() {
name: "path".to_string(), name: "path".to_string(),
type_: "String".to_string(), type_: "String".to_string(),
}], }],
ret: Some("Vec<String>".to_string()), ret: "Vec<String>".to_string(),
}); });
capability_tree capability_tree
@ -173,5 +182,5 @@ fn example() {
.sub_capabilities .sub_capabilities
.push(directory_management_capability); .push(directory_management_capability);
log::debug!("{}", capability_tree); log::debug!("CapTree\r\n{}", capability_tree);
} }

View file

@ -1,5 +1,7 @@
//! AbleOS Kernel Entrypoint //! AbleOS Kernel Entrypoint
use crate::capabilities;
// use crate::arch::sloop; // use crate::arch::sloop;
use { use {
crate::{ crate::{
@ -37,6 +39,8 @@ pub fn kmain(cmdline: &str, boot_modules: BootModules) -> ! {
// TODO: schedule the filesystem driver from the initramfs // TODO: schedule the filesystem driver from the initramfs
// TODO: schedule the init system from the initramfs // TODO: schedule the init system from the initramfs
capabilities::example();
// TODO: change this to a driver // TODO: change this to a driver
{ {
let mut prog = alloc::vec![]; let mut prog = alloc::vec![];