### Define Basic Pkl Configuration Source: https://github.com/z-jxy/rpkl/blob/main/README.md Illustrates a simple Pkl configuration file defining an IP address and nested database credentials. This serves as a foundational example for Pkl's declarative syntax. ```pkl ip = "127.0.0.1" database { username = "admin" password = "secret" } ``` -------------------------------- ### Example of Generated Rust Opaque Field Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Shows the resulting Rust code when the `--opaque` option is applied, demonstrating how a field is declared with the `rpkl::Value` type. ```rust pub dynamic_config: rpkl::Value ``` -------------------------------- ### Example Pkl Module Definition Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md A sample Pkl module demonstrating various data types including strings, integers, lists, anonymous maps, and union types, which serve as input for the rpkl codegen process. ```Pkl ip = "127.0.0.1" port = 8080 birds: Listing = new { "Pigeon" "Hawk" "Penguin" } anon_map = new { ["anon_key"] = "anon_value" ["anon_key2"] = "anon_value2" } mode: "Dev" | "Production" = "Dev" ``` -------------------------------- ### Pkl String Literal Constraint Example Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md An example of how Pkl can enforce constraints on string values, which `rpkl-cli` can then use to generate Rust enums. ```pkl mode: "Dev" | "Production" ``` -------------------------------- ### Example of Generated Rust Enum and Struct Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Provides the Rust code output that results from using the `--as-enum` option, demonstrating how a Pkl string constraint is transformed into a Rust enum and integrated into a struct. ```rust #[derive(Debug, Clone, serde::Deserialize)] pub struct Config { pub mode: Mode, // other fields... } #[derive(Debug, Clone, serde::Deserialize)] pub enum Mode { Dev, Production, } ``` -------------------------------- ### Generate Rust Code from Pkl Module Source: https://github.com/z-jxy/rpkl/blob/main/README.md Provides an example of using the `rpkl` library to evaluate a Pkl module and generate Rust code from it. This feature, enabled by the `codegen` feature, allows for programmatic generation of Rust structs or other code based on Pkl definitions. ```rust use rpkl::{api::Evaluator, codegen::CodegenOptions}; fn main() -> Result<(), Box> { let mut evaluator = Evaluator::new()?; let pkl_mod = evaluator.evaluate_module("example.pkl")?; let code: String = pkl_mod.codegen()?; std::fs::write("src/example.rs", code)?; Ok(()) } ``` -------------------------------- ### Generated Rust Structs from Pkl Module Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md The Rust code generated by rpkl codegen for the example Pkl module. It shows how Pkl types are mapped to Rust types, including nested structs for anonymous maps and handling of string fields. ```Rust #[derive(Debug, ::serde::Deserialize)] pub struct Example { pub ip: String, pub port: i64, pub birds: Vec, pub anon_map: example::AnonMap, pub mode: String, } pub mod example { #[derive(Debug, ::serde::Deserialize)] pub struct AnonMap { #[serde(rename = "anon_key2")] pub anon_key_2: String, pub anon_key: String, } } ``` -------------------------------- ### Basic Usage of rpkl-cli Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Demonstrates the fundamental command to evaluate a Pkl file and output the generated Rust code to standard output. ```bash rpkl path/to/your/config.pkl ``` -------------------------------- ### Define Pkl Configuration with External Properties Source: https://github.com/z-jxy/rpkl/blob/main/README.md Demonstrates how to define Pkl properties that can be read from external sources, typically passed during evaluation. This allows for dynamic configuration values injected at runtime. ```pkl username = read("prop:username") password = read("prop:password") ``` -------------------------------- ### Deserialize Pkl Configuration in Rust Source: https://github.com/z-jxy/rpkl/blob/main/README.md Shows how to define Rust structs that mirror the structure of a Pkl configuration file. The `rpkl::from_config` function is then used to load and deserialize the Pkl configuration into the corresponding Rust types. ```rust #[derive(Deserialize)] struct Config { ip: String, database: Database, } #[derive(Deserialize)] struct Database { username: String, password: String, } let config: Config = rpkl::from_config("./config.pkl")?; ``` -------------------------------- ### Evaluate Pkl with Custom Options in Rust Source: https://github.com/z-jxy/rpkl/blob/main/README.md Explains how to pass evaluator options, such as properties, to the Pkl evaluation process using `rpkl::from_config_with_options` in Rust. This enables injecting dynamic values into Pkl configurations. ```rust let options = EvaluatorOptions::default() .properties([("username", "root"), ("password", "password123")]); let config: Config = rpkl::from_config_with_options("./config.pkl", Some(options))?; ``` -------------------------------- ### Generating Rust Enums with Combined Attributes Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Demonstrates how to combine `--as-enum` with `--type-attribute` and `--field-attribute` to generate a Rust enum and its variants with custom attributes. ```bash rpkl \ --as-enum "Config.mode=Dev,Production" \ --type-attribute "Mode=#[derive(Default)]" \ --field-attribute "Mode.Dev=#[default]" \ input.pkl ``` -------------------------------- ### Generating Rust Enums from Pkl String Constraints Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Explains how to use the `--as-enum` option to instruct `rpkl-cli` to generate a Rust enum for a string field that has constrained values in Pkl. ```bash rpkl --as-enum "Config.mode=Dev,Production" input.pkl ``` -------------------------------- ### Generating Opaque Type Fields in Rust Structs Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Illustrates the use of the `--opaque` option to generate a field as a generic `rpkl::Value` instead of a fully defined struct, useful for dynamic or unknown map structures. ```bash rpkl --opaque "Config.dynamicSettings" input.pkl ``` -------------------------------- ### Configure Pkl String Constraints as Rust Enums Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md Illustrates how to use the `as_enum` option in `CodegenOptions` to instruct rpkl codegen to generate a Rust enum from a Pkl field that uses string constraints (e.g., a union type), instead of a plain `String`. ```Rust .as_enum("Config.mode", &["Dev", "Production"]) ``` -------------------------------- ### Adding Custom Attributes to Generated Rust Structs Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Illustrates how to use the `--type-attribute` option to add custom attributes, such as `#[derive(Default)]`, to a specific generated Rust struct type. ```bash rpkl --type-attribute "Config=#[derive(Default)]" input.pkl ``` -------------------------------- ### Generate Opaque Type Fields for Pkl Maps Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md Demonstrates using the `opaque` option in `CodegenOptions` to treat a Pkl map or mapping as a generic `rpkl::Value` type in the generated Rust code, rather than generating a concrete struct for its fields. ```Rust .opaque("Config.dynamicSettings") ``` -------------------------------- ### Adding Custom Attributes to Generated Rust Struct Fields Source: https://github.com/z-jxy/rpkl/blob/main/crates/cli/README.md Shows how to apply the `--field-attribute` option to add attributes, like `#[default]`, to a specific field within a generated Rust struct or enum variant. ```bash rpkl --field-attribute "Mode.Dev=#[default]" input.pkl ``` -------------------------------- ### Generated Rust Enum from Pkl String Constraint Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md Shows the resulting Rust code structure when a Pkl string constraint is configured to be generated as a Rust enum. The Pkl field is replaced by the new enum type, and the enum definition is included. ```Rust #[derive(Debug, ::serde::Deserialize)] pub struct Config { pub mode: Mode, // other fields... } #[derive(Debug, ::serde::Deserialize)] pub enum Mode { Dev, Production, } ``` -------------------------------- ### Add Custom Type Attributes to Generated Rust Structs Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md Demonstrates how to use the `type_attribute` option in `CodegenOptions` to add custom attributes, such as `#[derive(Clone)]`, to a specific generated Rust struct type. This allows for further customization of the generated code. ```Rust .type_attribute("Example", "#[derive(Clone)]") ``` -------------------------------- ### Add Custom Field Attributes to Generated Rust Fields Source: https://github.com/z-jxy/rpkl/blob/main/docs/codegen.md Explains how to use the `field_attribute` option in `CodegenOptions` to add custom attributes to specific fields or enum variants within generated Rust structs or enums, such as `#[default]` for an enum variant. ```Rust .field_attribute("Mode.Dev", "#[default]") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.