2024-06-28 16:04:57 +05:30
|
|
|
{.push raises: [].}
|
2023-03-07 11:10:36 +01:00
|
|
|
|
2024-07-09 13:14:28 +02:00
|
|
|
import results
|
2023-03-07 11:10:36 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
ParsingErrorKind* {.pure.} = enum
|
2024-03-16 00:08:47 +01:00
|
|
|
InvalidFormat
|
2023-03-07 11:10:36 +01: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-16 00:08:47 +01:00
|
|
|
case err.kind
|
2023-03-07 11:10:36 +01:00
|
|
|
of ParsingErrorKind.InvalidFormat:
|
|
|
|
return "invalid format: " & err.cause
|
|
|
|
of ParsingErrorKind.MissingPart:
|
|
|
|
return "missing part: " & err.part
|