Coverage for src/galaxy_beep_frame/converter/token/__init__.py: 100%
23 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-31 04:18 +0200
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-31 04:18 +0200
1"""Subpackage to manage token."""
3from dataclasses import dataclass, field
5from galaxy_beep_frame.exception import (
6 TokenImmutableError,
7)
9from .mutability_state import MutabilityState
10from .mutable_state import MutableState
11from .token_type import TokenType
14@dataclass
15class Token:
16 """
17 Hold information about current state and value.
19 Start mutable (eg. can be modified) and can become irreversably
20 immutable.
21 """
23 type: TokenType
24 value: bytearray = field(default_factory=bytearray)
26 mutability: MutabilityState = field(init=False)
28 def __post_init__(self) -> None:
29 """Post initialization process."""
30 self.mutability = MutableState(self)
32 def _ensure_mutable(self) -> MutableState:
33 """
34 Check if the current mutability of the Token is MutableState.
36 Notes
37 -----
38 Internal use
40 """
41 mutability_state = self.mutability
42 if not isinstance(mutability_state, MutableState):
43 raise TokenImmutableError(self)
44 return mutability_state
46 def update(self, char: int) -> None:
47 """
48 Update value stored in the token by appending a byte.
50 Notes
51 -----
52 This token should not be finished.
54 """
55 mutability_state = self._ensure_mutable()
56 return mutability_state.update(char)
58 def end(self) -> None:
59 """
60 Update value stored in the token by appending a byte.
62 Notes
63 -----
64 After this method is called, the token will not be updatable
65 anymore.
67 """
68 mutability_state = self._ensure_mutable()
69 mutability_state.end()
72__all__ = [
73 "MutabilityState",
74 "MutableState",
75 "Token",
76 "TokenType",
77]