Trusted.Env (Cure Standard Library v0.38.0)

View Source

Environment variables, for the trusted (unproved) standard library.

Why this module exists

Reading a process environment cannot be proved total: the variable may be absent, and OTP converts its native bytes to Unicode text before returning it. Std.* therefore has no access to it, and every program that needs a configuration value used to re-declare the same @externs.

Values follow :os.getenv/1's decoding, including its native-encoding fallback. This API cannot detect invalid UTF-8 in the original OS bytes; re-encoding an already decoded charlist would not recover that distinction.

The trust contract

This module satisfies the four rules every Trusted.* module must meet (see docs/TRUSTED.md):

  1. Total surface. No public function returns a bare Tuple or Binary. A missing variable answers None(); get_checked/1 answers Error(IoError(:badarg)) for an empty name, =, or NUL.
  2. Declared boundary. Every @extern a public function reaches carries a ## Trust: block naming the OTP contract relied on.
  3. No proof claims. Nothing here is a proof.
  4. Reviewable axiom set. cure audit trust Trusted.Env snapshots the reachable externs.

Examples

mod EnvExample
  use Std.Option
  use Trusted.Env

  fn home_or_default() -> String =
    match get("HOME")
      Some(value) -> value
      None()      -> "/tmp"
mod EnvCheckedExample
  use Std.Result
  use Std.Option
  use Trusted.Env
  use Trusted.Error

  fn describe_var(name: String) -> String =
    match get_checked(name)
      Ok(Some(value)) -> value
      Ok(None())      -> "unset"
      Error(reason)   -> describe(reason)

Summary

Functions

Collect the name of each "NAME=value" entry, in reverse order.

Read an environment variable as Option(String).

Read an environment variable, distinguishing "unset" from an invalid name.

true when the variable is set, whatever its value. Invalid names are false.

Insert s into an already-sorted list, preserving order.

The name part of a "NAME=value" entry: every code point before the FIRST =.

Every variable name, sorted.

Trust: :os.getenv/0 answers every variable as a list of flat strings of the form "NAME=value", both fields charlists, with no absent-value arm -- absence is represented by the variable not appearing at all.

Trust: :cure_std_trusted_env.read/1 validates the name, then calls :os.getenv/1 exactly once and decodes that captured charlist-or-false reply into Result(Option(List(Char)), Atom). Empty names, =, NUL, and OTP argument errors answer Error(:badarg). An empty value remains Some([]), distinct from None(). Text follows OTP's Unicode/native decoding; there is no claim about the original bytes' UTF-8 validity. The helper lives in lib/cure/trusted/env.ex; its only effectful OTP dependency is :os.getenv/1, not a second read or an unchecked cast.

Functions

env_names(entries: List(List(Char)), acc: List(String)) -> List(String)

 fn env_names(entries: List(List(Char)), acc: List(String)) -> List(String)

Collect the name of each "NAME=value" entry, in reverse order.

get(name: String) -> Option(String)

 fn get(name: String) -> Option(String)

Read an environment variable as Option(String).

None() means the variable is unset or the name is invalid. An empty value answers Some("") -- the two are different states, and conflating them makes a deliberately-empty setting indistinguishable from a missing one.

Examples

mod GetEnvExample
  use Std.Option
  use Trusted.Env

  fn home() -> String =
    match get("HOME")
      Some(value) -> value
      None()      -> "/tmp"

get_checked(name: String) -> Result(Option(String), FileError)

 fn get_checked(name: String) -> Result(Option(String), FileError)

Read an environment variable, distinguishing "unset" from an invalid name.

The Result-returning counterpart of get/1. Empty names and names containing = or NUL answer Error(IoError(:badarg)), not an exception. Values are OTP-decoded text, not a validation of the original OS bytes.

Examples

mod GetCheckedExample
  use Std.Result
  use Std.Option
  use Trusted.Env
  use Trusted.Error

  fn describe_var(name: String) -> String =
    match get_checked(name)
      Ok(Some(value)) -> value
      Ok(None())      -> "unset"
      Error(reason)   -> describe(reason)

has?(name: String) -> Bool

 fn has?(name: String) -> Bool

true when the variable is set, whatever its value. Invalid names are false.

Examples

mod HasEnvExample
  use Trusted.Env

  fn has_home() -> Bool = has?("HOME")

insert_string(s: String, sorted: List(String)) -> List(String)

 fn insert_string(s: String, sorted: List(String)) -> List(String)

Insert s into an already-sorted list, preserving order.

name_of_entry(remaining: List(Char), acc: List(Char)) -> List(Char)

 fn name_of_entry(remaining: List(Char), acc: List(Char)) -> List(Char)

The name part of a "NAME=value" entry: every code point before the FIRST =.

Total: an entry with no = (which :os.getenv/0 does not produce, but a defensive reading costs nothing) answers the whole entry as its name.

names() -> List(String)

 fn names() -> List(String)

Every variable name, sorted.

Examples

mod EnvNamesExample
  use Std.List
  use Trusted.Env

  fn count() -> Int = Std.List.length(names())

raw_getenv_all() -> List(List(Char))

 fn raw_getenv_all() -> List(List(Char))

Trust: :os.getenv/0 answers every variable as a list of flat strings of the form "NAME=value", both fields charlists, with no absent-value arm -- absence is represented by the variable not appearing at all.

The =value suffix is split off by name_of_entry/1; a value may itself contain =, so only the FIRST separator is significant.

raw_read_env(name: List(Char)) -> Result(Option(List(Char)), Atom)

 fn raw_read_env(name: List(Char)) -> Result(Option(List(Char)), Atom)

Trust: :cure_std_trusted_env.read/1 validates the name, then calls :os.getenv/1 exactly once and decodes that captured charlist-or-false reply into Result(Option(List(Char)), Atom). Empty names, =, NUL, and OTP argument errors answer Error(:badarg). An empty value remains Some([]), distinct from None(). Text follows OTP's Unicode/native decoding; there is no claim about the original bytes' UTF-8 validity. The helper lives in lib/cure/trusted/env.ex; its only effectful OTP dependency is :os.getenv/1, not a second read or an unchecked cast.

reverse_chars(remaining: List(Char), acc: List(Char)) -> List(Char)

 fn reverse_chars(remaining: List(Char), acc: List(Char)) -> List(Char)

Reverse a code-point list.

sort_strings(remaining: List(String), acc: List(String)) -> List(String)

 fn sort_strings(remaining: List(String), acc: List(String)) -> List(String)

Insertion sort over String.