-
Notifications
You must be signed in to change notification settings - Fork 64
[WIP] Adding encode functions to SVD elements #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
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 9c7f2fb
Split access, added encode and tests
ryankurte c252c1a
Split usage, added encode and tests
ryankurte 2d36b81
Whoops, added files
ryankurte eea653b
Added encoding to enumerated values
ryankurte b3d4ba4
Split Defaults + added encode_children function
ryankurte ee4a8a1
Splitting writeconstraint/writeconstraintrange.
ryankurte ceb577c
Bitranges, yeeeeaah
ryankurte 2abbdfd
Interrupt module
ryankurte 0eebd41
Split field and register, haven't implemented encode or tests yet.
ryankurte 8e248d5
Registerinfo tests
ryankurte 87b078e
Playing with lossless uint encoding/decoding type
ryankurte 89094bb
Playing with lossless uint encoding/decoding type
ryankurte ed68e57
Almost there -big sigh-
ryankurte e543875
Reformatted (with 120 linewidth)
ryankurte 821e11c
Working on e2e tests to make sure encoding works /ok/
ryankurte 6607684
Working on e2e import/export tests
ryankurte 05b9e77
Input/output processing looking pretty close to matching, some differ…
ryankurte 93df472
Added notes on normalising files
ryankurte ed018a6
Normalising example files, added script to help.
ryankurte 268dea2
Removed examples from repo, should drop LOC lots
ryankurte d564bd4
Mostly merged
ryankurte 480c796
Refactored out try! macro and renamed to avoid confusion with rust st…
ryankurte cdbb797
Fixed PartialEq error, moved _extensible to pub(crate)
ryankurte 6638c7b
Cleaning up unused imports etc.
ryankurte dd34c72
Cleaning up glob imports
ryankurte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: (), | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
making
_extensiblefield public kinda defeats the purpose.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
newconstructors for each object, is that the best way to keep it hidden?