Skip to content

Commit

Permalink
Remove pydantic support from result
Browse files Browse the repository at this point in the history
  • Loading branch information
danielSanchezQ committed Sep 10, 2024
1 parent 11a9341 commit 4b3ac32
Showing 1 changed file with 0 additions and 75 deletions.
75 changes: 0 additions & 75 deletions rusty_results/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,81 +720,6 @@ def __contains__(self, item: T) -> bool:
def __iter__(self) -> Iterator[T]:
return self.iter()

@classmethod
def __get_validators__(cls):
yield cls.__validate

@classmethod
def __validate(cls, value: Union["Ok", "Err", Dict], field: "ModelField"):
if isinstance(value, Ok):
return cls.__validate_ok(value, field)
elif isinstance(value, Err):
return cls.__validate_err(value, field)
elif isinstance(value, dict):
return cls.__validate_dict(value, field)

raise TypeError("Unable to validate Result") # pragma: no cover

@classmethod
def __validate_ok(cls, value: "Ok", field: "ModelField"):
import pydantic

if not field.sub_fields or len(field.sub_fields) != 2:
raise TypeError("Wrong subfields found for Ok") # pragma: no cover

field_value = field.sub_fields[0]
valid_value, error = field_value.validate(value.Ok, {}, loc="")
if error:
# ignore type since it do not come from a base model
raise pydantic.ValidationError(error, Result) # type: ignore

return Ok(valid_value)

@classmethod
def __validate_err(cls, value: "Err", field: "ModelField"):
import pydantic

if not field.sub_fields or len(field.sub_fields) != 2:
raise TypeError("Wrong subfields found for Ok") # pragma: no cover

field_value = field.sub_fields[1]
valid_value, error = field_value.validate(value.Error, {}, loc="")
if error:
# ignore type since it do not come from a base model
raise pydantic.ValidationError(error, Result) # type: ignore

return Err(valid_value)

@classmethod
def __validate_dict(cls, value: Dict, field: "ModelField"): # mypy: ignore
import pydantic

if not field.sub_fields or len(field.sub_fields) != 2:
raise TypeError("Wrong subfields found for Ok") # pragma: no cover

if len(value) != 1:
raise TypeError(
"Extra object parameters found, Results have strictly 1 value (either Value (Ok) or Error (Err))"
) # pragma: no cover

return_class: Callable[[Any], Any]
inner_value: Any
if "Ok" in value:
inner_value, return_class, subfield = value.get("Ok"), Ok, 0
elif "Error" in value:
inner_value, return_class, subfield = value.get("Error"), Err, 1
else:
# should never be able to reach here
raise TypeError("Cannot find any Result correct value") # pragma: no cover

field_value = field.sub_fields[subfield]
valid_value, error = field_value.validate(inner_value, {}, loc="")
if error:
# ignore type since it do not come from a base model
raise pydantic.ValidationError(error, Result) # type: ignore # pragma: no cover

return return_class(valid_value)


@dataclass(eq=True, frozen=True)
class Ok(ResultProtocol[T, E]):
Expand Down

0 comments on commit 4b3ac32

Please sign in to comment.