mullama 0.3.0

Comprehensive Rust bindings for llama.cpp with memory-safe API and advanced features
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//! Structured output generation via JSON Schema validation
//!
//! This module converts JSON Schema definitions to GBNF grammars for constrained
//! generation, enabling the model to produce output that conforms to a schema.
//!
//! ## Supported JSON Schema Features
//!
//! - **Types**: `object`, `array`, `string`, `number`, `integer`, `boolean`, `null`
//! - **Object properties**: `properties`, `required`, `additionalProperties`
//! - **Arrays**: `items` (single schema)
//! - **Enums**: `enum` (for strings)
//! - **String constraints**: `minLength`, `maxLength`
//! - **Integer constraints**: `minimum`, `maximum`
//! - **Union types**: `oneOf`, `anyOf`
//! - **References**: `$ref` (local references within the same schema)
//! - **Patterns**: `pattern` (regular expression constraints)
//!
//! ## Unsupported Features
//!
//! - `allOf`, `not`
//! - `patternProperties`
//! - `format` (dates, emails, etc.) - silently ignored
//!
//! ## Example
//!
//! ```rust,no_run
//! use mullama::structured_output::JsonSchemaConverter;
//! use serde_json::json;
//!
//! let schema = json!({
//!     "type": "object",
//!     "properties": {
//!         "name": { "type": "string" },
//!         "age": { "type": "integer", "minimum": 0 }
//!     },
//!     "required": ["name", "age"]
//! });
//!
//! let grammar = JsonSchemaConverter::convert(&schema).unwrap();
//! ```

use crate::grammar::Grammar;
use crate::MullamaError;
use serde_json::Value;

/// Error types for structured output conversion
#[derive(Debug, Clone)]
pub enum StructuredOutputError {
    /// Schema uses an unsupported feature
    UnsupportedFeature(String),
    /// Schema is invalid or malformed
    InvalidSchema(String),
    /// Grammar compilation failed
    GrammarError(String),
}

impl std::fmt::Display for StructuredOutputError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnsupportedFeature(msg) => write!(f, "Unsupported schema feature: {}", msg),
            Self::InvalidSchema(msg) => write!(f, "Invalid schema: {}", msg),
            Self::GrammarError(msg) => write!(f, "Grammar error: {}", msg),
        }
    }
}

impl std::error::Error for StructuredOutputError {}

impl From<StructuredOutputError> for MullamaError {
    fn from(e: StructuredOutputError) -> Self {
        MullamaError::GrammarError(e.to_string())
    }
}

/// Converts JSON Schema to GBNF grammar
pub struct JsonSchemaConverter {
    rules: Vec<String>,
    rule_counter: usize,
    definitions: serde_json::Map<String, Value>,
}

