Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c2a13da
Split out endian + added encode function
ryankurte Aug 8, 2017
9c7f2fb
Split access, added encode and tests
ryankurte Aug 8, 2017
c252c1a
Split usage, added encode and tests
ryankurte Aug 8, 2017
2d36b81
Whoops, added files
ryankurte Aug 8, 2017
eea653b
Added encoding to enumerated values
ryankurte Aug 8, 2017
b3d4ba4
Split Defaults + added encode_children function
ryankurte Aug 12, 2017
ee4a8a1
Splitting writeconstraint/writeconstraintrange.
ryankurte Aug 13, 2017
ceb577c
Bitranges, yeeeeaah
ryankurte Aug 16, 2017
2abbdfd
Interrupt module
ryankurte Aug 17, 2017
0eebd41
Split field and register, haven't implemented encode or tests yet.
ryankurte Aug 22, 2017
8e248d5
Registerinfo tests
ryankurte Aug 22, 2017
87b078e
Playing with lossless uint encoding/decoding type
ryankurte Aug 22, 2017
89094bb
Playing with lossless uint encoding/decoding type
ryankurte Aug 22, 2017
ed68e57
Almost there -big sigh-
ryankurte Aug 22, 2017
e543875
Reformatted (with 120 linewidth)
ryankurte Aug 22, 2017
821e11c
Working on e2e tests to make sure encoding works /ok/
ryankurte Aug 29, 2017
6607684
Working on e2e import/export tests
ryankurte Sep 12, 2017
05b9e77
Input/output processing looking pretty close to matching, some differ…
ryankurte Sep 12, 2017
93df472
Added notes on normalising files
ryankurte Sep 12, 2017
ed018a6
Normalising example files, added script to help.
ryankurte Sep 12, 2017
268dea2
Removed examples from repo, should drop LOC lots
ryankurte Sep 12, 2017
d564bd4
Mostly merged
ryankurte Sep 21, 2017
480c796
Refactored out try! macro and renamed to avoid confusion with rust st…
ryankurte Sep 21, 2017
cdbb797
Fixed PartialEq error, moved _extensible to pub(crate)
ryankurte Sep 23, 2017
6638c7b
Cleaning up unused imports etc.
ryankurte Sep 25, 2017
dd34c72
Cleaning up glob imports
ryankurte Sep 25, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
extern crate xmltree;

use std::collections::HashMap;

use xmltree::Element;

use helpers::*;

