-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from rust-embedded/datatype
add Datatype
- Loading branch information
Showing
11 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,11 @@ | ||
use super::{Config, Element, Encode, EncodeError, XMLNode}; | ||
|
||
impl Encode for crate::svd::DataType { | ||
type Error = EncodeError; | ||
|
||
fn encode_with_config(&self, _config: &Config) -> Result<Element, EncodeError> { | ||
let mut elem = Element::new("dataType"); | ||
elem.children.push(XMLNode::Text(self.as_str().to_string())); | ||
Ok(elem) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,14 @@ | ||
use super::*; | ||
use crate::svd::DataType; | ||
|
||
impl Parse for DataType { | ||
type Object = Self; | ||
type Error = SVDErrorAt; | ||
type Config = Config; | ||
|
||
fn parse(tree: &Node, _config: &Self::Config) -> Result<Self, Self::Error> { | ||
let text = tree.get_text()?; | ||
|
||
Self::parse_str(text).ok_or_else(|| SVDError::InvalidDatatype(text.into()).at(tree.id())) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,88 @@ | ||
/// Register data type | ||
#[cfg_attr( | ||
feature = "serde", | ||
derive(serde::Deserialize, serde::Serialize), | ||
serde(rename_all = "snake_case") | ||
)] | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub enum DataType { | ||
/// unsigned byte | ||
U8, | ||
/// unsigned half word | ||
U16, | ||
/// unsigned word | ||
U32, | ||
/// unsigned double word | ||
U64, | ||
/// signed byte | ||
I8, | ||
/// signed half word | ||
I16, | ||
/// signed world | ||
I32, | ||
/// signed double word | ||
I64, | ||
/// pointer to unsigned byte | ||
U8Ptr, | ||
/// pointer to unsigned half word | ||
U16Ptr, | ||
/// pointer to unsigned word | ||
U32Ptr, | ||
/// pointer to unsigned double word | ||
U64Ptr, | ||
/// pointer to signed byte | ||
I8Ptr, | ||
/// pointer to signed half word | ||
I16Ptr, | ||
/// pointer to signed world | ||
I32Ptr, | ||
/// pointer to signed double word | ||
I64Ptr, | ||
} | ||
|
||
impl DataType { | ||
/// Parse a string into an [`DataType`] value, returning [`Option::None`] if the string is not valid. | ||
pub fn parse_str(s: &str) -> Option<Self> { | ||
match s { | ||
"uint8_t" => Some(Self::U8), | ||
"uint16_t" => Some(Self::U16), | ||
"uint32_t" => Some(Self::U32), | ||
"uint64_t" => Some(Self::U64), | ||
"int8_t" => Some(Self::I8), | ||
"int16_t" => Some(Self::I16), | ||
"int32_t" => Some(Self::I32), | ||
"int64_t" => Some(Self::I64), | ||
"uint8_t *" => Some(Self::U8Ptr), | ||
"uint16_t *" => Some(Self::U16Ptr), | ||
"uint32_t *" => Some(Self::U32Ptr), | ||
"uint64_t *" => Some(Self::U64Ptr), | ||
"int8_t *" => Some(Self::I8Ptr), | ||
"int16_t *" => Some(Self::I16Ptr), | ||
"int32_t *" => Some(Self::I32Ptr), | ||
"int64_t *" => Some(Self::I64Ptr), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Convert this [`DataType`] into a static string. | ||
pub const fn as_str(self) -> &'static str { | ||
match self { | ||
Self::U8 => "uint8_t", | ||
Self::U16 => "uint16_t", | ||
Self::U32 => "uint32_t", | ||
Self::U64 => "uint64_t", | ||
Self::I8 => "int8_t", | ||
Self::I16 => "int16_t", | ||
Self::I32 => "int32_t", | ||
Self::I64 => "int64_t", | ||
Self::U8Ptr => "uint8_t *", | ||
Self::U16Ptr => "uint16_t *", | ||
Self::U32Ptr => "uint32_t *", | ||
Self::U64Ptr => "uint64_t *", | ||
Self::I8Ptr => "int8_t *", | ||
Self::I16Ptr => "int16_t *", | ||
Self::I32Ptr => "int32_t *", | ||
Self::I64Ptr => "int64_t *", | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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