@@ -11,10 +11,11 @@ use crate::read::ReadCtxt;
1111use crate :: {
1212 Arith , BaseKind , DynFormat , Endian , Expr , Format , IntRel , Label , Pattern , UnaryOp , ViewExpr ,
1313} ;
14+ use serde:: Serialize ;
1415use std:: borrow:: Cow ;
1516use std:: cmp:: Ordering ;
1617
17- #[ derive( Clone , Copy , Debug , PartialEq , Eq , Default ) ]
18+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Default , Serialize ) ]
1819pub enum ParseLoc {
1920 InBuffer {
2021 offset : usize ,
@@ -124,9 +125,10 @@ impl ParseLoc {
124125}
125126
126127/// Helper type for associating a [`ParseLoc`] with a value of a generic type.
127- #[ derive( Clone , Copy , Debug ) ]
128+ #[ derive( Clone , Copy , Debug , Serialize ) ]
128129pub struct Parsed < T : Clone > {
129130 pub ( crate ) loc : ParseLoc ,
131+ #[ serde( rename = "data" ) ]
130132 pub ( crate ) inner : T ,
131133}
132134
@@ -143,7 +145,8 @@ impl<T: Clone> AsRef<T> for Parsed<T> {
143145 }
144146}
145147
146- #[ derive( Clone , Debug ) ]
148+ #[ derive( Clone , Debug , Serialize ) ]
149+ #[ serde( tag = "tag" , content = "data" ) ]
147150pub enum ParsedValue {
148151 /// Flat parses of the sub-set of `Value` variants that do not contain any embedded `Value` terms
149152 Flat ( Parsed < Value > ) ,
@@ -408,6 +411,14 @@ impl ParsedValue {
408411 }
409412 ( Pattern :: Option ( None ) , ParsedValue :: Option ( None ) ) => true ,
410413 ( Pattern :: Option ( Some ( p) ) , ParsedValue :: Option ( Some ( v) ) ) => v. matches_inner ( scope, p) ,
414+ ( Pattern :: Int ( bounds) , ParsedValue :: Flat ( Parsed { inner : v, .. } ) ) => match v {
415+ Value :: U8 ( n) => bounds. contains ( usize:: from ( * n) ) ,
416+ Value :: U16 ( n) => bounds. contains ( usize:: from ( * n) ) ,
417+ Value :: U32 ( n) => bounds. contains ( usize:: try_from ( * n) . unwrap ( ) ) ,
418+ Value :: U64 ( n) => bounds. contains ( usize:: try_from ( * n) . unwrap ( ) ) ,
419+ Value :: Usize ( n) => bounds. contains ( * n) ,
420+ _ => false ,
421+ } ,
411422 _ => false ,
412423 }
413424 }
@@ -792,9 +803,18 @@ impl Expr {
792803 Expr :: AsU8 ( x) => Cow :: Owned ( ParsedValue :: from_evaluated (
793804 match x. eval_value_with_loc ( scope) {
794805 Value :: U8 ( x) => Value :: U8 ( x) ,
795- Value :: U16 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap ( ) ) ,
796- Value :: U32 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap ( ) ) ,
797- Value :: U64 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap ( ) ) ,
806+ Value :: U16 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap_or_else ( |err| {
807+ panic ! ( "cannot perform AsU8 cast on u16 {x}: {err}" )
808+ } ) ) ,
809+ Value :: U32 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap_or_else ( |err| {
810+ panic ! ( "cannot perform AsU8 cast on u32 {x}: {err}" )
811+ } ) ) ,
812+ Value :: U64 ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap_or_else ( |err| {
813+ panic ! ( "cannot perform AsU8 cast on u64 {x}: {err}" )
814+ } ) ) ,
815+ Value :: Usize ( x) => Value :: U8 ( u8:: try_from ( x) . unwrap_or_else ( |err| {
816+ panic ! ( "cannot perform AsU8 cast on usize {x}: {err}" )
817+ } ) ) ,
798818 x => panic ! ( "cannot convert {x:?} to U8" ) ,
799819 } ,
800820 ) ) ,
0 commit comments