Skip to content

Commit

Permalink
Add optional datum type enforcement during parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
npjg committed Aug 22, 2024
1 parent 2d391a9 commit cd837c8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/MediaStation/Primitives/Datum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from typing import Optional

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

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

0 comments on commit cd837c8

Please sign in to comment.