ParseError v0.1.0

Source
Why a string could not be parsed into a value.

Package: Format

Definition

enum ParseError {
    Empty,
    InvalidCharacter(uint),
    Overflow
}

The error half of the Result that ParseInt64 and ParseFloat64 return. The TryParse functions collapse it to a bool, so it only surfaces from the Parse functions.

Variants

VariantMeaning
EmptyThe input had no bytes.
InvalidCharacter(uint)A byte that does not fit the grammar; the payload is its index.
OverflowThe value is outside the range the target type can hold.

The index carried by InvalidCharacter points at the offending byte — the first non-digit, a lone sign, or an exponent marker with no digits after it — which is enough to report where the input went wrong.

Example

import Format::{ ParseInt64, ParseError };
import Io::PrintLine;

func Main() -> int {
    match ParseInt64("12x4") {
        .Success(n) => PrintLine(n),
        .Error(e) => match e {
            .Empty => PrintLine("empty"),
            .InvalidCharacter(i) => PrintLine(i),   // 2 -- the 'x'
            .Overflow => PrintLine("overflow")
        }
    }
    return 0;
}

See also