fuzzy-parser 0.3.0

Fuzzy JSON repair for LLM-generated DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Schema definitions for fuzzy repair
//!
//! This module provides schema types that callers use to define
//! valid field names, type discriminators, and expected value shapes
//! for fuzzy matching and coercion.
//!
//! # Design
//!
//! Schemas are built from three layers:
//!
//! - [`FieldKind`] — the expected shape of a single field *value*
//!   (enum set, nested object, coercion target, ...). Nested kinds make
//!   the schema recursive: objects inside arrays inside objects can all
//!   be described and repaired.
//! - [`ObjectSchema`] — a set of named fields ([`FieldDef`]) for one object.
//! - [`TaggedEnumSchema`] — a discriminated union: a tag field selects
//!   which [`ObjectSchema`] applies.
//!
//! All schema types own their strings, so schemas can be built at runtime
//! (e.g. from a config file or an API definition), not only from `'static`
//! literals.

/// Expected shape of a single field value.
///
/// After a field's *name* has been repaired, its `FieldKind` decides what
/// repair (if any) is applied to the field's *value*:
///
/// - Fuzzy correction against a closed set ([`FieldKind::Enum`],
///   [`FieldKind::EnumArray`])
/// - Recursive repair with a nested schema ([`FieldKind::Object`],
///   [`FieldKind::ObjectArray`])
/// - Type coercion of string-encoded scalars ([`FieldKind::Integer`],
///   [`FieldKind::Number`], [`FieldKind::Bool`], [`FieldKind::String`])
///
/// Values that don't match the expected shape (e.g. a non-parseable string
/// for [`FieldKind::Integer`]) are left untouched — no lossy repair is made.
///
/// This enum is `#[non_exhaustive]`: new repair kinds may be added in minor
/// releases, so external `match` expressions need a wildcard arm.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub enum FieldKind {
    /// No value repair; the value is left untouched.
    #[default]
    Any,
    /// A string constrained to a closed set of values; fuzzy-corrected.
    Enum(Vec<String>),
    /// An array of strings, each constrained to a closed set; fuzzy-corrected.
    EnumArray(Vec<String>),
    /// A nested object repaired recursively with its own schema.
    Object(ObjectSchema),
    /// An array of objects, each repaired recursively with the same schema.
    ObjectArray(ObjectSchema),
    /// A nested tagged enum (discriminated union) repaired with its own
    /// [`TaggedEnumSchema`]: tag value, field names, and field values.
    TaggedEnum(TaggedEnumSchema),
    /// An array of tagged enums, each repaired with the same schema
    /// (e.g. a list of DSL intents).
    TaggedEnumArray(TaggedEnumSchema),
    /// Coerce string-encoded integers to numbers (`"42"` → `42`).
    Integer,
    /// Coerce string-encoded numbers to numbers (`"4.2"` → `4.2`).
    Number,
    /// Coerce string-encoded booleans to booleans (`"true"` → `true`).
    Bool,
    /// Coerce scalar numbers / booleans to their string rendering (`42` → `"42"`).
    String,
}

impl FieldKind {
    /// Build an [`FieldKind::Enum`] from any iterator of string-likes.
    pub fn enum_of<I, S>(values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self::Enum(values.into_iter().map(|s| s.as_ref().to_string()).collect())
    }

    /// Build an [`FieldKind::EnumArray`] from any iterator of string-likes.
    pub fn enum_array<I, S>(values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self::EnumArray(values.into_iter().map(|s| s.as_ref().to_string()).collect())
    }
}

/// A named field with an expected value shape.
#[derive(Debug, Clone, PartialEq)]
pub struct FieldDef {
    /// The field name (used for fuzzy field-name repair).
    pub name: String,
    /// The expected shape of the field's value.
    pub kind: FieldKind,
}

impl FieldDef {
    /// Create a new field definition.
    pub fn new(name: impl AsRef<str>, kind: FieldKind) -> Self {
        Self {
            name: name.as_ref().to_string(),
            kind,
        }
    }
}

/// Schema for an object with known fields.
///
/// Field *names* are fuzzy-repaired against the defined names; field
/// *values* are repaired according to each field's [`FieldKind`].
/// Because [`FieldKind`] can contain nested `ObjectSchema`s, repair
/// recurses to any depth.
///
/// # Example
///
/// ```
/// use fuzzy_parser::{FieldKind, ObjectSchema};
///
/// let schema = ObjectSchema::new(["name"])
///     .with_field_kind("timeout", FieldKind::Integer)
///     .with_field_kind("derives", FieldKind::enum_array(["Debug", "Clone"]))
///     .with_field_kind(
///         "inner",
///         FieldKind::Object(ObjectSchema::new(["host", "port"])),
///     );
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ObjectSchema {
    /// The defined fields.
    pub fields: Vec<FieldDef>,
}

impl ObjectSchema {
    /// Create a schema from field names (all fields accept any value shape).
    pub fn new<I, S>(valid_fields: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self {
            fields: valid_fields
                .into_iter()
                .map(|name| FieldDef::new(name, FieldKind::Any))
                .collect(),
        }
    }

