Major reorganization
[ton] / don / _shared.py
1 import collections
2
3 from don import constants
4
5 _TYPES_TO_TAGS = {
6     int: constants.DEFAULT_INTEGER_ENCODING,
7     float: constants.DEFAULT_DECIMAL_ENCODING,
8     bytes: constants.BINARY,
9     str: constants.DEFAULT_STRING_ENCODING,
10     list: constants.LIST,
11     dict: constants.DICTIONARY,
12     collections.OrderedDict: constants.DICTIONARY,
13 }
14
15 TaggedObject = collections.namedtuple('TaggedObject', ['tag', 'value'])
16
17 def _tag(o):
18     if isinstance(o, TaggedObject):
19         return o
20
21     if o is None:
22         return TaggedObject(tag = constants.VOID, value = o)
23
24     if o is True:
25         return TaggedObject(tag = constants.TRUE, value = o)
26
27     if o is False:
28         return TaggedObject(tag = constants.FALSE, value = o)
29
30     return TaggedObject(tag = _TYPES_TO_TAGS[type(o)], value = o)
31
32 ParseResult = collections.namedtuple(
33     'ParseResult',
34     [
35         'success',
36         'value',
37         'remaining',
38     ],
39 )
40
41 _FAILED_PARSE_RESULT = ParseResult(success = False, value = None, remaining = None)