Skip to content

LechintanTudor/serde_ccl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serde CCL

Crates.io Documentation

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

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.

About

Serde-based crate for deserializing CCL Documents.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages