1
0
Fork 0
forked from AbleOS/ableos
ableos/kernel/src/capabilities.rs

187 lines
5.1 KiB
Rust
Raw Normal View History

2023-07-10 22:54:05 -05:00
//! AbleOS capability tree implementation
use {
2023-07-12 06:03:29 -05:00
crate::{tab, utils::TAB},
2023-07-10 22:54:05 -05:00
alloc::{
string::{String, ToString},
vec,
},
core::fmt,
};
// Seperate
use alloc::vec::Vec;
struct Argument {
name: String,
type_: String,
}
struct Function {
name: String,
args: Vec<Argument>,
2023-07-12 06:03:29 -05:00
ret: String,
2023-07-10 22:54:05 -05:00
}
struct Capability {
name: String,
functions: Vec<Function>,
sub_capabilities: Vec<Capability>,
}
impl Capability {
fn new(name: String) -> Capability {
Capability {
name,
functions: Vec::new(),
sub_capabilities: Vec::new(),
}
}
}
impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-07-12 06:03:29 -05:00
write!(f, "Capability: {}\r\n", self.name)?;
2023-07-10 22:54:05 -05:00
for function in &self.functions {
2023-07-12 06:03:29 -05:00
write!(f, "{}Function: {}\r\n", tab!(1), function.name)?;
2023-07-10 22:54:05 -05:00
for arg in &function.args {
2023-07-12 06:03:29 -05:00
write!(
f,
"{}Argument: {} (Type: {})\r\n",
tab!(2),
arg.name,
arg.type_
)?;
2023-07-10 22:54:05 -05:00
}
2023-07-12 06:03:29 -05:00
write!(f, "{}Return Type: {}\r\n", tab!(2), &function.ret)?;
2023-07-10 22:54:05 -05:00
}
for sub_capability in &self.sub_capabilities {
2023-07-12 06:03:29 -05:00
write!(f, "{}{}\r\n", tab!(1), sub_capability)?;
2023-07-10 22:54:05 -05:00
}
Ok(())
}
}
2023-07-12 06:03:29 -05:00
// impl Capability {
// fn to_string_with_indentation(&self, level: usize) -> String {
// let indent = tab!(level);
// let mut result = format!("{}Capability: {}\n", indent, self.name);
// for function in &self.functions {
// result.push_str(&format!(
// "{}Function: {}\n",
// tab!(indent + 1),
// function.name
// ));
// for arg in &function.args {
// result.push_str(&format!(
// "{}Argument: {} (Type: {})\n",
// tab!(indent + 2),
// arg.name,
// arg.type_
// ));
// }
// result.push_str(&format!(
// "{}Return Type: {}\n",
// tab!(indent + 2),
// &function.ret
// ));
// }
// for sub_capability in &self.sub_capabilities {
// result.push_str(&sub_capability.to_string_with_indentation(level + 1));
// }
// result
// }
// }
2023-07-10 22:54:05 -05:00
struct CapabilityTree {
capabilities: Vec<Capability>,
}
impl CapabilityTree {
fn new() -> CapabilityTree {
CapabilityTree {
capabilities: Vec::new(),
}
}
}
2023-07-12 06:03:29 -05:00
/// A super simple capabilities example
pub fn example() {
2023-07-10 22:54:05 -05:00
let mut capability_tree = Capability::new("VFS".to_string());
let mut file_management_capability = Capability::new("File Management".to_string());
file_management_capability.functions.push(Function {
name: "OpenFile".to_string(),
args: vec![
Argument {
name: "path".to_string(),
type_: "String".to_string(),
},
Argument {
name: "mode".to_string(),
type_: "String".to_string(),
},
],
2023-07-12 06:03:29 -05:00
ret: "FileHandle".to_string(),
2023-07-10 22:54:05 -05:00
});
file_management_capability.functions.push(Function {
name: "ReadFile".to_string(),
args: vec![
Argument {
name: "file".to_string(),
type_: "FileHandle".to_string(),
},
Argument {
name: "buffer".to_string(),
type_: "&mut [u8]".to_string(),
},
Argument {
name: "length".to_string(),
type_: "usize".to_string(),
},
],
2023-07-12 06:03:29 -05:00
ret: "usize".to_string(),
2023-07-10 22:54:05 -05:00
});
file_management_capability.functions.push(Function {
name: "WriteFile".to_string(),
args: vec![
Argument {
name: "file".to_string(),
type_: "FileHandle".to_string(),
},
Argument {
name: "buffer".to_string(),
type_: "&[u8]".to_string(),
},
],
2023-07-12 06:03:29 -05:00
ret: "None".to_string(),
2023-07-10 22:54:05 -05:00
});
let mut directory_management_capability = Capability::new("Directory Management".to_string());
directory_management_capability.functions.push(Function {
name: "CreateDirectory".to_string(),
args: vec![Argument {
name: "path".to_string(),
type_: "String".to_string(),
}],
2023-07-12 06:03:29 -05:00
ret: "bool".to_string(),
2023-07-10 22:54:05 -05:00
});
directory_management_capability.functions.push(Function {
name: "ListDirectory".to_string(),
args: vec![Argument {
name: "path".to_string(),
type_: "String".to_string(),
}],
2023-07-12 06:03:29 -05:00
ret: "Vec<String>".to_string(),
2023-07-10 22:54:05 -05:00
});
capability_tree
.sub_capabilities
.push(file_management_capability);
capability_tree
.sub_capabilities
.push(directory_management_capability);
2023-07-12 06:03:29 -05:00
log::debug!("CapTree\r\n{}", capability_tree);
2023-07-10 22:54:05 -05:00
}