Thanks to visit codestin.com
Credit goes to docs.rs

Skip to main content

CclObject

Struct CclObject 

Source
pub struct CclObject(/* private fields */);
Expand description

Represents a parsed CCL document as a recursive map structure

Following the OCaml implementation: type entry_map = value_entry list KeyMap.t

A CCL document is a fixed-point recursive structure where:

  • Every Model is a map from String to Vec<Model>
  • An empty map {} represents a leaf/terminal value
  • String values are encoded in the recursive structure
  • Lists are represented as multiple entries with the same key
  • Uses IndexMap to preserve insertion order (keys ordered by first appearance)
  • Uses Vec to preserve order of values for each key (insertion order)

Implementations§

Source§

impl CclObject

Source

pub fn new() -> Self

Create a new empty model

Source

pub fn get(&self, key: &str) -> Result<&CclObject>

Get a value by key, returning an error if the key doesn’t exist

If the key has multiple values, returns the first one (matching OCaml behavior). Use get_all() to get all values for a key.

Source

pub fn get_all(&self, key: &str) -> Result<&[CclObject]>

Get all values for a key, returning an error if the key doesn’t exist

Source

pub fn keys(&self) -> impl Iterator<Item = &String>

Get an iterator over the keys in this model

Source

pub fn values(&self) -> impl Iterator<Item = &CclObject>

Get an iterator over the first value for each key

This flattens the Vec structure, returning only the first value per key. Use iter_all() to get all values.

Source

pub fn iter(&self) -> impl Iterator<Item = (&String, &CclObject)>

Get an iterator over key-value pairs (first value only per key)

This flattens the Vec structure, returning only the first value per key. Use iter_all() to get all key-value pairs including duplicates.

Source

pub fn iter_all(&self) -> impl Iterator<Item = (&String, &CclObject)>

Get an iterator over all key-value pairs including duplicate keys

Source

pub fn len(&self) -> usize

Get the number of entries in this model

Source

pub fn is_empty(&self) -> bool

Check if this model is empty

Source

pub fn inner_mut(&mut self) -> &mut IndexMap<String, Vec<CclObject>>

Get mutable access to the internal IndexMap for direct manipulation

This allows programmatic construction of CCL structures when you need full control over the data model.

§Example
use sickle::CclObject;

let mut obj = CclObject::new();
let map = obj.inner_mut();
map.insert("key".to_string(), vec![CclObject::from_string("value")]);
Source

pub fn empty() -> Self

Create an empty CclObject (represents an empty value in CCL: key =)

§Example
use sickle::CclObject;

let empty = CclObject::empty();
// Represents: key =
Source

pub fn from_list(items: Vec<impl Into<String>>) -> Self

Create a CclObject representing a list using bare list syntax

In CCL, a list is represented using the same empty key with multiple values. Now that we use Vec<CclObject> internally, we can properly support duplicate keys.

§Example
use sickle::CclObject;

let list = CclObject::from_list(vec!["brew", "scoop", "pacman"]);
// Represents:
// packages =
//   = brew
//   = scoop
//   = pacman
Source

pub fn add_comment(&mut self, text: impl Into<String>)

Add a comment entry to this CclObject

CCL comments use the /= prefix followed by the comment text as the key, with an empty value. This method adds a comment entry directly to the object.

§Example
use sickle::CclObject;

let mut obj = CclObject::new();
obj.add_comment("Generated file - do not edit");
// When printed, represents: /= Generated file - do not edit
Source

pub fn add_blank_line(&mut self)

Add a blank line entry to this CclObject

Blank lines in CCL output are represented as entries with an empty key and empty value. This is useful for visual separation in generated files.

§Example
use sickle::CclObject;

let mut obj = CclObject::new();
obj.add_comment("Header section");
obj.add_blank_line();
// When printed, adds visual separation
Source

pub fn compose(&self, other: &CclObject) -> CclObject

Compose two CCL objects together (monoid binary operation)

This implements the fundamental CCL composition operation that makes CCL a monoid. When composing two objects:

  • Keys unique to either object are preserved
  • Keys present in both are recursively composed
  • The empty object is the identity element
