Remove EXI in favor of XML

This commit is contained in:
Able 2023-04-01 07:52:15 -05:00
parent 0971ef1260
commit 9101a1ad2f
9 changed files with 241 additions and 238 deletions

19
Cargo.lock generated
View file

@ -107,10 +107,6 @@ dependencies = [
name = "derelict_microarchitecture" name = "derelict_microarchitecture"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "exi"
version = "0.1.0"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -330,12 +326,6 @@ dependencies = [
"versioning", "versioning",
] ]
[[package]]
name = "simple_xml_serialize"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f8896c3696f051907ae5e59f0c2d9fb9f7521c29d636e4a1ea4ff0e4e5aac15"
[[package]] [[package]]
name = "std" name = "std"
version = "0.1.0" version = "0.1.0"
@ -486,15 +476,12 @@ dependencies = [
] ]
[[package]] [[package]]
name = "xml-builder" name = "xml"
version = "0.5.2" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efc4f1a86af7800dfc4056c7833648ea4515ae21502060b5c98114d828f5333b"
[[package]] [[package]]
name = "xml_tests" name = "xml_tests"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"simple_xml_serialize", "xml",
"xml-builder",
] ]

View file

@ -18,7 +18,7 @@ members = [
"libraries/audio_interface", "libraries/audio_interface",
"libraries/clparse", "libraries/clparse",
"libraries/cryptography", "libraries/cryptography",
"libraries/exi", "libraries/xml",
"libraries/locale-maxima", "libraries/locale-maxima",
"libraries/messaging", "libraries/messaging",
"libraries/process", "libraries/process",

View file

@ -1,87 +0,0 @@
use types::NBitUnsignedInteger;
pub mod types;
pub const EXI_HEADER_START: &str = "$EXI";
pub struct ExiOptions {
// NOTE: Go back to the EXI spec and fix this
pub alignment: u8,
pub compression: bool,
pub strict: bool,
pub fragment: bool,
// https://www.w3.org/TR/exi/#key-preserveOption
pub preserve: bool,
// https://www.w3.org/TR/exi/#key-selfContained
pub self_contained: bool,
// https://www.w3.org/TR/exi/#key-schemaIdOption
pub schema_id: String,
// https://www.w3.org/TR/exi/#key-datatypeRepresentationOption
// NOTE: What possible usecase could you have for changing the representation of data types.
// Touching this will be UB. Cry about it.
pub datatype_representation_map: bool,
// https://www.w3.org/TR/exi/#key-blockSizeOption
pub block_size: NBitUnsignedInteger,
// https://www.w3.org/TR/exi/#key-valueMaxLengthOption
pub value_max_length: NBitUnsignedInteger,
// https://www.w3.org/TR/exi/#key-valuePartitionCapacityOption
pub value_partition_capacity: NBitUnsignedInteger,
// https://www.w3.org/TR/exi/#key-userMetaData
pub user_defined_meta_data: String,
}
pub enum ExiFormatVersion {
PreviewVersion1 = 1_0000,
// The ableOS standard
FinalVersion1 = 0_0000,
FinalVersion15 = 0_1110,
FinalVersion16 = 0_1111_0000,
FinalVersion17 = 0_1111_0001,
}
pub struct ExiHeader {
exi_format: ExiFormatVersion,
exi_options: ExiOptions,
}
pub struct ExiDocument {}
impl ExiDocument {
pub fn new_from_xml(xml_string: String) -> Self {
println!("{}", xml_string);
Self {}
}
}
#[cfg(unix)]
mod unix {
use std::{
fs::{self, File},
io::Read,
path::Path,
};
use crate::ExiDocument;
fn parse_exi_from_file(path: &Path) -> ExiDocument {
let file_contents = fs::read_to_string(path).unwrap();
ExiDocument::new_from_xml(file_contents)
// ExiDocument {}
}
#[test]
fn test_xml_to_exi() {
parse_exi_from_file(Path::new(
"/home/able/Projects/ableos_userland/libraries/exi/assets/sample.xml",
));
}
}

View file

@ -1,107 +0,0 @@
pub struct List {
pub length: usize,
pub values: Vec<EXITypes>,
}
// https://www.w3.org/TR/exi/#encodingUnsignedInteger
pub struct NBitUnsignedInteger {
pub length: Option<Integer>,
pub bits: Vec<bool>,
}
impl NBitUnsignedInteger {
pub fn new(length: Integer) -> Self {
let c = if length.bits == vec![0] {
None
} else {
Some(length)
};
Self {
length: c,
bits: Vec::new(),
}
}
}
pub struct BinaryBlob {
pub length: Integer,
pub bytes: Vec<u8>,
}
pub struct Decimal {
pub lhs: UnsignedInteger,
pub rhs: UnsignedInteger,
}
pub enum EXITypes {
Binary(BinaryBlob),
Boolean(bool),
Decimal(Decimal),
Float(f64),
Integer(Integer),
UnsignedInteger(UnsignedInteger),
QName(String),
DateTime(DateTime),
NBitUnsignedInteger(NBitUnsignedInteger),
String(String),
List(List),
}
pub struct DateTime {
pub year: Integer,
pub month_day: NBitUnsignedInteger,
pub time: NBitUnsignedInteger,
pub fractional_secs: NBitUnsignedInteger,
pub timezone: NBitUnsignedInteger,
pub presence: bool,
}
impl DateTime {
pub fn new() -> Self {
Self {
year: Integer::new(),
month_day: NBitUnsignedInteger::new(9.into()),
time: NBitUnsignedInteger::new(17.into()),
fractional_secs: NBitUnsignedInteger::new(9.into()),
timezone: NBitUnsignedInteger::new(11.into()),
presence: false,
}
}
}
pub struct Integer {
pub negative: bool,
pub bits: Vec<u8>,
}
impl Integer {
pub fn new() -> Self {
Self {
negative: false,
bits: vec![0],
}
}
}
impl From<i8> for Integer {
fn from(value: i8) -> Self {
Self {
negative: value.is_negative(),
bits: value.abs().to_le_bytes().to_vec(),
}
}
}
pub struct UnsignedInteger {
pub bits: Vec<u8>,
}
impl UnsignedInteger {
pub fn new() -> Self {
Self { bits: vec![0] }
}
}
impl From<i8> for UnsignedInteger {
fn from(value: i8) -> Self {
Self {
bits: value.abs().to_le_bytes().to_vec(),
}
}
}

View file

@ -1,5 +1,5 @@
[package] [package]
name = "exi" name = "xml"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

232
libraries/xml/src/lib.rs Normal file
View file

@ -0,0 +1,232 @@
use core::fmt::{Debug, Display};
#[derive(PartialEq, Clone)]
pub struct Attribute {
name: String,
value: String,
}
impl Attribute {
fn to_string(&self) -> String {
format!("{:?}={:?}", self.name, self.value)
}
}
#[derive(Clone)]
// TODO Add an autotype attribute that defaults to true
pub struct XMLElement {
pub name: String,
pub text: String,
pub attributes: Vec<Attribute>,
pub children: Vec<XMLElement>,
}
impl XMLElement {
pub fn new<T: Display + Debug>(name: T) -> Self {
Self {
name: name.to_string(),
text: String::new(),
attributes: vec![],
children: vec![],
}
}
pub fn to_string(self) -> String {
self.into()
}
pub fn set_child(&mut self, child: XMLElement) {
// TODO: handle replacing preexisting children
if child.name == "".to_string() {
panic!("empty element name")
}
self.children.push(child);
}
pub fn set_attribute<T1: Display + Debug, T2: Display + Debug>(&mut self, name: T1, value: T2) {
let new_attr = Attribute {
name: name.to_string(),
value: value.to_string(),
};
let mut duplicate_attribute = (false, 0);
for (index, attr) in self.attributes.clone().into_iter().enumerate() {
if attr.name == name.to_string() {
duplicate_attribute = (true, index);
break;
}
}
match duplicate_attribute {
(false, _) => {
self.attributes.push(new_attr.clone());
}
(true, index) => {
self.attributes[index] = new_attr.clone();
}
}
}
}
impl Into<String> for XMLElement {
fn into(self) -> String {
let mut string = "".to_string();
let element_open: String = format!("<{}", self.name);
string.push_str(&element_open);
if self.attributes.len() > 0 {
string.push(' ');
}
let mut attr_count = 0;
for attr in &self.attributes {
string.push_str(&attr.to_string());
attr_count += 1;
if &attr_count != &self.attributes.len() {
string.push(' ');
}
}
string.push('>');
string.push_str(&self.text);
for children in &self.children.clone() {
let chl: String = (*children).clone().into();
string.push_str(&chl);
}
let element_close: String = format!("</{}>", self.name);
string.push_str(&element_close);
string
}
}
#[test]
fn element_to_string_test() {
let mut xml_ele = XMLElement::new("mouse");
xml_ele.set_attribute("x", 0);
xml_ele.set_attribute("x", 1);
xml_ele.set_child(XMLElement::new("last_update"));
let xml_str: String = xml_ele.into();
let rhs = r##"<mouse "x"="1"><last_update></last_update></mouse>"##;
assert_eq!(xml_str, rhs.to_string());
}
#[repr(C)]
#[derive(Debug)]
pub enum Types {
// Used to represent an empty attribute
None,
Integer(Int),
UnsignedInteger(UInt),
Fixed(FixedPoint),
Float(FloatingPoint),
String(String),
}
#[derive(Debug)]
pub enum FixedPoint {
Fixed16(bool, u8, u8),
Fixed32(bool, u16, u16),
Fixed64(bool, u32, u32),
Fixed128(bool, u64, u64),
Fixed256(bool, u128, u128),
}
#[derive(Debug)]
pub enum FloatingPoint {
Float32(f32),
Float64(f64),
}
#[derive(Debug)]
pub enum Int {
I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),
}
#[derive(Debug)]
pub enum UInt {
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
}
impl From<u64> for UInt {
fn from(value: u64) -> Self {
Self::U64(value)
}
}
#[test]
pub fn test_string_to_num() {
println!("{:?}", string_to_num(256));
}
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: String = string.to_string();
use FloatingPoint::*;
use Int::*;
use Types::{Fixed, Float, Integer, UnsignedInteger};
use UInt::*;
match string.parse::<u8>() {
Ok(num) => return Ok(UnsignedInteger(U8(num))),
Err(_) => {}
}
match string.parse::<u16>() {
Ok(num) => return Ok(UnsignedInteger(U16(num))),
Err(_) => {}
}
match string.parse::<u32>() {
Ok(num) => return Ok(UnsignedInteger(U32(num))),
Err(_) => {}
}
match string.parse::<u64>() {
Ok(num) => return Ok(UnsignedInteger(U64(num))),
Err(_) => {}
}
match string.parse::<u128>() {
Ok(num) => return Ok(UnsignedInteger(U128(num))),
Err(_) => {}
}
match string.parse::<i8>() {
Ok(num) => return Ok(Integer(I8(num))),
Err(_) => {}
}
match string.parse::<i16>() {
Ok(num) => return Ok(Integer(I16(num))),
Err(_) => {}
}
match string.parse::<i32>() {
Ok(num) => return Ok(Integer(I32(num))),
Err(_) => {}
}
match string.parse::<i64>() {
Ok(num) => return Ok(Integer(I64(num))),
Err(_) => {}
}
match string.parse::<i128>() {
Ok(num) => return Ok(Integer(I128(num))),
Err(_) => {}
}
match string.parse::<f32>() {
Ok(num) => return Ok(Float(Float32(num))),
Err(_) => {}
}
match string.parse::<f64>() {
Ok(num) => return Ok(Float(Float64(num))),
Err(_) => {}
}
// TODO: Decimal handler
{}
Err(XMLError::TypeNotANumber)
}
#[derive(Debug)]
pub enum XMLError {
TypeNotANumber,
}

View file

@ -6,5 +6,4 @@ 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]
simple_xml_serialize = "*" xml = { path = "../../libraries/xml" }
xml-builder = "0.5.2"

View file

@ -1,28 +1,7 @@
// use simple_xml_serialize::XMLElement; use xml;
// fn main() {
// let mut ele = XMLElement::new("mouse");
// println!("{}", ele);
// }
use xml_builder::{XMLBuilder, XMLElement, XMLVersion};
fn main() { fn main() {
let mut xml = XMLBuilder::new() let root = xml::XMLElement::new("name");
.version(XMLVersion::XML1_1)
.encoding("UTF-8".into())
.build();
let mut mouse = XMLElement::new("mouse"); println!("{}", root.to_string())
mouse.add_attribute("x", "8");
mouse.add_attribute("y", "0");
mouse.add_attribute("y_change", "-1");
mouse.add_attribute("x_change", "1");
xml.set_root_element(mouse);
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();
println!("{}", String::from_utf8_lossy(&writer));
} }