cx

The “Context” data type exists to help create and read different types of data structures.

For example, to store a key and value:

let context = cx.dict()
   |> cx.add_string("first_name", first_name)

To retrieve that value:

cx.get_string(context, "first_name")

To store a nested key and value:

 let context =
   cx.dict()
   |> cx.add(
    "person",
    cx.dict()
      |> cx.add_string("first_name", first_name),
  )

To retrieve that value:

cx.get_string(context, "person.first_name")

Types

A recursive type for creating and reading nested objects (lists and dictionaries) that contain different combinations of types.

pub type Context {
  CBool(value: Bool)
  CString(value: String)
  CInt(value: Int)
  CFloat(value: Float)
  CDict(value: Dict(String, Context))
  CList(value: List(Context))
}

Constructors

  • CBool(value: Bool)
  • CString(value: String)
  • CInt(value: Int)
  • CFloat(value: Float)
  • CDict(value: Dict(String, Context))
  • CList(value: List(Context))

Errors that can occur when reading values from the “context” (a data structure).

pub type ContextError {
  KeyNotFound(key: String)
  ValueNotFound(key: String)
  UnexpectedContextType(key: String)
}

Constructors

  • KeyNotFound(key: String)
  • ValueNotFound(key: String)
  • UnexpectedContextType(key: String)

Functions

pub fn add(
  context: Context,
  key: String,
  value: Context,
) -> Context

Add a value, of any type, under a string key, to the “context” (a data structure).

pub fn add_bool(
  context: Context,
  key: String,
  value: Bool,
) -> Context

Add a boolean value, stored under a certain key, to the “context” (a data structure).

pub fn add_float(
  context: Context,
  key: String,
  value: Float,
) -> Context

Add a float value, stored under a certain key, to the “context” (a data structure).

pub fn add_int(
  context: Context,
  key: String,
  value: Int,
) -> Context

Add an integer value, stored under a certain key, to the “context” (a data structure).

pub fn add_list(
  context: Context,
  key: String,
  value: List(Context),
) -> Context

Add a list of values, of any type, under a string key, to the “context” (a data structure).

pub fn add_string(
  context: Context,
  key: String,
  value: String,
) -> Context

Add a string value, stored under a certain key, to the “context” (a data structure).

pub fn add_strings(
  context: Context,
  key: String,
  values: List(String),
) -> Context

Store a list of strings, under a string key, in the “context” (a data structure).

pub fn dict() -> Context

Create a data structure to store key-value pairs, where the key is a String.

pub fn get(
  context: Context,
  key: String,
) -> Result(Context, ContextError)

Get a value, of any type, stored under a certain key, from the “context” (a data structure).

pub fn get_bool(
  context: Context,
  key: String,
) -> Result(Bool, ContextError)

Get a boolean value, stored under a string key, from the “context” (a data structure).

pub fn get_list(
  context: Context,
  key: String,
) -> Result(List(Context), ContextError)

Returns a list of values, of varying types, stored under a string key in the “context” (a data structure).

pub fn get_string(
  context: Context,
  key: String,
) -> Result(String, ContextError)

Get a string value, stored under a string key, from the “context” (a data structure).

pub fn get_strings(
  context: Context,
  key: String,
) -> Result(List(String), ContextError)

Get a list of strings stored in the context under a given string key.

Search Document