§Algebraic Properties
  • Associativity: a.compose(&b).compose(&c) == a.compose(&b.compose(&c))
  • Left Identity: CclObject::new().compose(&x) == x
  • Right Identity: x.compose(&CclObject::new()) == x
§Example
use sickle::{load, CclObject};

let a = load("config =\n  host = localhost").unwrap();
let b = load("config =\n  port = 8080").unwrap();
let composed = a.compose(&b);
// Result: config = { host = localhost, port = 8080 }
Source

pub fn compose_associative(a: &CclObject, b: &CclObject, c: &CclObject) -> bool

Check if composing three objects is associative

Tests: (a ∘ b) ∘ c == a ∘ (b ∘ c)

This is used for testing the algebraic properties of CCL.

Source

pub fn identity_left(x: &CclObject) -> bool

Check left identity property

Tests: empty ∘ x == x

Source

pub fn identity_right(x: &CclObject) -> bool

Check right identity property

Tests: x ∘ empty == x

Source

pub fn get_string(&self, key: &str) -> Result<&str>

Get a string value by key

Looks up the key and extracts its string representation

Source

pub fn get_bool(&self, key: &str) -> Result<bool>

Get a boolean value by key (strict mode)

Only accepts “true” and “false”. For lenient parsing that also accepts “yes” and “no”, use get_bool_lenient().

Source

pub fn get_bool_with_options( &self, key: &str, options: BoolOptions, ) -> Result<bool>

Get a boolean value by key with options

Allows configuring boolean parsing behavior.

Source

pub fn get_bool_lenient(&self, key: &str) -> Result<bool>

Get a boolean value by key (lenient mode)

Accepts “true”, “false”, “yes”, and “no”. For strict parsing, use get_bool().

Source

pub fn get_int(&self, key: &str) -> Result<i64>

Get an integer value by key

Source

pub fn get_float(&self, key: &str) -> Result<f64>

Get a float value by key

Source

pub fn get_list(&self, key: &str) -> Result<Vec<String>>

Get a list of string values by key (reference-compliant behavior)

Only bare list syntax produces lists. Duplicate keys with values are NOT treated as lists.

For typed access to lists of scalars, use get_list_typed::<T>() instead. For coercion behavior, use get_list_coerced().

Source

pub fn get_list_coerced(&self, key: &str) -> Result<Vec<String>>

Get a list of string values by key (with coercion)

Duplicate keys are coerced into lists, and scalar literals are filtered. When multiple entries exist for the same key (e.g., servers = web1\nservers = web2), all values are collected into a single list.

For typed access to lists of scalars, use get_list_typed::<T>() instead. For reference-compliant behavior, use get_list().

Source

pub fn get_list_typed<T>(&self, key: &str) -> Result<Vec<T>>
where T: FromStr, T::Err: Display,

Get a typed list of values by key

This method provides generic access to lists of any parseable type. Unlike get_list(), this doesn’t filter scalar literals - it parses all keys as type T.

§Examples
// Numbers list
let input = "numbers = 1\nnumbers = 42\nnumbers = -17";
let entries = parse(input)?;
let model = build_hierarchy(&entries)?;
let numbers: Vec<i64> = model.get_list_typed("numbers")?;
assert_eq!(numbers, vec![1, 42, -17]);

// Booleans list
let input = "flags = true\nflags = false";
let entries = parse(input)?;
let model = build_hierarchy(&entries)?;
let flags: Vec<bool> = model.get_list_typed("flags")?;
assert_eq!(flags, vec![true, false]);
§Errors

Returns Error::ValueError if any key cannot be parsed as type T.

Source

pub fn from_string(s: impl Into<String>) -> Self

Create a CclObject representing a string value

In CCL, a string is represented as a map with a single key (the string) and an empty value: {"string_value": [{}]}

§Example
use sickle::CclObject;

let val = CclObject::from_string("hello");
// Represents: key = hello

Trait Implementations§

Source§

impl Clone for CclObject

Source§

fn clone(&self) -> CclObject

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CclObject

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CclObject

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for CclObject

Source§

fn eq(&self, other: &CclObject) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for CclObject

Source§

impl StructuralPartialEq for CclObject

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.