mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-02-23 17:17:08 +09:00
add(parser): FieldFifoInterface and custom bool (YES/Y/NO/N) parser
This commit is contained in:
parent
e7d3e5c90b
commit
51a074d2c0
1 changed files with 57 additions and 0 deletions
|
@ -4,6 +4,7 @@ use std::any;
|
||||||
|
|
||||||
pub(crate) use parse_prefixed_u32::ParsePrefixedU32;
|
pub(crate) use parse_prefixed_u32::ParsePrefixedU32;
|
||||||
pub use parse_prefixed_u32::ParsePrefixedU32Error;
|
pub use parse_prefixed_u32::ParsePrefixedU32Error;
|
||||||
|
pub(crate) use parse_custom_bool::ParseCustomBool;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error, PartialEq)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
|
@ -149,3 +150,59 @@ mod parse_rw_specifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod parse_field_fifo_interface {
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::types::FieldFifoInterface;
|
||||||
|
|
||||||
|
use super::ParseEnumError;
|
||||||
|
|
||||||
|
impl FromStr for FieldFifoInterface {
|
||||||
|
type Err = ParseEnumError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s {
|
||||||
|
"Vector" => Ok(Self::Vector),
|
||||||
|
"Block" => Ok(Self::Block),
|
||||||
|
"Both" => Ok(Self::Both),
|
||||||
|
s => Err(ParseEnumError::invalid_variant::<Self>(s.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod parse_custom_bool {
|
||||||
|
|
||||||
|
use super::ParseEnumError;
|
||||||
|
|
||||||
|
pub(crate) trait ParseCustomBool {
|
||||||
|
fn parse_custom_bool(self) -> Result<bool, ParseEnumError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParseCustomBool for &str {
|
||||||
|
fn parse_custom_bool(self) -> Result<bool, ParseEnumError> {
|
||||||
|
match self {
|
||||||
|
"YES" | "Y" => Ok(true),
|
||||||
|
"NO" | "N" => Ok(false),
|
||||||
|
s => Err(ParseEnumError::invalid_variant::<bool>(s.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::{ParseCustomBool, ParseEnumError};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_prefixed_u32() {
|
||||||
|
assert_eq!("YES".parse_custom_bool(), Ok(true));
|
||||||
|
assert_eq!("NO".parse_custom_bool(), Ok(false));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
"AO".parse_custom_bool(),
|
||||||
|
Err(ParseEnumError::invalid_variant::<bool>("AO".to_string()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue