3 releases
| 0.1.2 | Nov 2, 2025 |
|---|---|
| 0.1.1 | Mar 4, 2025 |
| 0.1.0 | Jan 30, 2025 |
#231 in Configuration
229 downloads per month
30KB
782 lines
Serde CCL
Serde-based crate for deserializing CCL Documents.
Example
CCL document named example.ccl.
imports =
= ~/.config/terminal/theme.ccl
= ~/.config/terminal/font.ccl
dynamic_title = false
font_size = 12
shell = tmux new-session -A -s main
Code to deserialize the CCL document.
use serde::Deserialize;
const DOCUMENT = include_str!("example.ccl");
#[derive(Debug, Deserialize)]
struct Config {
imports: Vec<String>,
dynamic_title: bool,
font_size: f64,
shell: String,
}
fn main() {
let config = serde_ccl::from_str::<Config>(DOCUMENT).unwrap();
println!("{config:?}");
}
Other Examples
Deserializing Arrays
Arrays are deserialized as key-value pairs where the key is empty. Non-empty keys are ignored.
use serde::Deserialize;
const CCL: &str = r"
values =
= 0
ignored = 1
= 1
";
#[derive(Deserialize)]
struct Data {
values: Vec<i32>,
}
fn main() {
let data = serde_ccl::from_str::<Data>(CCL).unwrap();
assert_eq!(data.values, &[0, 1]);
}
Deserializing Enums
Enums are deserialized as key-value pairs where the key is the variant name and the value is the payload.
use serde::Deserialize;
const CCL: &str = r"
none =
None =
rgb =
Rgb =
= 10
= 20
= 30
";
#[derive(Deserialize)]
struct Data {
none: Color,
rgb: Color,
}
#[derive(Deserialize)]
enum Color {
None,
Rgb(u8, u8, u8),
}
fn main() {
let data = serde_ccl::from_str::<Data>(CCL).unwrap();
assert!(matches!(data.none, Color::None));
assert!(matches!(data.rgb, Color::Rgb(10, 20, 30)));
}
Deserializing Unit Enums
For enums containing only unit variants it's more convenient to deserialize them from strings instead of key-value pairs. This can be achieved using a simple macro.
use serde::Deserialize;
macro_rules! define_enum {
($Name:ident { $($Variant:ident => $repr:literal,)* }) => {
#[derive(Deserialize)]
#[serde(try_from = "&str")]
pub enum $Name {
$($Variant,)*
}
impl TryFrom<&str> for $Name {
type Error = &'static str;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Ok(match s {
$($repr => Self::$Variant,)*
_ => return Err("invalid variant"),
})
}
}
};
}
const CCL: &str = r"
theme = light
";
define_enum!(Theme {
Light => "light",
Dark => "dark",
});
#[derive(Deserialize)]
struct Data {
theme: Theme,
}
#[test]
fn test_enum_from_str() {
let data = serde_ccl::from_str::<Data>(CCL).unwrap();
assert!(matches!(data.theme, Theme::Light));
License
serde_ccl is dual-licensed under either
-
MIT License (LICENSE-MIT or https://opensource.org/license/mit/)
-
Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above without any additional terms or conditions.
Dependencies
~150–465KB