construct-typing/construct_typed/tarray.py
2021-01-01 13:55:19 +01:00

38 lines
962 B
Python

import typing as t
import construct as cs
from .generic_wrapper import *
class TArray(
Adapter[
SubconParsedType,
SubconBuildTypes,
t.List[SubconParsedType],
t.List[SubconParsedType],
]
):
"""
Adapter for an Array, that transforms the "ListContainer" to an standard "list" while parsing
"""
def __init__(
self,
count: ConstantOrContextLambda[int],
subcon: Construct[SubconParsedType, SubconBuildTypes],
discard: bool = False,
) -> None:
# init adatper
super(TArray, self).__init__(cs.Array(count, subcon, discard)) # type: ignore
def _decode(
self, obj: ListContainer[ParsedType], context: Context, path: PathType
) -> ParsedType:
return list(obj) # type: ignore
def _encode(
self,
obj: t.Any,
context: Context,
path: PathType,
) -> t.List[t.Any]:
return obj # type: ignore