macro_rules! try {
($e:expr) => {
$e.expect(concat!(file!(), ":", line!(), " ", stringify!($e)))
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Access {
ReadOnly,
ReadWrite,
ReadWriteOnce,
WriteOnce,
WriteOnly,
}

impl ParseElem for Access {
fn parse(tree: &Element) -> Access {
let text = try!(tree.text.as_ref());

match &text[..] {
"read-only" => Access::ReadOnly,
"read-write" => Access::ReadWrite,
"read-writeOnce" => Access::ReadWriteOnce,
"write-only" => Access::WriteOnly,
"writeOnce" => Access::WriteOnce,
_ => panic!("unknown access variant: {}", text),
}
}
}

impl EncodeElem for Access {
fn encode(&self) -> Element {
let text = match *self {
Access::ReadOnly => String::from("read-only"),
Access::ReadWrite => String::from("read-write"),
Access::ReadWriteOnce => String::from("read-writeOnce"),
Access::WriteOnly => String::from("write-only"),
Access::WriteOnce => String::from("writeOnce"),
};

Element{
name: String::from("access"),
attributes: HashMap::new(),
children: Vec::new(),
text: Some(text),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn decode_encode() {
let types = vec![
(Access::ReadOnly, String::from("<access>read-only</access>")),
(Access::ReadWrite, String::from("<access>read-write</access>")),
(Access::ReadWriteOnce, String::from("<access>read-writeOnce</access>")),
(Access::WriteOnly, String::from("<access>write-only</access>")),
(Access::WriteOnce, String::from("<access>writeOnce</access>"))
];

for (a, s) in types {
let tree1 = &try!(Element::parse(s.as_bytes()));
let access = Access::parse(tree1);
assert_eq!(access, a, "Parsing `{}` expected `{:?}`", s, a);
let tree2 = &access.encode();
assert_eq!(tree1, tree2, "Encoding {:?} expected {}", a, s);
}
}
}
76 changes: 76 additions & 0 deletions src/endian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
extern crate xmltree;

use std::collections::HashMap;

use xmltree::Element;

use helpers::*;

macro_rules! try {
($e:expr) => {
$e.expect(concat!(file!(), ":", line!(), " ", stringify!($e)))
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Endian {
Little,
Big,
Selectable,
Other
}

impl ParseElem for Endian {
fn parse(tree: &Element) -> Endian {
let text = try!(tree.text.as_ref());

match &text[..] {
"little" => Endian::Little,
"big" => Endian::Big,
"selectable" => Endian::Selectable,
"other" => Endian::Other,
_ => panic!("unknown endian variant: {}", text),
}
}
}

impl EncodeElem for Endian {
fn encode(&self) -> Element {
let text = match *self {
Endian::Little => String::from("little"),
Endian::Big => String::from("big"),
Endian::Selectable => String::from("selectable"),
Endian::Other => String::from("other"),
};

Element{
name: String::from("endian"),
attributes: HashMap::new(),
children: Vec::new(),
text: Some(text),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn decode_encode() {
let types = vec![
(Endian::Little, String::from("<endian>little</endian>")),
(Endian::Big, String::from("<endian>big</endian>")),
(Endian::Selectable, String::from("<endian>selectable</endian>")),
(Endian::Other, String::from("<endian>other</endian>"))
];

for (e, s) in types {
let tree1 = &try!(Element::parse(s.as_bytes()));
let endian = Endian::parse(tree1);
assert_eq!(endian, e, "Parsing `{}` expected `{:?}`", s, e);
let tree2 = &endian.encode();
assert_eq!(tree1, tree2, "Encoding {:?} expected {}", e, s);
}
}
}
115 changes: 115 additions & 0 deletions src/enumeratedvalue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
extern crate xmltree;

use std::collections::HashMap;

use xmltree::Element;
use ElementExt;

use helpers::*;
use parse;

macro_rules! try {
($e:expr) => {
$e.expect(concat!(file!(), ":", line!(), " ", stringify!($e)))
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct EnumeratedValue {
pub name: String,
pub description: Option<String>,
pub value: Option<u32>,
pub is_default: Option<bool>,
// Reserve the right to add more fields to this struct
pub _extensible: (),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making _extensible field public kinda defeats the purpose.

@ryankurte ryankurte Aug 8, 2017

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the review!

Ahh, yeah. I couldn't work out how to initialise an object with private fields / didn't find anything with wild googling.

The best solution I came up with was to create new constructors for each object, is that the best way to keep it hidden?

}

impl EnumeratedValue {
pub fn parse(tree: &Element) -> Option<EnumeratedValue> {
assert_eq!(tree.name, "enumeratedValue");

Some(
EnumeratedValue {
name: try!(tree.get_child_text("name")),
description: tree.get_child_text("description"),
value: tree.get_child("value").map(|t| try!(parse::u32(t))),
is_default: tree.get_child_text("isDefault").map(
|t| {
try!(t.parse())
},
),
_extensible: (),
},
)
}
}

impl EncodeElem for EnumeratedValue {
fn encode(&self) -> Element {
let mut base = Element{
name: String::from("enumeratedValue"),
attributes: HashMap::new(),
children: vec![
new_element("name", Some(self.name.clone())),
],
text: None,
};

match self.description {
Some(ref d) => {
let s = (*d).clone();
base.children.push(new_element("description", Some(s)));
},
None => (),
};

match self.value {
Some(ref v) => {
base.children.push(new_element("value", Some(format!("0x{:08.x}", *v))));
},
None => (),
};

match self.is_default {
Some(ref v) => {
base.children.push(new_element("isDefault", Some(format!("{}", v))));
},
None => (),
};

base
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn decode_encode() {
let example = String::from("
<enumeratedValue>
<name>WS0</name>
<description>Zero wait-states inserted in fetch or read transfers</description>
<value>0x00000000</value>
<isDefault>true</isDefault>
</enumeratedValue>
");
let expected = EnumeratedValue{
name: String::from("WS0"),
description: Some(String::from("Zero wait-states inserted in fetch or read transfers")),
value: Some(0),
is_default: Some(true),
_extensible: (),
};

let tree1 = &try!(Element::parse(example.as_bytes()));

let parsed = EnumeratedValue::parse(tree1).unwrap();
assert_eq!(parsed, expected, "Parsing tree failed");

let tree2 = &parsed.encode();
assert_eq!(tree1, tree2, "Encoding value failed");
}
}
Loading