toml_decode()

def toml_decode(toml_string)

Parse a TOML string into a Starlark dictionary.

This function takes a TOML-formatted string and converts it into a Starlark dictionary that you can easily work with. TOML is a human-readable configuration file format that maps well to Starlark data structures. The TOML must be valid or this function will raise an error.

Raises: Raises an error if the TOML string is malformed or invalid

Args

  • toml_string: string: A valid TOML-formatted string to parse

Returns

A Starlark dictionary representing the parsed TOML data

toml_encode()

def toml_encode(value, pretty)

Convert a dictionary into a TOML-formatted string.

This function serializes Starlark dictionaries and other values into TOML-formatted strings suitable for configuration files or output. By default, the output is compact. For human-readable formatting with better spacing and organization, use toml_encode_pretty() instead.

Raises: Raises an error if the value cannot be serialized to TOML

Args

  • value: The dictionary or Starlark value to encode as TOML
  • pretty: bool: If True, format with better spacing and organization (default: False)

Returns

A TOML-formatted string representation of the input value

toml_encode_compact()

def toml_encode_compact(value)

Convert a value into a compact TOML string without extra whitespace.

This is a convenience function for encoding with minimal whitespace, useful for efficient file storage or network transmission of configuration data. Equivalent to calling toml_encode(value, pretty=False).

Args

  • value: The dictionary or Starlark value to encode

Returns

A compact TOML string with minimal whitespace

toml_encode_pretty()

def toml_encode_pretty(value)

Convert a value into a formatted TOML string with proper indentation.

This is a convenience function for creating human-readable TOML output with proper indentation and section organization. This is ideal for configuration files that need to be read and edited by humans. Equivalent to calling toml_encode(value, pretty=True).

Args

  • value: The dictionary or Starlark value to encode

Returns

A formatted TOML string with indentation and newlines

toml_is_valid()

def toml_is_valid(toml_string)

Check whether a string is valid TOML without raising an error.

This is a quick validation helper: it returns True if the string parses as TOML and False otherwise. No decoded data is returned.

Args

  • toml_string: string: The string to validate.

Returns

True if the string is valid TOML, False otherwise.

toml_merge()

def toml_merge(base_dict, override_dict)

Merge two dictionaries, with values from override_dict replacing base_dict values.

This utility function performs a shallow merge of two dictionaries, commonly used when combining default configuration with user-provided overrides. Values from override_dict take precedence over those in base_dict. For deep merging of nested structures, consider applying this function recursively.

Args

  • base_dict: The base/default dictionary.
  • override_dict: The dictionary with overrides. Its values replace base_dict’s values.

Returns

A new dictionary with merged values where override_dict values take precedence.

toml_parse_string()

def toml_parse_string(content)

Parse a TOML formatted string into a dictionary.

This function is the original backwards-compatible name for parsing TOML strings. New code should use toml_decode() instead.

Raises: Raises an error if the TOML string is malformed or invalid

Args

  • content: string: The TOML-formatted string to parse

Returns

A dictionary representation of the TOML data

toml_to_string()

def toml_to_string(value)

Convert a dictionary to a TOML-formatted string.

This function is the original backwards-compatible name for encoding TOML. New code should use toml_encode() instead.

Raises: Raises an error if the value cannot be serialized to TOML

Args

  • value: The dictionary or Starlark value to serialize

Returns

The TOML string representation of the input value

toml_to_string_pretty()

def toml_to_string_pretty(value)

Convert a dictionary to a pretty-printed TOML-formatted string.

This function is the original backwards-compatible name for pretty encoding. New code should use toml_encode(value, pretty=True) or toml_encode_pretty() instead.

Raises: Raises an error if the value cannot be serialized to TOML

Args

  • value: The dictionary or Starlark value to serialize

Returns

The pretty-formatted TOML string representation

toml_try_decode()

def toml_try_decode(toml_string, default)

Attempt to decode a TOML string, returning a default value on failure.

This function provides safe TOML parsing with graceful error handling. If the string is not valid TOML, it returns the default value instead of raising an exception.

Args

  • toml_string: string: The TOML string to decode.
  • default: The value to return if decoding fails. Default is None.

Returns

The decoded dictionary if successful, or the default value if decoding fails.