[docs]classResult(Generic[T]):""" Represents the result of the server response. Parameters ---------- raw_value: A raw value to deserialize. error: Error message detailing why the operation failed. Attributes ---------- value: Operation result. Stores empty bytes if the operation fails. error: Error message detailing why the operation failed, value is None if operation was successful. """__slots__=("value","error")_deserializer:ClassVar[Deserializer]=Deserializer()def__init__(self,raw_value:bytes,error:str|None=None)->None:iferrorisnotNone:# If we have an error, the value will be empty, so there is no point in deserializing it.self.value:T=raw_value# type: ignoreelse:self.value:T=self._deserializer.process(Reader(raw_value))self.error:str|None=errordef__hash__(self)->int:returnhash((self.value,self.error))def__eq__(self,other:Any)->bool:returnhash(other)==hash(self)def__ne__(self,other:Any)->bool:returnnotself.__eq__(other)def__bool__(self)->bool:returnself.successdef__repr__(self)->str:returnf"<Result(success={self.success})>"@propertydefsuccess(self)->bool:"""True if the operation was successful."""returnself.errorisNone@propertydeffailure(self)->bool:"""True if operation failed."""returnself.errorisnotNone
[docs]@classmethoddeffail(cls,error:str):"""Create a Result object for a failed operation."""returncls(raw_value=bytes(),error=error)
[docs]@classmethoddefok(cls,raw_value:bytes):"""Create a Result object for a successful operation."""returncls(raw_value=raw_value)
[docs]defis_empty(self)->bool:"""Checks if the value is empty."""returnisinstance(self.value,bytes)andnotbool(self.value)