impl JsonSchemaConverter {
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            rule_counter: 0,
            definitions: serde_json::Map::new(),
        }
    }

    /// Convert a JSON Schema to a Grammar
    ///
    /// # Arguments
    ///
    /// * `schema` - The JSON Schema to convert
    ///
    /// # Returns
    ///
    /// A Grammar that constrains output to match the schema
    pub fn convert(schema: &Value) -> Result<Grammar, StructuredOutputError> {
        let mut converter = Self::new();
        converter.add_primitives();

        // Extract definitions for $ref resolution
        if let Some(obj) = schema.as_object() {
            if let Some(defs) = obj.get("definitions") {
                if let Some(defs_obj) = defs.as_object() {
                    converter.definitions = defs_obj.clone();
                }
            }
            if let Some(defs) = obj.get("$defs") {
                if let Some(defs_obj) = defs.as_object() {
                    for (k, v) in defs_obj {
                        converter.definitions.insert(k.clone(), v.clone());
                    }
                }
            }
        }

        // Handle top-level oneOf/anyOf
        if let Some(obj) = schema.as_object() {
            if let Some(one_of) = obj.get("oneOf") {
                let root_rule = converter.one_of_to_rule("root", one_of)?;
                converter.rules.insert(0, root_rule);
                let gbnf = converter.rules.join("\n");
                return Grammar::from_gbnf(&gbnf)
                    .map_err(|e| StructuredOutputError::GrammarError(e.to_string()));
            }
            if let Some(any_of) = obj.get("anyOf") {
                let root_rule = converter.any_of_to_rule("root", any_of)?;
                converter.rules.insert(0, root_rule);
                let gbnf = converter.rules.join("\n");
                return Grammar::from_gbnf(&gbnf)
                    .map_err(|e| StructuredOutputError::GrammarError(e.to_string()));
            }
        }

        let root_rule = converter.schema_to_rule("root", schema)?;
        converter.rules.insert(0, root_rule);

        let gbnf = converter.rules.join("\n");
        Grammar::from_gbnf(&gbnf).map_err(|e| StructuredOutputError::GrammarError(e.to_string()))
    }

    /// Validate that a schema is supported
    pub fn validate_schema(schema: &Value) -> Result<(), StructuredOutputError> {
        Self::validate_schema_recursive(schema)
    }

    fn validate_schema_recursive(schema: &Value) -> Result<(), StructuredOutputError> {
        let obj = schema.as_object().ok_or_else(|| {
            StructuredOutputError::InvalidSchema("Schema must be an object".into())
        })?;

        for key in obj.keys() {
            match key.as_str() {
                "type"
                | "properties"
                | "required"
                | "additionalProperties"
                | "items"
                | "enum"
                | "minimum"
                | "maximum"
                | "minLength"
                | "maxLength"
                | "description"
                | "title"
                | "default"
                | "examples"
                | "const"
                | "oneOf"
                | "anyOf"
                | "$ref"
                | "pattern"
                | "definitions"
                | "$defs"
                | "minItems"
                | "maxItems" => {}
                "allOf" | "not" => {
                    return Err(StructuredOutputError::UnsupportedFeature(format!(
                        "'{}' is not supported",
                        key
                    )));
                }
                "patternProperties" => {
                    return Err(StructuredOutputError::UnsupportedFeature(
                        "'patternProperties' is not supported".into(),
                    ));
                }
                "format" => {}
                _ => {}
            }
        }

        if let Some(properties) = obj.get("properties") {
            if let Some(props) = properties.as_object() {
                for prop_schema in props.values() {
                    Self::validate_schema_recursive(prop_schema)?;
                }
            }
        }

        if let Some(items) = obj.get("items") {
            Self::validate_schema_recursive(items)?;
        }

        if let Some(one_of) = obj.get("oneOf") {
            if let Some(arr) = one_of.as_array() {
                for sub_schema in arr {
                    Self::validate_schema_recursive(sub_schema)?;
                }
            }
        }

        if let Some(any_of) = obj.get("anyOf") {
            if let Some(arr) = any_of.as_array() {
                for sub_schema in arr {
                    Self::validate_schema_recursive(sub_schema)?;
                }
            }
        }

        if let Some(defs) = obj.get("definitions") {
            if let Some(defs_obj) = defs.as_object() {
                for def_schema in defs_obj.values() {
                    Self::validate_schema_recursive(def_schema)?;
                }
            }
        }

        if let Some(defs) = obj.get("$defs") {
            if let Some(defs_obj) = defs.as_object() {
                for def_schema in defs_obj.values() {
                    Self::validate_schema_recursive(def_schema)?;
                }
            }
        }

        Ok(())
    }

    fn add_primitives(&mut self) {
        // Whitespace
        self.rules.push("ws ::= [ \\t\\n]*".to_string());

        // String primitive (handles escaping)
        self.rules.push(
            r#"string ::= "\"" ([^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]))* "\"""#.to_string()
        );

        // Number primitive
        self.rules.push(
            "number ::= \"-\"? ([0-9] | [1-9] [0-9]*) (\".\" [0-9]+)? ([eE] [\"+\\-\"]? [0-9]+)?"
                .to_string(),
        );

        // Integer primitive
        self.rules
            .push("integer ::= \"-\"? ([0-9] | [1-9] [0-9]*)".to_string());

        // Boolean primitive
        self.rules
            .push("boolean ::= \"true\" | \"false\"".to_string());

        // Null primitive
        self.rules.push("null ::= \"null\"".to_string());
    }

    fn generate_rule_name(&mut self, prefix: &str) -> String {
        self.rule_counter += 1;
        format!("{}_{}", prefix, self.rule_counter)
    }

    fn schema_to_rule(
        &mut self,
        name: &str,
        schema: &Value,
    ) -> Result<String, StructuredOutputError> {
        let obj = schema.as_object().ok_or_else(|| {
            StructuredOutputError::InvalidSchema("Schema must be an object".into())
        })?;

        // Handle const
        if let Some(const_val) = obj.get("const") {
            return self.const_to_rule(name, const_val);
        }

        // Handle enum
        if let Some(enum_vals) = obj.get("enum") {
            return self.enum_to_rule(name, enum_vals);
        }

        // Handle $ref
        if let Some(ref_val) = obj.get("$ref") {
            return self.ref_to_rule(name, ref_val);
        }

        // Handle oneOf
        if let Some(one_of) = obj.get("oneOf") {
            return self.one_of_to_rule(name, one_of);
        }

        // Handle anyOf
        if let Some(any_of) = obj.get("anyOf") {
            return self.any_of_to_rule(name, any_of);
        }

        // Handle type
        let type_val = obj.get("type");

        match type_val.and_then(|v| v.as_str()) {
            Some("object") => self.object_to_rule(name, obj),
            Some("array") => self.array_to_rule(name, obj),
            Some("string") => self.string_to_rule(name, obj),
            Some("number") => Ok(format!("{} ::= number", name)),
            Some("integer") => self.integer_to_rule(name, obj),
            Some("boolean") => Ok(format!("{} ::= boolean", name)),
            Some("null") => Ok(format!("{} ::= null", name)),
            Some(t) => Err(StructuredOutputError::InvalidSchema(format!(
                "Unknown type '{}'",
                t
            ))),
            None => {
                let obj_rule = self.generate_rule_name("obj");
                let arr_rule = self.generate_rule_name("arr");
                self.rules.push(format!(
                    "{} ::= string | number | boolean | null | {} | {}",
                    name, obj_rule, arr_rule
                ));
                self.rules
                    .push(format!("{} ::= {{}} | {{ string : value }}", obj_rule));
                self.rules
                    .push(format!("{} ::= [] | [ value (\",\" value)*]", arr_rule));
                self.rules
                    .push("value ::= string | number | boolean | null | obj_1 | arr_2".to_string());
                Ok(format!("{} ::= {}", name, name))
            }
        }
    }

    fn const_to_rule(
        &mut self,
        name: &str,
        value: &Value,
    ) -> Result<String, StructuredOutputError> {
        let literal = match value {
            Value::String(s) => format!("\"\\\"{}\\\"\"", escape_gbnf_string(s)),
            Value::Number(n) => format!("\"{}\"", n),
            Value::Bool(b) => format!("\"{}\"", b),
            Value::Null => "\"null\"".to_string(),
            _ => {
                return Err(StructuredOutputError::UnsupportedFeature(
                    "const with object/array values".into(),
                ))
            }
        };
        Ok(format!("{} ::= {}", name, literal))
    }

    fn enum_to_rule(
        &mut self,
        name: &str,
        values: &Value,
    ) -> Result<String, StructuredOutputError> {
        let arr = values
            .as_array()
            .ok_or_else(|| StructuredOutputError::InvalidSchema("enum must be an array".into()))?;

        let alternatives: Vec<String> = arr
            .iter()
            .map(|v| match v {
                Value::String(s) => Ok(format!("\"\\\"{}\\\"\"", escape_gbnf_string(s))),
                Value::Number(n) => Ok(format!("\"{}\"", n)),
                Value::Bool(b) => Ok(format!("\"{}\"", b)),
                Value::Null => Ok("\"null\"".to_string()),
                _ => Err(StructuredOutputError::UnsupportedFeature(
                    "enum with object/array values".into(),
                )),
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(format!("{} ::= {}", name, alternatives.join(" | ")))
    }

    fn object_to_rule(
        &mut self,
        name: &str,
        schema: &serde_json::Map<String, Value>,
    ) -> Result<String, StructuredOutputError> {
        let properties = schema.get("properties").and_then(|v| v.as_object());
        let required: Vec<&str> = schema
            .get("required")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str()).collect())
            .unwrap_or_default();

        // If no properties defined, use generic object
        let Some(props) = properties else {
            return Ok(format!("{} ::= \"{{\" ws \"}}\"", name));
        };

        if props.is_empty() {
            return Ok(format!("{} ::= \"{{\" ws \"}}\"", name));
        }

        // Generate rules for each property
        let mut prop_rules = Vec::new();
        let mut prop_names = Vec::new();

        for (prop_name, prop_schema) in props {
            let rule_name = self.generate_rule_name(&format!("{}_prop", name));
            let rule = self.schema_to_rule(&rule_name, prop_schema)?;
            self.rules.push(rule);

            let is_required = required.contains(&prop_name.as_str());
            prop_rules.push((prop_name.clone(), rule_name, is_required));
            prop_names.push(prop_name.clone());
        }

        // Build the object rule
        // For simplicity, we'll generate required properties in order
        // and make optional properties actually optional with a specific pattern

        let mut parts = Vec::new();
        let mut first = true;

        for (prop_name, rule_name, is_required) in &prop_rules {
            let prop_pattern = if first {
                format!(
                    "\"\\\"{}\\\":\" ws {}",
                    escape_gbnf_string(prop_name),
                    rule_name
                )
            } else {
                format!(
                    "\",\" ws \"\\\"{}\\\":\" ws {}",
                    escape_gbnf_string(prop_name),
                    rule_name
                )
            };

            if *is_required {
                parts.push(prop_pattern);
                first = false;
            } else {
                // Optional property
                let opt_rule = self.generate_rule_name("opt");
                self.rules
                    .push(format!("{} ::= ({})? ", opt_rule, prop_pattern));
                parts.push(opt_rule);
            }
        }

        let body = parts.join(" ");
        Ok(format!("{} ::= \"{{\" ws {} ws \"}}\"", name, body))
    }

    fn array_to_rule(
        &mut self,
        name: &str,
        schema: &serde_json::Map<String, Value>,
    ) -> Result<String, StructuredOutputError> {
        // Get items schema
        let items = schema.get("items");

        let item_rule = if let Some(item_schema) = items {
            let rule_name = self.generate_rule_name(&format!("{}_item", name));
            let rule = self.schema_to_rule(&rule_name, item_schema)?;
            self.rules.push(rule);
            rule_name
        } else {
            // No items schema, allow any value
            "string | number | boolean | null".to_string()
        };

        // Create array content rule
        let content_rule = self.generate_rule_name(&format!("{}_content", name));
        self.rules.push(format!(
            "{} ::= ({} (\",\" ws {})*)? ",
            content_rule, item_rule, item_rule
        ));

        Ok(format!("{} ::= \"[\" ws {} ws \"]\"", name, content_rule))
    }

    fn string_to_rule(
        &mut self,
        name: &str,
        schema: &serde_json::Map<String, Value>,
    ) -> Result<String, StructuredOutputError> {
        let min_length = schema
            .get("minLength")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;
        let max_length = schema.get("maxLength").and_then(|v| v.as_u64());

        // Handle pattern constraint - convert basic regex patterns to GBNF
        if let Some(pattern_val) = schema.get("pattern") {
            if let Some(pattern_str) = pattern_val.as_str() {
                return self.pattern_to_rule(name, pattern_str);
            }
        }

        if min_length > 0 || max_length.is_some() {
            let char_rule = r#"[^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])"#;

            let content_rule = self.generate_rule_name(&format!("{}_content", name));
            let repetition = if let Some(max) = max_length {
                format!("{},{}", min_length, max)
            } else if min_length > 0 {
                format!("{},", min_length)
            } else {
                "0,".to_string()
            };
            self.rules.push(format!(
                "{} ::= ({}){{{{{}}}}}",
                content_rule, char_rule, repetition
            ));
            return Ok(format!("{} ::= \"\\\"\" {} \"\\\"\"", name, content_rule));
        }

        Ok(format!("{} ::= string", name))
    }

    fn integer_to_rule(
        &mut self,
        name: &str,
        schema: &serde_json::Map<String, Value>,
    ) -> Result<String, StructuredOutputError> {
        let minimum = schema.get("minimum").and_then(|v| v.as_i64());
        let maximum = schema.get("maximum").and_then(|v| v.as_i64());

        // For simple cases with small ranges, we could enumerate values
        // For now, we use the basic integer rule
        if let (Some(min), Some(max)) = (minimum, maximum) {
            if max - min <= 100 && max - min >= 0 {
                // Small range, enumerate all values
                let values: Vec<String> = (min..=max).map(|n| format!("\"{}\"", n)).collect();
                return Ok(format!("{} ::= {}", name, values.join(" | ")));
            }
        }

        // Use default integer rule
        Ok(format!("{} ::= integer", name))
    }

    /// Handle $ref references
    fn ref_to_rule(
        &mut self,
        name: &str,
        ref_val: &Value,
    ) -> Result<String, StructuredOutputError> {
        let ref_str = ref_val
            .as_str()
            .ok_or_else(|| StructuredOutputError::InvalidSchema("$ref must be a string".into()))?;

        let definition_name = if ref_str.starts_with("#/definitions/") {
            &ref_str[14..]
        } else if ref_str.starts_with("#/$defs/") {
            &ref_str[8..]
        } else if !ref_str.starts_with('#') {
            ref_str
        } else {
            return Err(StructuredOutputError::InvalidSchema(format!(
                "Unsupported $ref format: '{}'. Only local references (#/definitions/ or #/$defs/) are supported",
                ref_str
            )));
        };

        let definition = self
            .definitions
            .get(definition_name)
            .ok_or_else(|| {
                StructuredOutputError::InvalidSchema(format!(
                    "Definition '{}' not found in schema definitions",
                    definition_name
                ))
            })?
            .clone();

        self.schema_to_rule(name, &definition)
    }

    /// Handle oneOf (union type - match exactly one of the schemas)
    fn one_of_to_rule(
        &mut self,
        name: &str,
        one_of: &Value,
    ) -> Result<String, StructuredOutputError> {
        let schemas = one_of
            .as_array()
            .ok_or_else(|| StructuredOutputError::InvalidSchema("oneOf must be an array".into()))?;

        if schemas.is_empty() {
            return Err(StructuredOutputError::InvalidSchema(
                "oneOf must have at least one schema".into(),
            ));
        }

        if schemas.len() == 1 {
            return self.schema_to_rule(name, &schemas[0]);
        }

        let mut alternatives = Vec::new();
        for (i, sub_schema) in schemas.iter().enumerate() {
            let rule_name = self.generate_rule_name(&format!("{}_oneof_{}", name, i));
            let rule = self.schema_to_rule(&rule_name, sub_schema)?;
            self.rules.push(rule);
            alternatives.push(rule_name);
        }

        Ok(format!("{} ::= {}", name, alternatives.join(" | ")))
    }

    /// Handle anyOf (union type - match at least one schema)
    fn any_of_to_rule(
        &mut self,
        name: &str,
        any_of: &Value,
    ) -> Result<String, StructuredOutputError> {
        let schemas = any_of
            .as_array()
            .ok_or_else(|| StructuredOutputError::InvalidSchema("anyOf must be an array".into()))?;

        if schemas.is_empty() {
            return Err(StructuredOutputError::InvalidSchema(
                "anyOf must have at least one schema".into(),
            ));
        }

        if schemas.len() == 1 {
            return self.schema_to_rule(name, &schemas[0]);
        }

        let mut alternatives = Vec::new();
        for (i, sub_schema) in schemas.iter().enumerate() {
            let rule_name = self.generate_rule_name(&format!("{}_anyof_{}", name, i));
            let rule = self.schema_to_rule(&rule_name, sub_schema)?;
            self.rules.push(rule);
            alternatives.push(rule_name);
        }

        Ok(format!("{} ::= {}", name, alternatives.join(" | ")))
    }

    /// Convert a regex pattern to a GBNF rule for string constraints
    fn pattern_to_rule(
        &mut self,
        name: &str,
        pattern: &str,
    ) -> Result<String, StructuredOutputError> {
        let gbnf_rule = if pattern.starts_with('^') && pattern.ends_with('$') {
            &pattern[1..pattern.len() - 1]
        } else {
            pattern
        };

        let char_rule = self.generate_rule_name(&format!("{}_char", name));

        if gbnf_rule == "\\d+" || gbnf_rule == "[0-9]+" {
            self.rules.push(format!("{} ::= [0-9]", char_rule));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else if gbnf_rule == "\\d*" || gbnf_rule == "[0-9]*" {
            self.rules.push(format!("{} ::= [0-9]", char_rule));
            Ok(format!("{} ::= \"\\\"\" ({})* \"\\\"\"", name, char_rule))
        } else if gbnf_rule.starts_with("[a-zA-Z") || gbnf_rule.starts_with("[A-Za-z") {
            self.rules.push(format!("{} ::= [a-zA-Z]", char_rule));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else if gbnf_rule == "[a-z]+" || gbnf_rule == "[a-zA-Z]+" {
            self.rules.push(format!("{} ::= [a-zA-Z]", char_rule));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else if gbnf_rule.starts_with("[0-9a-fA-F") || gbnf_rule.starts_with("[0-9A-Fa-f") {
            self.rules.push(format!("{} ::= [0-9a-fA-F]", char_rule));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else if gbnf_rule.starts_with("[^") && gbnf_rule.ends_with(']') {
            let negated_content = &gbnf_rule[2..gbnf_rule.len() - 1];
            self.rules
                .push(format!("{} ::= [^{}\"\\\\]", char_rule, negated_content));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else if gbnf_rule.starts_with('[') && gbnf_rule.ends_with(']') {
            let content = &gbnf_rule[1..gbnf_rule.len() - 1];
            self.rules.push(format!("{} ::= [{}]", char_rule, content));
            Ok(format!("{} ::= \"\\\"\" {}+ \"\\\"\"", name, char_rule))
        } else {
            Ok(format!("{} ::= string", name))
        }
    }
}

impl Default for JsonSchemaConverter {
    fn default() -> Self {
        Self::new()
    }
}

/// Escape a string for use in GBNF
fn escape_gbnf_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Get a grammar for generic JSON output
pub fn json_grammar() -> Result<Grammar, MullamaError> {
    crate::grammar::presets::json()
}

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

    #[test]
    fn test_simple_object_schema() {
        let schema = json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "age": { "type": "integer" }
            },
            "required": ["name"]
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_enum_schema() {
        let schema = json!({
            "type": "string",
            "enum": ["red", "green", "blue"]
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_array_schema() {
        let schema = json!({
            "type": "array",
            "items": { "type": "string" }
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_nested_object() {
        let schema = json!({
            "type": "object",
            "properties": {
                "person": {
                    "type": "object",
                    "properties": {
                        "name": { "type": "string" }
                    }
                }
            }
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_unsupported_feature_ref() {
        // $ref is now supported when definitions are provided
        let schema = json!({
            "$ref": "#/definitions/Person",
            "definitions": {
                "Person": {
                    "type": "object",
                    "properties": {
                        "name": { "type": "string" }
                    }
                }
            }
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_ref_not_found() {
        let schema = json!({
            "$ref": "#/definitions/NonExistent"
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_err());
    }

    #[test]
    fn test_oneof_schema() {
        let schema = json!({
            "oneOf": [
                { "type": "string" },
                { "type": "number" }
            ]
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_anyof_schema() {
        let schema = json!({
            "anyOf": [
                { "type": "string" },
                { "type": "integer" }
            ]
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_pattern_string() {
        let schema = json!({
            "type": "string",
            "pattern": "^[0-9]+$"
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_unsupported_allof() {
        let schema = json!({
            "allOf": [
                { "type": "string" },
                { "type": "number" }
            ]
        });

        let result = JsonSchemaConverter::validate_schema(&schema);
        assert!(result.is_err());
    }

    #[test]
    fn test_integer_range() {
        let schema = json!({
            "type": "integer",
            "minimum": 1,
            "maximum": 10
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_boolean_schema() {
        let schema = json!({
            "type": "boolean"
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }

    #[test]
    fn test_const_schema() {
        let schema = json!({
            "const": "fixed_value"
        });

        let result = JsonSchemaConverter::convert(&schema);
        assert!(result.is_ok());
    }
}