mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-02-23 17:17:08 +09:00
refactor: create mod parse_prefixed_u32
This commit is contained in:
parent
1b93197ef6
commit
371068e363
1 changed files with 42 additions and 37 deletions
|
@ -1,22 +1,26 @@
|
||||||
//! Module for converting XML custom token string to rust types.
|
//! Module for converting XML custom token string to rust types.
|
||||||
|
|
||||||
use std::num;
|
pub(crate) use parse_prefixed_u32::ParsePrefixedU32;
|
||||||
|
pub use parse_prefixed_u32::ParsePrefixedU32Error;
|
||||||
|
|
||||||
use thiserror::Error;
|
mod parse_prefixed_u32 {
|
||||||
|
use std::num;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
use thiserror::Error;
|
||||||
pub enum ParsePrefixedU32Error {
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ParsePrefixedU32Error {
|
||||||
#[error("invalid prefix: found {found}")]
|
#[error("invalid prefix: found {found}")]
|
||||||
InvalidPrefix { found: char },
|
InvalidPrefix { found: char },
|
||||||
#[error("parse int error")]
|
#[error("parse int error")]
|
||||||
ParseIntError(#[from] num::ParseIntError),
|
ParseIntError(#[from] num::ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait ParsePrefixedU32 {
|
pub(crate) trait ParsePrefixedU32 {
|
||||||
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error>;
|
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParsePrefixedU32 for &str {
|
impl ParsePrefixedU32 for &str {
|
||||||
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error> {
|
fn parse_prefixed_u32(&self) -> Result<u32, ParsePrefixedU32Error> {
|
||||||
let radix = match self.chars().nth(1) {
|
let radix = match self.chars().nth(1) {
|
||||||
Some('x') => 16,
|
Some('x') => 16,
|
||||||
|
@ -30,10 +34,10 @@ impl ParsePrefixedU32 for &str {
|
||||||
radix => Ok(u32::from_str_radix(&self[2..], radix)?),
|
radix => Ok(u32::from_str_radix(&self[2..], radix)?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::ParsePrefixedU32;
|
use super::ParsePrefixedU32;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -44,4 +48,5 @@ mod test {
|
||||||
|
|
||||||
assert!("0abcde".parse_prefixed_u32().is_err());
|
assert!("0abcde".parse_prefixed_u32().is_err());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue