rpkl 0.8.0

Bindings and codegen for Apple's Pkl configuration language
Documentation
use std::path::PathBuf;

/* Generated by rpkl */

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Example {
    #[serde(rename = "ip")]
    pub ip: String,
    pub port: i64,
    pub ints: std::ops::Range<i64>,
    pub birds: Vec<rpkl::Value>,
    pub mapping: rpkl::Value,
    pub anon_map: example::AnonMap,
    pub database: example::Database,
    pub mode: example::Mode,
}

pub mod example {
    #[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
    pub struct AnonMap {
        pub anon_key: String,
        #[serde(rename = "anon_key2")]
        pub anon_key_2: String,
    }

    #[derive(Debug, serde::Deserialize, serde::Serialize)]
    pub struct Database {
        pub username: String,
        pub password: String,
    }

    #[derive(Default, Debug, serde::Deserialize, serde::Serialize)]
    pub enum Mode {
        #[default]
        Dev,
        Production,
    }
}

fn main() {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("pkl")
        .join("example.pkl");
    println!("{:?}", rpkl::from_config::<Example>(&path));

    #[cfg(feature = "codegen")]
    {
        let mut evaluator = rpkl::api::evaluator::Evaluator::new().unwrap();
        let pkl_mod = evaluator.evaluate_module(path).unwrap();
        let options = rpkl::codegen::CodegenOptions::default()
            .type_attribute("AnonMap", "#[derive(Default)]")
            .field_attribute("Example.ip", "#[serde(rename = \"ip\")]")
            .as_enum("Example.mode", &["Dev", "Production"])
            .type_attribute("Mode", "#[derive(Default)]")
            .field_attribute("Mode.Dev", "#[default]")
            .opaque("Example.mapping");
        println!("{}", pkl_mod.codegen_with_options(options).unwrap());
    }
}