Skip to content

Commit cd837c8

Browse files
committed
Add optional datum type enforcement during parsing.
1 parent 2d391a9 commit cd837c8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/MediaStation/Primitives/Datum.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from enum import IntEnum
2+
from typing import Optional
23

34
import self_documenting_struct as struct
45
from asset_extraction_framework.Exceptions import BinaryParsingError
@@ -54,10 +55,12 @@ class Type(IntEnum):
5455
## The number of bytes read from the stream depends on the type
5556
## of the datum.
5657
## \param[in] stream - A binary stream that supports the read method.
57-
def __init__(self, stream):
58+
def __init__(self, stream, expected_type: Optional[Type] = None):
5859
# READ THE TYPE OF THE DATUM.
5960
# Regardless of the datum's value the type always has constant size.
6061
self.t = struct.unpack.uint16_le(stream)
62+
if expected_type is not None and self.t != expected_type:
63+
raise BinaryParsingError(f'Expected datum type {expected_type.name}, but got datum type {self.Type(self.t).name}.')
6164

6265
# READ THE VALUE IN THE DATUM.
6366
if (self.t == Datum.Type.UINT8):

0 commit comments

Comments
 (0)