mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-02-23 17:17:08 +09:00
refactor: split ParsePrefixedU32 to another mod (parser)
This commit is contained in:
parent
b549445b26
commit
f6d711ad51
3 changed files with 49 additions and 28 deletions
|
@ -4,6 +4,7 @@ use roxmltree::{Node, TextPos};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::types::Module;
|
use crate::types::Module;
|
||||||
|
use crate::parser::{ParsePrefixedU32, ParsePrefixedU32Error};
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum DomConversionError {
|
pub enum DomConversionError {
|
||||||
|
@ -61,34 +62,6 @@ impl DomConversionError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum ParsePrefixedU32Error {
|
|
||||||
#[error("invalid prefix: found {found}")]
|
|
||||||
InvalidPrefix { found: char },
|
|
||||||
#[error("parse int error")]
|
|
||||||
ParseIntError(#[from] num::ParseIntError),
|
|
||||||
}
|
|
||||||
|
|
||||||
trait ParsePrefixedU32 {
|
|
||||||
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParsePrefixedU32 for &str {
|
|
||||||
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error> {
|
|
||||||
let radix = match self.chars().nth(1) {
|
|
||||||
Some('x') => 16,
|
|
||||||
Some(c) if c.is_numeric() => 10,
|
|
||||||
Some(c) => return Err(ParsePrefixedU32Error::InvalidPrefix { found: c }),
|
|
||||||
None => return Err(ParsePrefixedU32Error::InvalidPrefix { found: ' ' }),
|
|
||||||
};
|
|
||||||
|
|
||||||
match radix {
|
|
||||||
10 => Ok(self.parse()?),
|
|
||||||
radix => Ok(u32::from_str_radix(&self[2..], radix)?),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub fn from_xml_dom(root: Node) -> Result<Self, DomConversionError> {
|
pub fn from_xml_dom(root: Node) -> Result<Self, DomConversionError> {
|
||||||
assert!(root.parent().unwrap().is_root());
|
assert!(root.parent().unwrap().is_root());
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
pub mod parser;
|
||||||
pub mod converter;
|
pub mod converter;
|
||||||
|
|
47
src/parser.rs
Normal file
47
src/parser.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
//! Module for converting XML custom token string to rust types.
|
||||||
|
|
||||||
|
use std::num;
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ParsePrefixedU32Error {
|
||||||
|
#[error("invalid prefix: found {found}")]
|
||||||
|
InvalidPrefix { found: char },
|
||||||
|
#[error("parse int error")]
|
||||||
|
ParseIntError(#[from] num::ParseIntError),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) trait ParsePrefixedU32 {
|
||||||
|
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParsePrefixedU32 for &str {
|
||||||
|
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error> {
|
||||||
|
let radix = match self.chars().nth(1) {
|
||||||
|
Some('x') => 16,
|
||||||
|
Some(c) if c.is_numeric() => 10,
|
||||||
|
Some(c) => return Err(ParsePrefixedU32Error::InvalidPrefix { found: c }),
|
||||||
|
None => return Err(ParsePrefixedU32Error::InvalidPrefix { found: ' ' }),
|
||||||
|
};
|
||||||
|
|
||||||
|
match radix {
|
||||||
|
10 => Ok(self.parse()?),
|
||||||
|
radix => Ok(u32::from_str_radix(&self[2..], radix)?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::ParsePrefixedU32;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_prefixed_u32() {
|
||||||
|
assert_eq!("0xdeadbeef".parse_prefixed_u32().unwrap(), 0xdeadbeef);
|
||||||
|
assert_eq!("0xDEADBEEF".parse_prefixed_u32().unwrap(), 0xdeadbeef);
|
||||||
|
assert_eq!("1234".parse_prefixed_u32().unwrap(), 1234);
|
||||||
|
|
||||||
|
assert!("0abcde".parse_prefixed_u32().is_err());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue