envie/decode

Types

A decoder that converts a raw string value into a typed result.

pub type Decoder(a) {
  Decoder(run: fn(String) -> Result(a, String))
}

Constructors

  • Decoder(run: fn(String) -> Result(a, String))

Values

pub fn bool() -> Decoder(Bool)

Parse a boolean. Accepts (case-insensitive): true/yes/1/on → True; false/no/0/off → False.

pub fn float() -> Decoder(Float)

Parse a float.

pub fn float_range(
  min min: Float,
  max max: Float,
) -> Decoder(Float)

Parse a float that must be within [min, max].

pub fn int() -> Decoder(Int)

Parse an integer.

pub fn int_list(
  separator separator: String,
) -> Decoder(List(Int))

Split a string by a separator and parse each element as an integer.

pub fn int_range(min min: Int, max max: Int) -> Decoder(Int)

Parse an integer that must be within [min, max].

pub fn map(decoder: Decoder(a), f: fn(a) -> b) -> Decoder(b)

Transform the decoded value with a pure function.

pub fn non_empty_string() -> Decoder(String)

Require a non-empty (after trimming) string.

pub fn one_of(allowed: List(String)) -> Decoder(String)

Accept only values that appear in allowed (case-sensitive).

pub fn port() -> Decoder(Int)

Parse a TCP/UDP port number (1–65 535).

pub fn string() -> Decoder(String)

Accept any string as-is.

pub fn string_list(
  separator separator: String,
) -> Decoder(List(String))

Split a string by a separator into a list of trimmed, non-empty strings.

pub fn string_prefix(prefix: String) -> Decoder(String)

Require that the string starts with a given prefix.

pub fn then(
  decoder: Decoder(a),
  f: fn(a) -> Result(b, String),
) -> Decoder(b)

Chain a decoder with a fallible transformation.

pub fn url() -> Decoder(uri.Uri)

Parse a valid URI. Note that this is very permissive (accepts “localhost”, etc.). Use url_with_scheme or web_url for stricter validation.

pub fn url_with_scheme(schemes: List(String)) -> Decoder(uri.Uri)

Parse a valid URI that must have one of requested schemes (e.g., [“https”]).

pub fn validated(
  decoder: Decoder(a),
  validator: fn(a) -> Result(a, String),
) -> Decoder(a)

Run an additional validation on the decoded value.

pub fn web_url() -> Decoder(uri.Uri)

Shortcut for url_with_scheme(["http", "https"]).

Search Document