From 51a074d2c0c324085d174779bc82e30d334a7d47 Mon Sep 17 00:00:00 2001 From: Wataru Otsubo Date: Thu, 30 Jan 2025 19:53:03 +0900 Subject: [PATCH] add(parser): FieldFifoInterface and custom bool (YES/Y/NO/N) parser --- src/parser.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index aea5d71..1afa7c7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,6 +4,7 @@ use std::any; pub(crate) use parse_prefixed_u32::ParsePrefixedU32; pub use parse_prefixed_u32::ParsePrefixedU32Error; +pub(crate) use parse_custom_bool::ParseCustomBool; use thiserror::Error; #[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 { + match s { + "Vector" => Ok(Self::Vector), + "Block" => Ok(Self::Block), + "Both" => Ok(Self::Both), + s => Err(ParseEnumError::invalid_variant::(s.to_string())), + } + } + } +} + +mod parse_custom_bool { + + use super::ParseEnumError; + + pub(crate) trait ParseCustomBool { + fn parse_custom_bool(self) -> Result; + } + + impl ParseCustomBool for &str { + fn parse_custom_bool(self) -> Result { + match self { + "YES" | "Y" => Ok(true), + "NO" | "N" => Ok(false), + s => Err(ParseEnumError::invalid_variant::(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::("AO".to_string())) + ); + } + } +}