Port XML to no_std

pull/1/head
Able 2023-04-05 10:04:57 -05:00
parent b2274fe422
commit 5210aaef62
11 changed files with 190 additions and 17 deletions

28
Cargo.lock generated
View File

@ -37,6 +37,21 @@ version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
[[package]]
name = "ari_client"
version = "0.1.0"
[[package]]
name = "ari_interface"
version = "0.1.0"
dependencies = [
"log",
]
[[package]]
name = "ari_server"
version = "0.1.0"
[[package]] [[package]]
name = "audio_interface" name = "audio_interface"
version = "0.1.0" version = "0.1.0"
@ -60,6 +75,15 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -478,6 +502,10 @@ dependencies = [
[[package]] [[package]]
name = "xml" name = "xml"
version = "0.1.0" version = "0.1.0"
dependencies = [
"bincode",
"serde",
]
[[package]] [[package]]
name = "xml_tests" name = "xml_tests"

View File

@ -15,6 +15,7 @@ members = [
"drivers/mice/ps2_mouse", "drivers/mice/ps2_mouse",
"libraries/able_graphics_library", "libraries/able_graphics_library",
"libraries/ari_interface",
"libraries/audio_interface", "libraries/audio_interface",
"libraries/clparse", "libraries/clparse",
"libraries/cryptography", "libraries/cryptography",
@ -29,6 +30,8 @@ members = [
"libraries/trash_manifest", "libraries/trash_manifest",
"libraries/versioning", "libraries/versioning",
"programs/ari_client",
"programs/ari_server",
"programs/delete", "programs/delete",
"programs/list", "programs/list",
"programs/shell", "programs/shell",

View File

@ -0,0 +1,9 @@
[package]
name = "ari_interface"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "*"

View File

@ -0,0 +1,79 @@
use log::trace;
// ARI is a set of programs to manage an ableOS remote install.
// In the case of this being enabled ableOS will require a dedicated Server with an ableOS install and a number of Client devices
// A Client will fetch programs and files from the Server and cache them locally for a configurable amount of time
// TODO: Expand this explination
/// The size of blocks sent over the network by ARI
pub const ARIFILE_BLOCK_SIZE: usize = 4096/*bytes*/;
#[derive(Debug, Hash, PartialEq)]
pub struct ARIFileBlock([u8; ARIFILE_BLOCK_SIZE]);
#[derive(Debug, Hash, PartialEq)]
pub struct ARIFile {
inner_data: Vec<ARIFileBlock>,
}
impl ARIFile {
pub fn new() -> Self {
Self { inner_data: vec![] }
}
pub fn file_from_vec(vec: Vec<u8>) -> Self {
let mut data = vec![];
let mut data_chunk = vec![];
for x in vec.into_iter() {
if data_chunk.len() == ARIFILE_BLOCK_SIZE {
let data_block: [u8; ARIFILE_BLOCK_SIZE] = data_chunk[0..ARIFILE_BLOCK_SIZE]
.try_into()
.expect("slice with incorrect length");
data.push(ARIFileBlock(data_block));
data_chunk = vec![];
}
data_chunk.push(x);
}
if !data_chunk.is_empty() {
let missing_byte_count = ARIFILE_BLOCK_SIZE - data_chunk.len();
let msg: String = format!("Length Not Aligned missing {} bytes", missing_byte_count);
trace!("{}", msg);
for _ in data_chunk.len()..ARIFILE_BLOCK_SIZE {
data_chunk.push(0)
}
data.push(ARIFileBlock(
data_chunk[0..ARIFILE_BLOCK_SIZE]
.try_into()
.expect("slice with incorrect length"),
))
}
Self { inner_data: data }
}
}
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
#[test]
pub fn file_diffrence_hash() {
let file_1 = ARIFile::file_from_vec(vec![1]);
let hash_1 = calculate_hash(&file_1.inner_data[0]);
let file_2 = ARIFile::file_from_vec(vec![1, 1, 1, 1, 1, 1, 1, 1]);
let hash_2 = calculate_hash(&file_2.inner_data[0]);
assert_ne!(hash_1, hash_2);
}
#[test]
pub fn hash_test() {
let file = ARIFile::file_from_vec(vec![1]);
assert_eq!(calculate_hash(&file.inner_data[0]), 174020520262829630);
}

View File

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bincode = "1.3.3"
serde = { version = "1.0", features = ["derive"] }

View File

@ -1,6 +1,16 @@
#![no_std]
use core::fmt::{Debug, Display}; use core::fmt::{Debug, Display};
#[derive(PartialEq, Clone)] #[macro_use]
extern crate alloc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Attribute { pub struct Attribute {
name: String, name: String,
value: String, value: String,
@ -11,7 +21,8 @@ impl Attribute {
format!("{:?}={:?}", self.name, self.value) format!("{:?}={:?}", self.name, self.value)
} }
} }
#[derive(Clone)] #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
// TODO Add an autotype attribute that defaults to true // TODO Add an autotype attribute that defaults to true
pub struct XMLElement { pub struct XMLElement {
pub name: String, pub name: String,
@ -19,6 +30,19 @@ pub struct XMLElement {
pub attributes: Vec<Attribute>, pub attributes: Vec<Attribute>,
pub children: Vec<XMLElement>, pub children: Vec<XMLElement>,
} }
impl XMLElement {
pub fn to_bin(&self) -> Result<Vec<u8>, XMLError> {
match bincode::serialize(&self) {
Ok(bin) => return Ok(bin),
Err(err) => return Err(XMLError::EncodingError(err)),
}
}
pub fn from_bin(bin: Vec<u8>) -> Self {
todo!()
}
}
impl XMLElement { impl XMLElement {
pub fn new<T: Display + Debug>(name: T) -> Self { pub fn new<T: Display + Debug>(name: T) -> Self {
Self { Self {
@ -158,10 +182,10 @@ impl From<u64> for UInt {
} }
} }
#[test] // #[test]
pub fn test_string_to_num() { // pub fn test_string_to_num() {
println!("{:?}", string_to_num("-256.0")); // string_to_num("-256.0")
} // }
pub fn string_to_num<T: Debug + Display>(string: T) -> Result<Types, XMLError> { pub fn string_to_num<T: Debug + Display>(string: T) -> Result<Types, XMLError> {
// let string = "-27".to_string(); // `parse()` works with `&str` and `String`! // let string = "-27".to_string(); // `parse()` works with `&str` and `String`!
@ -222,30 +246,34 @@ pub fn string_to_num<T: Debug + Display>(string: T) -> Result<Types, XMLError> {
} }
// TODO: Decimal handler // TODO: Decimal handler
if false { {
let mut split_string = string.split("."); let mut split_string = string.split(".");
let positive; let positive;
let mut lhs = split_string.next().unwrap(); let mut lhs = split_string.next().unwrap();
println!("{:?}", lhs.chars().next()); // println!("{:?}", lhs.chars().next());
let mut lhs_median: Vec<char> = lhs.chars().collect();
if lhs.chars().next() == Some('-') { if lhs.chars().next() == Some('-') {
// lhs // lhs
positive = false; positive = false;
let mut lhs_median: Vec<char> = lhs.chars().collect();
lhs_median.remove(0); lhs_median.remove(0);
println!("A {:?}", lhs_median); // println!("A {:?}", lhs_median);
} else { } else {
positive = true; positive = true;
} }
println!("{:?}", lhs); // println!("{:?}", lhs);
let lhs_median_2 = lhs_median.iter().cloned().collect::<String>();
let mut lhs_final: Types = Types::None; let mut lhs_final: Types = Types::None;
let abc = string_to_num(lhs); let abc = string_to_num(lhs_median_2);
println!("{:?}", lhs_final); // println!("{:?}", lhs_final);
let rhs = split_string.next(); let rhs = split_string.next().unwrap();
println!("{:?}", rhs); // println!("{:?}", rhs);
let xyz = string_to_num(rhs);
// println!("{:?}", abc);
// println!("{:?}", xyz);
// return Types::Fixed(); // return Types::Fixed();
} }
@ -255,4 +283,6 @@ pub fn string_to_num<T: Debug + Display>(string: T) -> Result<Types, XMLError> {
#[derive(Debug)] #[derive(Debug)]
pub enum XMLError { pub enum XMLError {
TypeNotANumber, TypeNotANumber,
EncodingError(bincode::Error),
DecodingError(bincode::Error),
} }

View File

@ -0,0 +1,8 @@
[package]
name = "ari_client"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -0,0 +1,8 @@
[package]
name = "ari_server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -2,6 +2,6 @@ use xml;
fn main() { fn main() {
let root = xml::XMLElement::new("name"); let root = xml::XMLElement::new("name");
println!("{:?}", root.to_bin().unwrap());
println!("{}", root.to_string()) println!("{}", root.to_string());
} }