    /// Create an empty schema (add fields with the builder methods).
    pub fn empty() -> Self {
        Self::default()
    }

    /// Add a field that accepts any value shape.
    pub fn with_field(self, name: impl AsRef<str>) -> Self {
        self.with_field_kind(name, FieldKind::Any)
    }

    /// Add a field with an expected value shape.
    ///
    /// If a field with the same name already exists, its kind is replaced.
    pub fn with_field_kind(mut self, name: impl AsRef<str>, kind: FieldKind) -> Self {
        let name = name.as_ref();
        if let Some(def) = self.fields.iter_mut().find(|d| d.name == name) {
            def.kind = kind;
        } else {
            self.fields.push(FieldDef::new(name, kind));
        }
        self
    }

    /// Check if a field name is valid.
    pub fn is_valid_field(&self, field: &str) -> bool {
        self.fields.iter().any(|d| d.name == field)
    }

    /// Iterate over the defined field names.
    pub fn field_names(&self) -> impl Iterator<Item = &str> {
        self.fields.iter().map(|d| d.name.as_str())
    }

    /// Get the expected value shape for a field, if defined.
    pub fn kind_of(&self, field: &str) -> Option<&FieldKind> {
        self.fields.iter().find(|d| d.name == field).map(|d| &d.kind)
    }
}

/// Schema for a tagged enum (discriminated union)
///
/// Used for types with a discriminator field (e.g., tag: "type", "kind").
/// Each valid tag value maps to an [`ObjectSchema`] describing that
/// variant's fields. Fields registered globally (via
/// [`with_enum_array`](Self::with_enum_array),
/// [`with_nested_object`](Self::with_nested_object) or
/// [`with_field_kind`](Self::with_field_kind)) apply to every variant.
///
/// # Example (static, closure-based)
///
/// ```
/// use fuzzy_parser::TaggedEnumSchema;
///
/// let schema = TaggedEnumSchema::new(
///     "type",
///     &["AddDerive", "RemoveDerive"],
///     |tag| match tag {
///         "AddDerive" | "RemoveDerive" => Some(&["target", "derives"][..]),
///         _ => None,
///     },
/// )
/// .with_enum_array("derives", &["Debug", "Clone", "Serialize"])
/// .with_nested_object("config", &["timeout", "retries"]);
/// ```
///
/// # Example (dynamic, built at runtime)
///
/// ```
/// use fuzzy_parser::{FieldKind, ObjectSchema, TaggedEnumSchema};
///
/// // Field names can come from runtime data (config, API spec, ...).
/// let variant = String::from("AddDerive");
/// let schema = TaggedEnumSchema::with_tag("type").with_variant(
///     variant,
///     ObjectSchema::new(["target"])
///         .with_field_kind("derives", FieldKind::enum_array(["Debug", "Clone"]))
///         .with_field_kind("timeout", FieldKind::Integer),
/// );
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TaggedEnumSchema {
    /// The discriminator field name (e.g., "type", "kind")
    pub tag_field: String,
    /// The variants: (tag value, schema for that variant's fields)
    pub variants: Vec<(String, ObjectSchema)>,
    /// Fields that apply to every variant (checked after variant fields)
    pub global_fields: Vec<FieldDef>,
}

