ryo-app 0.1.0

[preview] Application layer for RYO - Project management, Intent handling, API
Documentation
//! Intent/Goal schema definitions for fuzzy JSON repair
//!
//! This module defines the schema for Intent and Goal types used by
//! `ryo-fuzzy-parser` for automatic typo correction in LLM-generated JSON.

use ryo_fuzzy_parser::TaggedEnumSchema;

/// Valid Intent type discriminators (tag values)
pub static INTENT_TYPES: &[&str] = &[
    // Rename
    "RenameIdent",
    // Structure changes
    "ChangeVisibility",
    "MoveItem",
    "ExtractTrait",
    "InlineTrait",
    // Module operations (Note: AddMod was consolidated into CreateMod)
    "RemoveMod",
    "CreateMod",
    // Field operations
    "AddField",
    "RemoveField",
    // Derive operations
    "AddDerive",
    "RemoveDerive",
    // Enum operations
    "AddEnum",
    "AddVariant",
    "RemoveVariant",
    "AddMatchArm",
    "RemoveMatchArm",
    "ReplaceMatchArm",
    "AddStructLiteralField",
    "RemoveStructLiteralField",
    // Delete operations
    "RemoveStruct",
    "RemoveEnum",
    // Duplicate operations
    "DuplicateFunction",
    "DuplicateStruct",
    "DuplicateEnum",
    "DuplicateModTree",
    // Const/TypeAlias
    "AddConst",
    "AddTypeAlias",
    // Spec
    "AddSpec",
    // Method operations
    "AddMethod",
    "RemoveMethod",
    // Delete operations (additional)
    "RemoveConst",
    "RemoveTypeAlias",
    "RemoveUse",
    "RemoveTrait",
    "RemoveImpl",
    // Item operations
    "AddItem",
    "RemoveItem",
    "AddCode",
    // Idiom operations
    "OrganizeImports",
    "MergeImplBlocks",
    "LoopToIterator",
    "UnwrapToQuestion",
    "IntroduceVariable",
    "GenerateBuilder",
    // PureStmt/PureExpr operations
    "ReplaceExpr",
    "RemoveStatement",
    "InsertStatement",
    "ReplaceStatement",
    // Additional idiom transformations
    "AssignOp",
    "BoolSimplify",
    "CloneOnCopy",
    "CollapsibleIf",
    "ComparisonToMethod",
    "RedundantClosure",
    "ManualMap",
    "MatchToIfLet",
    // Custom/Plugin
    "Custom",
    #[cfg(feature = "wasm-plugin")]
    "Plugin",
];

