1# Copyright 2023 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from dataclasses import dataclass
16from typing import Tuple
17
18
19@dataclass
20class SizedCustomField:
21
22    def __init__(self, value: int = 0):
23        self.value = value
24
25    def parse(span: bytes) -> Tuple['SizedCustomField', bytes]:
26        return (SizedCustomField(span[0]), span[1:])
27
28    def parse_all(span: bytes) -> 'SizedCustomField':
29        assert (len(span) == 1)
30        return SizedCustomField(span[0])
31
32    @property
33    def size(self) -> int:
34        return 1
35
36
37@dataclass
38class UnsizedCustomField:
39
40    def __init__(self, value: int = 0):
41        self.value = value
42
43    def parse(span: bytes) -> Tuple['UnsizedCustomField', bytes]:
44        return (UnsizedCustomField(span[0]), span[1:])
45
46    def parse_all(span: bytes) -> 'UnsizedCustomField':
47        assert (len(span) == 1)
48        return UnsizedCustomField(span[0])
49
50    @property
51    def size(self) -> int:
52        return 1
53
54
55def Checksum(span: bytes) -> int:
56    return sum(span) % 256
57