fuzzy-parser 0.1.0

Fuzzy JSON repair for LLM-generated DSL
Documentation
//! Fuzzy JSON repair for LLM-generated DSL
//!
//! This crate provides generic fuzzy matching and automatic correction for
//! JSON that may contain typos (common when generated by LLMs).
//!
//! # Design
//!
//! This crate provides **generic repair APIs** - the schema definitions
//! live in the calling crate (e.g., your application defines the schema).
//!
//! # Features
//!
//! - **JSON sanitization**: Fix syntax errors (trailing commas, missing braces)
//! - **Tagged enum repair**: Fix type discriminator typos (e.g., `"AddDeriv"` → `"AddDerive"`)
//! - **Field name repair**: Fix field name typos (e.g., `"taget"` → `"target"`)
//! - **Enum array repair**: Fix values in enum arrays (e.g., `["Debg"]` → `["Debug"]`)
//! - **Nested object repair**: Fix field names in nested objects
//! - **Multiple algorithm support**: Jaro-Winkler, Levenshtein, Damerau-Levenshtein
//! - **Configurable similarity threshold**
//!
//! # Example
//!
//! ```
//! use fuzzy_parser::{
//!     sanitize_json, repair_tagged_enum_json, TaggedEnumSchema, FuzzyOptions,
//! };
//!
//! // Define schema with enum arrays and nested objects
//! let schema = TaggedEnumSchema::new(
//!     "type",  // tag field
//!     &["AddDerive", "RemoveDerive"],  // valid types
//!     |tag| match tag {
//!         "AddDerive" | "RemoveDerive" => Some(&["target", "derives", "config"][..]),
//!         _ => None,
//!     },
//! )
//! .with_enum_array("derives", &["Debug", "Clone", "Serialize", "Default"])
//! .with_nested_object("config", &["timeout", "retries"]);
//!
//! // LLM output with syntax errors AND typos
//! let malformed = r#"{"type": "AddDeriv", "taget": "User", "derives": ["Debg",], "config": {"timout": 30,}}"#;
//!
//! // Step 1: Sanitize (fix syntax errors)
//! let sanitized = sanitize_json(malformed);
//!
//! // Step 2: Repair (fix typos)
//! let result = repair_tagged_enum_json(&sanitized, &schema, &FuzzyOptions::default()).unwrap();
//!
//! assert_eq!(result.repaired["type"], "AddDerive");
//! assert!(result.repaired.get("target").is_some());
//! assert_eq!(result.repaired["derives"][0], "Debug");
//! assert!(result.repaired["config"].get("timeout").is_some());
//! ```

pub mod distance;
pub mod error;
pub mod repair;
pub mod sanitize;
pub mod schema;

// Re-export main types
pub use distance::{Algorithm, Match};
pub use error::FuzzyError;
pub use repair::{
    repair_enum_array, repair_fields_with_list, repair_object_fields, repair_tagged_enum,
    repair_tagged_enum_array, repair_tagged_enum_json, Correction, FuzzyOptions, RepairResult,
};
pub use sanitize::sanitize_json;
pub use schema::{ObjectSchema, TaggedEnumSchema};

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

    #[test]
    fn test_full_workflow() {
        // Define a simple schema
        let schema =
            TaggedEnumSchema::new("type", &["AddDerive", "RenameIdent"], |tag| match tag {
                "AddDerive" => Some(&["target", "derives"][..]),
                "RenameIdent" => Some(&["from", "to", "kind"][..]),
                _ => None,
            });

        // Simulate LLM output with typos
        let llm_output = r#"{
            "type": "AddDeriv",
            "taget": "DatabaseConfig",
            "derives": ["Debug", "Clone"]
        }"#;

        let result =
            repair_tagged_enum_json(llm_output, &schema, &FuzzyOptions::default()).unwrap();

        // Verify corrections
        assert_eq!(result.repaired["type"], "AddDerive");
        assert_eq!(result.repaired["target"], "DatabaseConfig");
        assert_eq!(result.corrections.len(), 2);
    }

    #[test]
    fn test_sanitize_then_repair_workflow() {
        // Define schema with enum arrays
        let schema =
            TaggedEnumSchema::new("type", &["AddDerive"], |_| Some(&["target", "derives"][..]))
                .with_enum_array("derives", &["Debug", "Clone", "Serialize"]);

        // LLM output with BOTH syntax errors AND typos
        let malformed_llm_output = r#"{
            "type": "AddDeriv",
            "taget": "User",
            "derives": ["Debg", "Clne",],
        }"#;

        // Step 1: Sanitize
        let sanitized = sanitize_json(malformed_llm_output);

        // Verify sanitization worked
        assert!(!sanitized.contains(",]"));
        assert!(!sanitized.contains(",}"));

        // Step 2: Repair
        let result =
            repair_tagged_enum_json(&sanitized, &schema, &FuzzyOptions::default()).unwrap();

        // Verify all corrections
        assert_eq!(result.repaired["type"], "AddDerive");
        assert!(result.repaired.get("target").is_some());
        assert_eq!(result.repaired["derives"][0], "Debug");
        assert_eq!(result.repaired["derives"][1], "Clone");
    }

    #[test]
    fn test_sanitize_then_repair_missing_brace() {
        let schema = TaggedEnumSchema::new("type", &["Action"], |_| Some(&["name"][..]));

        // LLM truncated output
        let truncated = r#"{"type": "Action", "name": "test"#;

        let sanitized = sanitize_json(truncated);
        let result =
            repair_tagged_enum_json(&sanitized, &schema, &FuzzyOptions::default()).unwrap();

        assert_eq!(result.repaired["type"], "Action");
        assert_eq!(result.repaired["name"], "test");
    }
}