prompty 2.0.0-beta.1

Prompty is an asset class and format for LLM prompts
Documentation
// Code generated by Prompty emitter; DO NOT EDIT.

#![allow(
    unused_imports,
    dead_code,
    non_camel_case_types,
    unused_variables,
    clippy::all
)]

use super::super::context::{LoadContext, SaveContext};

/// Variant-specific data for [`Property`], discriminated by `kind`.
#[derive(Debug, Clone)]
pub enum PropertyKind {
    /// `kind` = `"array"`
    Array {
        /// The type of items contained in the array
        items: serde_json::Value,
    },
    /// `kind` = `"object"`
    Object {
        /// The properties contained in the object
        properties: Vec<Property>,
    },
    /// Wildcard / catch-all variant for unrecognized `kind` values.
    Custom {
        /// The raw `kind` string for this unknown variant.
        kind_name: String,
    },
}

impl Default for PropertyKind {
    fn default() -> Self {
        PropertyKind::Custom {
            kind_name: String::new(),
        }
    }
}
/// Represents a single property. - This model defines the structure of properties that can be used in prompts, including their type, description, whether they are required, and other attributes. - It allows for the definition of dynamic inputs that can be filled with data and processed to generate prompts for AI models.
#[derive(Debug, Clone, Default)]
pub struct Property {
    /// Name of the property
    pub name: String,
    /// A short description of the input property
    pub description: Option<String>,
    /// Whether the property is required
    pub required: Option<bool>,
    /// The default value of the property - this represents the default value if none is provided
    pub default: Option<serde_json::Value>,
    /// Example value used for either initialization or tooling
    pub example: Option<serde_json::Value>,
    /// Allowed enumeration values for the property
    pub enum_values: Option<Vec<serde_json::Value>>,
    /// Variant-specific data, discriminated by `kind`.
    pub kind: PropertyKind,
}

impl Property {
    /// Create a new Property with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Load Property from a JSON string.
    pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
        let value: serde_json::Value = serde_json::from_str(json)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load Property from a YAML string.
    pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
        let value: serde_json::Value = serde_yaml::from_str(yaml)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load Property from a `serde_json::Value`.
    ///
    /// Calls `ctx.process_input` before field extraction.
    pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
        let value = ctx.process_input(value.clone());
        if let Some(value) = value.as_bool() {
            return Property {
                kind: PropertyKind::Custom {
                    kind_name: "boolean".to_string(),
                },
                example: Some(value.into()),
                ..Default::default()
            };
        }
        if let Some(value) = value.as_i64() {
            return Property {
                kind: PropertyKind::Custom {
                    kind_name: "integer".to_string(),
                },
                example: Some(value.into()),
                ..Default::default()
            };
        }
        if let Some(s) = value.as_str() {
            let value = s.to_string();
            return Property {
                kind: PropertyKind::Custom {
                    kind_name: "string".to_string(),
                },
                example: Some(value.into()),
                ..Default::default()
            };
        }
        let kind_str = value.get("kind").and_then(|v| v.as_str()).unwrap_or("");
        let kind = match kind_str {
            "array" => PropertyKind::Array {
                items: value
                    .get("items")
                    .cloned()
                    .unwrap_or(serde_json::Value::Null),
            },
            "object" => PropertyKind::Object {
                properties: value
                    .get("properties")
                    .map(|v| Self::load_properties(v, ctx))
                    .unwrap_or_default(),
            },
            _ => PropertyKind::Custom {
                kind_name: kind_str.to_string(),
            },
        };
        Self {
            name: value
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
            description: value
                .get("description")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            required: value.get("required").and_then(|v| v.as_bool()),
            default: value.get("default").cloned(),
            example: value.get("example").cloned(),
            enum_values: value
                .get("enumValues")
                .and_then(|v| v.as_array())
                .map(|arr| arr.to_vec()),
            kind: kind,
        }
    }

    /// Returns the `kind` discriminator string for this instance.
    pub fn kind_str(&self) -> &str {
        match &self.kind {
            PropertyKind::Array { .. } => "array",
            PropertyKind::Object { .. } => "object",
            PropertyKind::Custom { kind_name, .. } => kind_name.as_str(),
        }
    }

    /// Serialize Property to a `serde_json::Value`.
    ///
    /// Calls `ctx.process_dict` after serialization.
    pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
        let mut result = serde_json::Map::new();
        // Write the discriminator
        result.insert(
            "kind".to_string(),
            serde_json::Value::String(self.kind_str().to_string()),
        );
        // Write base fields
        if !self.name.is_empty() {
            result.insert(
                "name".to_string(),
                serde_json::Value::String(self.name.clone()),
            );
        }
        if let Some(ref val) = self.description {
            result.insert(
                "description".to_string(),
                serde_json::Value::String(val.clone()),
            );
        }
        if let Some(val) = self.required {
            result.insert("required".to_string(), serde_json::Value::Bool(val));
        }
        if let Some(ref val) = self.default {
            result.insert("default".to_string(), val.clone());
        }
        if let Some(ref val) = self.example {
            result.insert("example".to_string(), val.clone());
        }
        if let Some(ref items) = self.enum_values {
            result.insert(
                "enumValues".to_string(),
                serde_json::to_value(items).unwrap_or(serde_json::Value::Null),
            );
        }
        // Write variant-specific fields
        match &self.kind {
            PropertyKind::Array { items, .. } => {
                if !items.is_null() {
                    result.insert("items".to_string(), items.clone());
                }
            }
            PropertyKind::Object { properties, .. } => {
                if !properties.is_empty() {
                    result.insert(
                        "properties".to_string(),
                        serde_json::Value::Array(
                            properties.iter().map(|item| item.to_value(ctx)).collect(),
                        ),
                    );
                }
            }
            PropertyKind::Custom { kind_name: _, .. } => {}
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize Property to a JSON string.
    pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.to_value(ctx))
    }

    /// Serialize Property to a YAML string.
    pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(&self.to_value(ctx))
    }

    /// Load a collection of Property from a JSON value.
    /// Handles both array format `[{...}]`.
    fn load_properties(data: &serde_json::Value, ctx: &LoadContext) -> Vec<Property> {
        match data {
            serde_json::Value::Array(arr) => arr
                .iter()
                .map(|v| Property::load_from_value(v, ctx))
                .collect(),

            _ => Vec::new(),
        }
    }

    /// Save a collection of Property to a JSON value.
    fn save_properties(items: &[Property], ctx: &SaveContext) -> serde_json::Value {
        serde_json::Value::Array(
            items
                .iter()
                .map(|item| item.to_value(ctx))
                .collect::<Vec<_>>(),
        )
    }
}