impl TaggedEnumSchema {
    /// Create a new tagged enum schema from a field-resolver closure.
    ///
    /// The closure is evaluated once per valid tag at construction time,
    /// so the schema itself owns its data and carries no generic parameter.
    pub fn new<F>(tag_field: impl AsRef<str>, valid_tags: &[&str], fields_for_tag: F) -> Self
    where
        F: Fn(&str) -> Option<&'static [&'static str]>,
    {
        let variants = valid_tags
            .iter()
            .map(|tag| {
                let fields = fields_for_tag(tag)
                    .map(|fs| ObjectSchema::new(fs.iter().copied()))
                    .unwrap_or_default();
                (tag.to_string(), fields)
            })
            .collect();
        Self {
            tag_field: tag_field.as_ref().to_string(),
            variants,
            global_fields: Vec::new(),
        }
    }

    /// Create an empty schema for dynamic construction.
    ///
    /// Add variants with [`with_variant`](Self::with_variant).
    pub fn with_tag(tag_field: impl AsRef<str>) -> Self {
        Self {
            tag_field: tag_field.as_ref().to_string(),
            variants: Vec::new(),
            global_fields: Vec::new(),
        }
    }

    /// Add (or replace) a variant with its field schema.
    pub fn with_variant(mut self, tag: impl AsRef<str>, schema: ObjectSchema) -> Self {
        let tag = tag.as_ref();
        if let Some(entry) = self.variants.iter_mut().find(|(t, _)| t == tag) {
            entry.1 = schema;
        } else {
            self.variants.push((tag.to_string(), schema));
        }
        self
    }

    /// Add a global enum array field for repair (applies to every variant).
    ///
    /// Values in this array field will be fuzzy-matched against `valid_values`.
    ///
    /// # Example
    ///
    /// ```
    /// use fuzzy_parser::TaggedEnumSchema;
    ///
    /// let schema = TaggedEnumSchema::new("type", &["AddDerive"], |_| Some(&["derives"][..]))
    ///     .with_enum_array("derives", &["Debug", "Clone", "Serialize"]);
    /// // Now "Debg" in derives array will be corrected to "Debug"
    /// ```
    pub fn with_enum_array<I, S>(self, field: impl AsRef<str>, valid_values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.with_field_kind(field, FieldKind::enum_array(valid_values))
    }

    /// Add a global nested object field for repair (applies to every variant).
    ///
    /// Field names in this nested object will be fuzzy-matched against
    /// `valid_fields`. For deeper nesting or value shapes inside the nested
    /// object, use [`with_field_kind`](Self::with_field_kind) with
    /// [`FieldKind::Object`] instead.
    ///
    /// # Example
    ///
    /// ```
    /// use fuzzy_parser::TaggedEnumSchema;
    ///
    /// let schema = TaggedEnumSchema::new("type", &["Configure"], |_| Some(&["config"][..]))
    ///     .with_nested_object("config", &["timeout", "retries", "enabled"]);
    /// // Now "timout" in config object will be corrected to "timeout"
    /// ```
    pub fn with_nested_object<I, S>(self, field: impl AsRef<str>, valid_fields: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.with_field_kind(field, FieldKind::Object(ObjectSchema::new(valid_fields)))
    }

    /// Add (or replace) a global field with an expected value shape.
    ///
    /// Global fields apply to every variant, after the variant's own
    /// field kinds.
    pub fn with_field_kind(mut self, field: impl AsRef<str>, kind: FieldKind) -> Self {
        let field = field.as_ref();
        if let Some(def) = self.global_fields.iter_mut().find(|d| d.name == field) {
            def.kind = kind;
        } else {
            self.global_fields.push(FieldDef::new(field, kind));
        }
        self
    }

    /// Check if a tag value is valid
    pub fn is_valid_tag(&self, tag: &str) -> bool {
        self.variants.iter().any(|(t, _)| t == tag)
    }

    /// Iterate over the valid tag values.
    pub fn tag_values(&self) -> impl Iterator<Item = &str> {
        self.variants.iter().map(|(t, _)| t.as_str())
    }

    /// Get the field schema for a tag value, if the tag is valid.
    pub fn variant_schema(&self, tag: &str) -> Option<&ObjectSchema> {
        self.variants.iter().find(|(t, _)| t == tag).map(|(_, s)| s)
    }
}

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

    #[test]
    fn test_tagged_enum_schema() {
        let schema =
            TaggedEnumSchema::new("type", &["AddDerive", "RemoveDerive"], |tag| match tag {
                "AddDerive" => Some(&["target", "derives"]),
                "RemoveDerive" => Some(&["target", "derives"]),
                _ => None,
            });

        assert!(schema.is_valid_tag("AddDerive"));
        assert!(!schema.is_valid_tag("InvalidType"));
        let fields: Vec<&str> = schema
            .variant_schema("AddDerive")
            .unwrap()
            .field_names()
            .collect();
        assert_eq!(fields, vec!["target", "derives"]);
    }

    #[test]
    fn test_object_schema() {
        let schema = ObjectSchema::new(["name", "value", "is_pub"]);

        assert!(schema.is_valid_field("name"));
        assert!(!schema.is_valid_field("invalid"));
    }

    #[test]
    fn test_dynamic_schema_from_owned_strings() {
        // Schema built entirely from runtime (non-'static) data
        let tag_field = String::from("kind");
        let tags = vec![String::from("Create"), String::from("Delete")];
        let fields = vec![String::from("name"), String::from("path")];

        let mut schema = TaggedEnumSchema::with_tag(&tag_field);
        for tag in &tags {
            schema = schema.with_variant(tag, ObjectSchema::new(&fields));
        }

        assert!(schema.is_valid_tag("Create"));
        assert!(schema.variant_schema("Delete").unwrap().is_valid_field("path"));
    }

    #[test]
    fn test_with_field_kind_replaces_existing() {
        let schema = ObjectSchema::new(["timeout"])
            .with_field_kind("timeout", FieldKind::Integer);

        assert_eq!(schema.fields.len(), 1);
        assert_eq!(schema.kind_of("timeout"), Some(&FieldKind::Integer));
    }

    #[test]
    fn test_recursive_schema_shape() {
        let schema = ObjectSchema::empty().with_field_kind(
            "outer",
            FieldKind::Object(
                ObjectSchema::empty()
                    .with_field_kind("inner", FieldKind::Object(ObjectSchema::new(["leaf"]))),
            ),
        );

        let FieldKind::Object(outer) = schema.kind_of("outer").unwrap() else {
            panic!("expected object kind");
        };
        let FieldKind::Object(inner) = outer.kind_of("inner").unwrap() else {
            panic!("expected object kind");
        };
        assert!(inner.is_valid_field("leaf"));
    }
}