/// Get valid fields for a given Intent type
pub fn get_intent_fields(tag: &str) -> Option<&'static [&'static str]> {
    match tag {
        // Rename
        "RenameIdent" => Some(&["from", "to", "kind"]),
        // Structure changes
        "ChangeVisibility" => Some(&["target", "to"]),
        "MoveItem" => Some(&["target", "to_module"]),
        "ExtractTrait" => Some(&["from_impl", "trait_name", "methods"]),
        "InlineTrait" => Some(&["trait_name", "struct_name", "remove_trait"]),
        // Module operations (Note: AddMod was consolidated into CreateMod)
        "RemoveMod" => Some(&["parent_mod", "mod_name"]),
        "CreateMod" => Some(&["parent_mod", "mod_name", "content", "is_pub"]),
        // Field operations
        "AddField" => Some(&["struct_name", "field_name", "field_type", "is_pub"]),
        "RemoveField" => Some(&["struct_name", "field_name"]),
        // Derive operations
        "AddDerive" => Some(&["target", "derives"]),
        "RemoveDerive" => Some(&["target", "derives"]),
        // Enum operations
        "AddEnum" => Some(&["name", "variants", "is_pub", "derives"]),
        "AddVariant" => Some(&["enum_name", "variant_name", "variant_type"]),
        "RemoveVariant" => Some(&["enum_name", "variant_name"]),
        "AddMatchArm" => Some(&[
            "symbol_path",
            "file_path",
            "function_name",
            "enum_name",
            "pattern",
            "body",
        ]),
        "RemoveMatchArm" => Some(&[
            "symbol_path",
            "file_path",
            "function_name",
            "enum_name",
            "pattern",
        ]),
        "ReplaceMatchArm" => Some(&[
            "symbol_path",
            "file_path",
            "function_name",
            "enum_name",
            "old_pattern",
            "new_pattern",
            "new_body",
        ]),
        "AddStructLiteralField" => Some(&["struct_name", "field_name", "value"]),
        "RemoveStructLiteralField" => Some(&["struct_name", "field_name"]),
        // Delete operations
        "RemoveStruct" => Some(&["name"]),
        "RemoveEnum" => Some(&["name"]),
        // Duplicate operations
        "DuplicateFunction" => Some(&["from", "to"]),
        "DuplicateStruct" => Some(&["from", "to", "include_impls"]),
        "DuplicateEnum" => Some(&["from", "to", "include_impls"]),
        "DuplicateModTree" => Some(&["from", "to"]),
        // Const/TypeAlias
        "AddConst" => Some(&["name", "ty", "value", "is_pub"]),
        "AddTypeAlias" => Some(&["name", "ty", "is_pub"]),
        // Spec
        "AddSpec" => Some(&[
            "target_type",
            "group",
            "alias_name",
            "relations",
            "module_path",
            "file_path",
        ]),
        // Method operations
        "AddMethod" => Some(&[
            "impl_target",
            "method_name",
            "params",
            "return_type",
            "body",
            "is_pub",
            "self_param",
        ]),
        "RemoveMethod" => Some(&["impl_target", "method_name"]),
        // Delete operations (additional)
        "RemoveConst" => Some(&["name"]),
        "RemoveTypeAlias" => Some(&["name"]),
        "RemoveUse" => Some(&["path"]),
        "RemoveTrait" => Some(&["name"]),
        "RemoveImpl" => Some(&["self_ty", "trait_name"]),
        // Item operations
        "AddItem" => Some(&["location", "content", "item_kind"]),
        "RemoveItem" => Some(&["target", "item_kind"]),
        "AddCode" => Some(&["parent", "parent_ref", "code"]),
        // Idiom operations
        "OrganizeImports" => Some(&["target_mod", "deduplicate", "merge_groups"]),
        "MergeImplBlocks" => Some(&["target_mod", "target_type", "inherent_only"]),
        "LoopToIterator" => Some(&["target_mod", "target_var"]),
        "UnwrapToQuestion" => Some(&["target_mod", "target_fn", "include_expect"]),
        "IntroduceVariable" => Some(&["target_mod", "target_fn", "expr", "var_name"]),
        "GenerateBuilder" => Some(&[
            "struct_name",
            "struct_id",
            "target_mod",
            "fields",
            "add_builder_method",
        ]),
        // PureStmt/PureExpr operations
        "ReplaceExpr" => Some(&[
            "target_mod",
            "target_fn",
            "old_expr",
            "new_expr",
            "replace_all",
            "symbol_path",
        ]),
        "RemoveStatement" => Some(&[
            "target_mod",
            "target_fn",
            "pattern",
            "remove_all",
            "symbol_path",
        ]),
        "InsertStatement" => Some(&[
            "target_mod",
            "target_fn",
            "stmt",
            "position",
            "reference_pattern",
            "symbol_path",
        ]),
        "ReplaceStatement" => Some(&[
            "target_mod",
            "target_fn",
            "old_stmt",
            "new_stmt",
            "symbol_path",
        ]),
        // Additional idiom transformations
        "AssignOp" => Some(&["target_mod", "target_fn"]),
        "BoolSimplify" => Some(&["target_mod"]),
        "CloneOnCopy" => Some(&["target_mod"]),
        "CollapsibleIf" => Some(&["target_mod"]),
        "ComparisonToMethod" => Some(&["target_mod"]),
        "RedundantClosure" => Some(&["target_mod"]),
        "ManualMap" => Some(&["target_mod"]),
        "MatchToIfLet" => Some(&["target_mod"]),
        // Custom/Plugin
        "Custom" => Some(&["description", "examples"]),
        #[cfg(feature = "wasm-plugin")]
        "Plugin" => Some(&["name", "file_patterns"]),
        _ => None,
    }
}

/// Valid fields for Goal struct
pub static GOAL_FIELDS: &[&str] = &[
    "query",
    "intents",
    "scope",
    "constraints",
    "conflict_strategy",
    "confidence",
];

/// Create Intent schema for fuzzy repair
pub fn intent_schema() -> TaggedEnumSchema<fn(&str) -> Option<&'static [&'static str]>> {
    TaggedEnumSchema::new("type", INTENT_TYPES, get_intent_fields)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_intent_types_coverage() {
        // Verify all types have field definitions
        for &type_name in INTENT_TYPES {
            assert!(
                get_intent_fields(type_name).is_some(),
                "Missing field definition for: {}",
                type_name
            );
        }
    }

    #[test]
    fn test_schema_creation() {
        let schema = intent_schema();
        assert_eq!(schema.tag_field, "type");
        assert!(schema.is_valid_tag("AddDerive"));
        assert!(!schema.is_valid_tag("InvalidType"));
    }
}