2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-03-07 10:10:36 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import results
|
2023-03-07 10:10:36 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ParsingErrorKind* {.pure.} = enum
|
2024-03-15 23:08:47 +00:00
|
|
|
InvalidFormat
|
2023-03-07 10:10:36 +00:00
|
|
|
MissingPart
|
|
|
|
|
|
|
|
ParsingError* = object
|
|
|
|
case kind*: ParsingErrorKind
|
|
|
|
of InvalidFormat:
|
|
|
|
cause*: string
|
|
|
|
of MissingPart:
|
|
|
|
part*: string
|
|
|
|
|
|
|
|
type ParsingResult*[T] = Result[T, ParsingError]
|
|
|
|
|
|
|
|
proc invalidFormat*(T: type ParsingError, cause = "invalid format"): T =
|
|
|
|
ParsingError(kind: ParsingErrorKind.InvalidFormat, cause: cause)
|
|
|
|
|
|
|
|
proc missingPart*(T: type ParsingError, part = "unknown"): T =
|
|
|
|
ParsingError(kind: ParsingErrorKind.MissingPart, part: part)
|
|
|
|
|
|
|
|
proc `$`*(err: ParsingError): string =
|
2024-03-15 23:08:47 +00:00
|
|
|
case err.kind
|
2023-03-07 10:10:36 +00:00
|
|
|
of ParsingErrorKind.InvalidFormat:
|
|
|
|
return "invalid format: " & err.cause
|
|
|
|
of ParsingErrorKind.MissingPart:
|
|
|
|
return "missing part: " & err.part
|