Home / Docs / Standard library / Prelude
ParseError
Release v0.4.0Edition 0.2 and later
A kind of the prelude, in scope in every module. Edition 0.2 and later.
una
pub kind ParseError = Empty | NotADigit(at: Int) | NoDigits | TooLongWhy parse_int fails (spec/22 §4).
Variants
Empty: the text is emptyNotADigit(at): a character is not a digit;atis the scalar position, from 0, of the first character that is neither the leading-nor an ASCII digitNoDigits: the text is-aloneTooLong: the text writes more than 4300 digits, the sign excluded and leading zeros included
See also
Example
src/shop.una
module shop
--- A stock adjustment typed by hand, and why it is not one. A sign is allowed, so `-3`
--- writes off three units.
adjustment(raw: Text) -> Text
=
let outcome = attempt parse_int(raw)
when outcome
is Ok(n) -> "'{raw}': adjust the stock by {n}"
is Err(failure) -> "'{raw}': {reason(failure)}"
reason(failure: ParseError) -> Text
=
when failure
is Empty -> "nothing typed"
is NoDigits -> "a sign and no digits"
is NotADigit(at) -> "not a digit at {at}"
is TooLong -> "more digits than an integer may have"
pub main() -> Unit
uses @console: Console[print]
=
@console.print(adjustment("12"))
@console.print(adjustment("-3"))
@console.print(adjustment(""))
@console.print(adjustment("-"))
@console.print(adjustment("1x2"))una run . prints:
Output
'12': adjust the stock by 12
'-3': adjust the stock by -3
'': nothing typed
'-': a sign and no digits
'1x2': not a digit at 1Specification
Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.