|
1 | | -"""Class for serializing and deserializing a signed 32-bit integer.""" |
| 1 | +"""Class for serializing and deserializing a signed 32-bit integer. |
| 2 | +See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ |
| 3 | +""" |
2 | 4 |
|
3 | 5 | from __future__ import annotations |
4 | 6 |
|
5 | | -from typing import Optional, Type |
| 7 | +from typing import Optional, Type, Union |
6 | 8 |
|
7 | 9 | from typing_extensions import Final, Self |
8 | 10 |
|
9 | 11 | from xrpl.core.binarycodec.binary_wrappers.binary_parser import BinaryParser |
10 | 12 | from xrpl.core.binarycodec.exceptions import XRPLBinaryCodecException |
11 | | -from xrpl.core.binarycodec.types.serialized_type import SerializedType |
| 13 | +from xrpl.core.binarycodec.types.int import Int |
12 | 14 |
|
13 | 15 | _WIDTH: Final[int] = 4 # 32 / 8 |
14 | 16 |
|
15 | 17 |
|
16 | | -class Int32(SerializedType): |
17 | | - """Class for serializing and deserializing a signed 32-bit integer.""" |
| 18 | +class Int32(Int): |
| 19 | + """ |
| 20 | + Class for serializing and deserializing a signed 32-bit integer. |
| 21 | + See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ |
| 22 | + """ |
18 | 23 |
|
19 | 24 | def __init__(self: Self, buffer: bytes = bytes(_WIDTH)) -> None: |
20 | 25 | """Construct a new Int32 type from a ``bytes`` value.""" |
21 | 26 | super().__init__(buffer) |
22 | 27 |
|
23 | | - @property |
24 | | - def value(self: Self) -> int: |
25 | | - """Get the value of the Int32 represented by `self.buffer`.""" |
26 | | - return int.from_bytes(self.buffer, byteorder="big", signed=True) |
27 | | - |
28 | 28 | @classmethod |
29 | 29 | def from_parser( |
30 | 30 | cls: Type[Self], parser: BinaryParser, _length_hint: Optional[int] = None |
31 | 31 | ) -> Self: |
32 | | - """Construct a new Int32 type from a BinaryParser.""" |
| 32 | + """ |
| 33 | + Construct a new Int32 type from a BinaryParser. |
| 34 | +
|
| 35 | + Args: |
| 36 | + parser: A BinaryParser to construct an Int32 from. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + The Int32 constructed from parser. |
| 40 | + """ |
33 | 41 | return cls(parser.read(_WIDTH)) |
34 | 42 |
|
35 | 43 | @classmethod |
36 | | - def from_value(cls: Type[Self], value: int) -> Self: |
37 | | - """Construct a new Int32 type from an integer.""" |
38 | | - if not isinstance(value, int): |
39 | | - raise XRPLBinaryCodecException( |
40 | | - f"Invalid type to construct Int32: expected int, " |
41 | | - f"received {value.__class__.__name__}." |
42 | | - ) |
43 | | - return cls(value.to_bytes(_WIDTH, byteorder="big", signed=True)) |
44 | | - |
45 | | - def to_json(self: Self) -> int: |
46 | | - """Convert the Int32 to JSON (returns the integer value).""" |
47 | | - return self.value |
48 | | - |
49 | | - def __eq__(self: Self, other: object) -> bool: |
50 | | - """Determine whether two Int32 objects are equal.""" |
51 | | - if isinstance(other, int): |
52 | | - return self.value == other |
53 | | - if isinstance(other, Int32): |
54 | | - return self.value == other.value |
55 | | - return NotImplemented |
56 | | - |
57 | | - def __lt__(self: Self, other: object) -> bool: |
58 | | - """Determine whether this Int32 is less than another.""" |
59 | | - if isinstance(other, int): |
60 | | - return self.value < other |
61 | | - if isinstance(other, Int32): |
62 | | - return self.value < other.value |
63 | | - return NotImplemented |
64 | | - |
65 | | - def __gt__(self: Self, other: object) -> bool: |
66 | | - """Determine whether this Int32 is greater than another.""" |
67 | | - if isinstance(other, int): |
68 | | - return self.value > other |
69 | | - if isinstance(other, Int32): |
70 | | - return self.value > other.value |
71 | | - return NotImplemented |
| 44 | + def from_value(cls: Type[Self], value: Union[str, int]) -> Self: |
| 45 | + """ |
| 46 | + Construct a new Int32 type from a number. |
| 47 | +
|
| 48 | + Args: |
| 49 | + value: The number to construct an Int32 from. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + The Int32 constructed from value. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + XRPLBinaryCodecException: If an Int32 could not be constructed from value. |
| 56 | + """ |
| 57 | + if isinstance(value, int): |
| 58 | + value_bytes = (value).to_bytes(_WIDTH, byteorder="big", signed=True) |
| 59 | + return cls(value_bytes) |
| 60 | + |
| 61 | + if isinstance(value, str): |
| 62 | + try: |
| 63 | + int_value = int(value) |
| 64 | + except ValueError as err: |
| 65 | + raise XRPLBinaryCodecException( |
| 66 | + f"Cannot construct Int32 from given value: {value!r}" |
| 67 | + ) from err |
| 68 | + try: |
| 69 | + return cls(int_value.to_bytes(_WIDTH, byteorder="big", signed=True)) |
| 70 | + except OverflowError as err: |
| 71 | + raise XRPLBinaryCodecException( |
| 72 | + f"Cannot construct Int32 from given value: {value!r}" |
| 73 | + ) from err |
| 74 | + |
| 75 | + raise XRPLBinaryCodecException( |
| 76 | + "Invalid type to construct an Int32: expected str or int," |
| 77 | + f" received {value.__class__.__name__}." |
| 78 | + ) |
0 commit comments