|
| 1 | +import dataclasses as std_dc |
| 2 | + |
| 3 | +from collections import deque |
| 4 | +from typing import Any, Dict, Generic, Optional, Protocol, Type, TypeVar, Union |
| 5 | + |
| 6 | +T = TypeVar("T") |
| 7 | + |
| 8 | + |
| 9 | +class Result(Generic[T]): |
| 10 | + """Represents a result.""" |
| 11 | + |
| 12 | + ok_data: Optional[T] |
| 13 | + errors: Optional["deque[str]"] # O(1) |
| 14 | + |
| 15 | + def __init__(self, okd: Optional[T], errors: Optional["deque[str]"]): |
| 16 | + self.ok_data = okd |
| 17 | + self.errors = errors |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def Ok(cls, data: T) -> "Result[T]": |
| 21 | + return cls(data, None) |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def Err(cls, *errors: str) -> "Result[T]": |
| 25 | + return cls(None, deque(errors)) |
| 26 | + |
| 27 | + def unwrap(self) -> T: |
| 28 | + """Unwrap the OK data.""" |
| 29 | + # cheap operation lmfao |
| 30 | + return self.ok_data # type: ignore |
| 31 | + |
| 32 | + def unwrap_err(self) -> "deque[str]": |
| 33 | + """Unwrap the Err data.""" |
| 34 | + # AGAIN. lmfao! you gotta be responsible. |
| 35 | + return self.errors # type: ignore |
| 36 | + |
| 37 | + def is_ok(self) -> bool: |
| 38 | + """CALL.""" |
| 39 | + return not self.errors |
| 40 | + |
| 41 | + def trace(self, upper: str) -> "Result[T]": |
| 42 | + if self.errors is not None: |
| 43 | + self.errors.appendleft("indent") |
| 44 | + self.errors.appendleft(upper) |
| 45 | + self.errors.append("unindent") |
| 46 | + |
| 47 | + return self |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def trace_below(cls, upper: str, *items: str) -> "Result[T]": |
| 51 | + errors = deque(items) |
| 52 | + errors.appendleft("indent") |
| 53 | + errors.appendleft(upper) |
| 54 | + errors.append("unindent") |
| 55 | + |
| 56 | + return cls(okd=None, errors=errors) |
| 57 | + |
| 58 | + def __repr__(self) -> str: |
| 59 | + if self.is_ok(): |
| 60 | + return f"Result.Ok({self.unwrap()!r})" |
| 61 | + else: |
| 62 | + return f"Result.Err({self.unwrap_err()!r})" |
| 63 | + |
| 64 | + |
| 65 | +class Dataclass(Protocol): |
| 66 | + __dataclass_fields__: Dict[str, std_dc.Field] |
| 67 | + |
| 68 | + |
| 69 | +DataclassType = Union[Type[Dataclass], Any] |
| 70 | + |
| 71 | + |
| 72 | +class _Indexable: |
| 73 | + def __getitem__(self, k: str): ... |
| 74 | + def __setitem__(self, k: str, data: Any): ... |
| 75 | + def as_dict(self) -> dict: ... |
| 76 | + def as_dc(self) -> Dataclass: ... |
| 77 | + |
| 78 | + |
| 79 | +class _DefinitelyDict(_Indexable): |
| 80 | + def __init__(self, d: Dict): |
| 81 | + self.data = d |
| 82 | + |
| 83 | + def __getitem__(self, k: str): |
| 84 | + self.data[k] |
| 85 | + |
| 86 | + def __setitem__(self, k: str, data: Any): |
| 87 | + self.data[k] = data |
| 88 | + |
| 89 | + def as_dict(self) -> dict: |
| 90 | + return self.data |
| 91 | + |
| 92 | + def as_dc(self) -> Dataclass: |
| 93 | + raise TypeError("This indexable is not a dataclass but a dict") |
| 94 | + |
| 95 | + |
| 96 | +class _DefinitelyDataclass(_Indexable): |
| 97 | + def __init__(self, dc: Dataclass): |
| 98 | + self.dc = dc |
| 99 | + |
| 100 | + def __getitem__(self, k: str): |
| 101 | + return getattr(self.dc, k) |
| 102 | + |
| 103 | + def __setitem__(self, k: str, data: Any): |
| 104 | + setattr(self.dc, k, data) |
| 105 | + |
| 106 | + def as_dict(self): |
| 107 | + raise TypeError("This indexable is not a dict but a dataclass") |
| 108 | + |
| 109 | + def as_dc(self) -> Dataclass: |
| 110 | + return self.dc |
| 111 | + |
| 112 | + |
| 113 | +def indexable(item: Any) -> "_Indexable": |
| 114 | + if isinstance(item, dict): |
| 115 | + return _DefinitelyDict(item) |
| 116 | + else: |
| 117 | + return _DefinitelyDataclass(item) |
0 commit comments