lp_parser_rs 3.4.1

A Rust parser for the LP file format.
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
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
//! LP Parser CLI - Parse, analyze, convert, and solve Linear Programming files.

mod cli;

use std::fs;
use std::io::{self, Stdout, Write};
use std::path::PathBuf;

use clap::Parser;
#[cfg(feature = "diff")]
use cli::DiffArgs;
use cli::{AnalyzeArgs, Cli, Commands, ConvertArgs, ConvertFormat, InfoArgs, OutputFormat, ParseArgs};
#[cfg(feature = "lp-solvers")]
use cli::{SolveArgs, Solver};
use lp_parser_rs::analysis::AnalysisConfig;
use lp_parser_rs::model::{Constraint, VariableType};
use lp_parser_rs::parser::parse_file;
use lp_parser_rs::problem::LpProblem;

type BoxError = Box<dyn std::error::Error>;

fn cmd_parse(args: ParseArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    let content = parse_file(&args.file)?;
    let problem = LpProblem::parse(&content)?;

    if !quiet && verbose > 0 {
        eprintln!("Parsed file: {}", args.file.display());
        eprintln!(
            "Problem: {} objectives, {} constraints, {} variables",
            problem.objective_count(),
            problem.constraint_count(),
            problem.variable_count()
        );
    }

    let mut writer = OutputWriter::new(args.output)?;

    match args.format {
        OutputFormat::Text => {
            writeln!(writer, "{problem}")?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Json => {
            if args.pretty {
                serde_json::to_writer_pretty(&mut writer, &problem)?;
            } else {
                serde_json::to_writer(&mut writer, &problem)?;
            }
            writeln!(writer)?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Yaml => {
            serde_yaml::to_writer(&mut writer, &problem)?;
        }
    }

    Ok(())
}

fn cmd_info(args: &InfoArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    let content = parse_file(&args.file)?;
    let problem = LpProblem::parse(&content)?;

    if !quiet && verbose > 0 {
        eprintln!("Analyzing file: {}", args.file.display());
    }

    let mut writer = OutputWriter::new(args.output.clone())?;

    match args.format {
        OutputFormat::Text => {
            write_info_text(&mut writer, &problem, args)?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Json => {
            let info = build_info_struct(&problem, args);
            if args.pretty {
                serde_json::to_writer_pretty(&mut writer, &info)?;
            } else {
                serde_json::to_writer(&mut writer, &info)?;
            }
            writeln!(writer)?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Yaml => {
            let info = build_info_struct(&problem, args);
            serde_yaml::to_writer(&mut writer, &info)?;
        }
    }

    Ok(())
}

fn cmd_analyze(args: AnalyzeArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    let content = parse_file(&args.file)?;
    let problem = LpProblem::parse(&content)?;

    if !quiet && verbose > 0 {
        eprintln!("Analyzing file: {}", args.file.display());
    }

    let config = AnalysisConfig {
        large_coefficient_threshold: args.large_coeff_threshold,
        small_coefficient_threshold: args.small_coeff_threshold,
        large_rhs_threshold: args.large_coeff_threshold,
        coefficient_ratio_threshold: args.ratio_threshold,
    };

    let analysis = problem.analyze_with_config(&config);

    let mut writer = OutputWriter::new(args.output)?;

    match args.format {
        OutputFormat::Text => {
            if args.issues_only {
                if analysis.issues.is_empty() {
                    writeln!(writer, "No issues detected.")?;
                } else {
                    writeln!(writer, "Issues Found: {}", analysis.issues.len())?;
                    for issue in &analysis.issues {
                        writeln!(writer, "  {issue}")?;
                    }
                }
            } else {
                write!(writer, "{analysis}")?;
            }
        }
        #[cfg(feature = "serde")]
        OutputFormat::Json => {
            if args.issues_only {
                if args.pretty {
                    serde_json::to_writer_pretty(&mut writer, &analysis.issues)?;
                } else {
                    serde_json::to_writer(&mut writer, &analysis.issues)?;
                }
            } else if args.pretty {
                serde_json::to_writer_pretty(&mut writer, &analysis)?;
            } else {
                serde_json::to_writer(&mut writer, &analysis)?;
            }
            writeln!(writer)?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Yaml => {
            if args.issues_only {
                serde_yaml::to_writer(&mut writer, &analysis.issues)?;
            } else {
                serde_yaml::to_writer(&mut writer, &analysis)?;
            }
        }
    }

    Ok(())
}

fn write_info_text<W: Write>(writer: &mut W, problem: &LpProblem, args: &InfoArgs) -> Result<(), BoxError> {
    writeln!(writer, "=== LP Problem Info ===")?;

    if let Some(name) = problem.name() {
        writeln!(writer, "Name: {name}")?;
    }
    writeln!(writer, "Sense: {}", problem.sense)?;
    writeln!(writer, "Objectives: {}", problem.objective_count())?;
    writeln!(writer, "Constraints: {}", problem.constraint_count())?;
    writeln!(writer, "Variables: {}", problem.variable_count())?;

    // Count variable types
    let mut binary_count = 0;
    let mut integer_count = 0;
    let mut continuous_count = 0;

    for var in problem.variables.values() {
        match var.var_type {
            VariableType::Binary => binary_count += 1,
            VariableType::Integer => integer_count += 1,
            _ => continuous_count += 1,
        }
    }

    writeln!(writer)?;
    writeln!(writer, "Variable Types:")?;
    writeln!(writer, "  Continuous: {continuous_count}")?;
    writeln!(writer, "  Integer: {integer_count}")?;
    writeln!(writer, "  Binary: {binary_count}")?;

    if args.objectives {
        writeln!(writer)?;
        writeln!(writer, "Objectives:")?;
        for (name_id, obj) in &problem.objectives {
            let name = problem.resolve(*name_id);
            writeln!(writer, "  {name}: {} terms", obj.coefficients.len())?;
        }
    }

    if args.constraints {
        writeln!(writer)?;
        writeln!(writer, "Constraints:")?;
        for (name_id, constr) in &problem.constraints {
            let name = problem.resolve(*name_id);
            match constr {
                Constraint::Standard { coefficients, operator, rhs, .. } => {
                    writeln!(writer, "  {name}: {} terms {operator} {rhs}", coefficients.len())?;
                }
                Constraint::SOS { sos_type, weights, .. } => {
                    writeln!(writer, "  {name}: {sos_type} with {} variables", weights.len())?;
                }
            }
        }
    }

    if args.variables {
        writeln!(writer)?;
        writeln!(writer, "Variables:")?;
        for (name_id, var) in &problem.variables {
            let name = problem.resolve(*name_id);
            writeln!(writer, "  {name}: {:?}", var.var_type)?;
        }
    }

    Ok(())
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct ProblemInfo {
    name: Option<String>,
    sense: String,
    objective_count: usize,
    constraint_count: usize,
    variable_count: usize,
    variable_types: VariableTypeCounts,
    #[serde(skip_serializing_if = "Option::is_none")]
    objectives: Option<Vec<ObjectiveInfo>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    constraints: Option<Vec<ConstraintInfo>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    variables: Option<Vec<VariableInfo>>,
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct VariableTypeCounts {
    continuous: usize,
    integer: usize,
    binary: usize,
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct ObjectiveInfo {
    name: String,
    term_count: usize,
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct ConstraintInfo {
    name: String,
    constraint_type: String,
    details: String,
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct VariableInfo {
    name: String,
    var_type: String,
}

#[cfg(feature = "serde")]
fn build_info_struct(problem: &LpProblem, args: &InfoArgs) -> ProblemInfo {
    let mut binary_count = 0;
    let mut integer_count = 0;
    let mut continuous_count = 0;

    for var in problem.variables.values() {
        match var.var_type {
            VariableType::Binary => binary_count += 1,
            VariableType::Integer => integer_count += 1,
            _ => continuous_count += 1,
        }
    }

    let objectives = if args.objectives {
        Some(
            problem
                .objectives
                .iter()
                .map(|(name_id, obj)| ObjectiveInfo { name: problem.resolve(*name_id).to_string(), term_count: obj.coefficients.len() })
                .collect(),
        )
    } else {
        None
    };

    let constraints = if args.constraints {
        Some(
            problem
                .constraints
                .iter()
                .map(|(name_id, constr)| {
                    let name = problem.resolve(*name_id).to_string();
                    match constr {
                        Constraint::Standard { coefficients, operator, rhs, .. } => ConstraintInfo {
                            name,
                            constraint_type: "standard".to_string(),
                            details: format!("{} terms {} {}", coefficients.len(), operator, rhs),
                        },
                        Constraint::SOS { sos_type, weights, .. } => ConstraintInfo {
                            name,
                            constraint_type: "sos".to_string(),
                            details: format!("{} with {} variables", sos_type, weights.len()),
                        },
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    let variables = if args.variables {
        Some(
            problem
                .variables
                .iter()
                .map(|(name_id, var)| VariableInfo { name: problem.resolve(*name_id).to_string(), var_type: format!("{:?}", var.var_type) })
                .collect(),
        )
    } else {
        None
    };

    ProblemInfo {
        name: problem.name().map(String::from),
        sense: format!("{}", problem.sense),
        objective_count: problem.objective_count(),
        constraint_count: problem.constraint_count(),
        variable_count: problem.variable_count(),
        variable_types: VariableTypeCounts { continuous: continuous_count, integer: integer_count, binary: binary_count },
        objectives,
        constraints,
        variables,
    }
}

#[cfg(feature = "diff")]
fn cmd_diff(args: DiffArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    use std::collections::{BTreeMap, BTreeSet, HashMap};

    use lp_parser_rs::interner::NameId;
    use lp_parser_rs::model::{Coefficient, ComparisonOp, Constraint};

    // Parse rename rules
    if args.rename.len() % 2 != 0 {
        return Err("--rename requires pairs of PATTERN REPLACEMENT".into());
    }
    let rules: Vec<(regex::Regex, String)> =
        args.rename.chunks_exact(2).map(|c| Ok::<_, BoxError>((regex::Regex::new(&c[0])?, c[1].clone()))).collect::<Result<_, _>>()?;

    let rewrite = |name: &str| -> String {
        let mut s = name.to_string();
        for (re, rep) in &rules {
            s = re.replace_all(&s, rep.as_str()).into_owned();
        }
        s
    };

    let abs_tol = args.abs_tol;
    let rel_tol = args.rel_tol;
    let differs = |a: f64, b: f64| -> bool {
        let diff = (a - b).abs();
        if diff == 0.0 {
            return false;
        }
        let scale = a.abs().max(b.abs());
        diff > abs_tol && diff > rel_tol * scale
    };

    if !quiet && verbose > 0 {
        eprintln!("Diffing {} vs {}", args.file1.display(), args.file2.display());
        eprintln!("abs_tol={abs_tol} rel_tol={rel_tol} rename_rules={}", rules.len());
    }

    let content1 = parse_file(&args.file1)?;
    let content2 = parse_file(&args.file2)?;
    let p1 = LpProblem::parse(&content1)?;
    let p2 = LpProblem::parse(&content2)?;

    // --- build canonical-name -> NameId maps --------------------------------
    let canon_ids = |problem: &LpProblem, ids: &[NameId]| -> HashMap<String, NameId> {
        ids.iter().map(|id| (rewrite(problem.resolve(*id)), *id)).collect()
    };

    let cvars1 = canon_ids(&p1, &p1.variables.keys().copied().collect::<Vec<_>>());
    let cvars2 = canon_ids(&p2, &p2.variables.keys().copied().collect::<Vec<_>>());
    let ccons1: HashMap<String, NameId> = p1.constraints.values().map(|c| (rewrite(p1.resolve(c.name())), c.name())).collect();
    let ccons2: HashMap<String, NameId> = p2.constraints.values().map(|c| (rewrite(p2.resolve(c.name())), c.name())).collect();
    let cobjs1 = canon_ids(&p1, &p1.objectives.keys().copied().collect::<Vec<_>>());
    let cobjs2 = canon_ids(&p2, &p2.objectives.keys().copied().collect::<Vec<_>>());

    let set_of = |m: &HashMap<String, NameId>| -> BTreeSet<String> { m.keys().cloned().collect() };
    let vars1 = set_of(&cvars1);
    let vars2 = set_of(&cvars2);
    let cons1 = set_of(&ccons1);
    let cons2 = set_of(&ccons2);
    let objs1 = set_of(&cobjs1);
    let objs2 = set_of(&cobjs2);

    let vars_added: Vec<String> = vars2.difference(&vars1).cloned().collect();
    let vars_removed: Vec<String> = vars1.difference(&vars2).cloned().collect();
    let cons_added: Vec<String> = cons2.difference(&cons1).cloned().collect();
    let cons_removed: Vec<String> = cons1.difference(&cons2).cloned().collect();
    let objs_added: Vec<String> = objs2.difference(&objs1).cloned().collect();
    let objs_removed: Vec<String> = objs1.difference(&objs2).cloned().collect();

    // Coefficient map keyed by canonical variable name.
    let coeff_map = |problem: &LpProblem, coeffs: &[Coefficient]| -> BTreeMap<String, f64> {
        coeffs.iter().map(|c| (rewrite(problem.resolve(c.name)), c.value)).collect()
    };

    let op_str = |op: ComparisonOp| -> &'static str {
        match op {
            ComparisonOp::LTE => "<=",
            ComparisonOp::GTE => ">=",
            ComparisonOp::LT => "<",
            ComparisonOp::GT => ">",
            ComparisonOp::EQ => "=",
        }
    };

    let mut cons_modified: Vec<(String, Vec<String>)> = Vec::new();
    for name in cons1.intersection(&cons2) {
        let c1 = &p1.constraints[&ccons1[name]];
        let c2 = &p2.constraints[&ccons2[name]];
        let mut changes = Vec::new();
        match (c1, c2) {
            (
                Constraint::Standard { coefficients: cf1, operator: op1, rhs: r1, .. },
                Constraint::Standard { coefficients: cf2, operator: op2, rhs: r2, .. },
            ) => {
                if op1 != op2 {
                    changes.push(format!("operator {} -> {}", op_str(*op1), op_str(*op2)));
                }
                if differs(*r1, *r2) {
                    changes.push(format!("rhs {r1} -> {r2}"));
                }
                let m1 = coeff_map(&p1, cf1);
                let m2 = coeff_map(&p2, cf2);
                let mut coef_diffs = 0usize;
                for (k, v1) in &m1 {
                    match m2.get(k) {
                        Some(v2) if differs(*v1, *v2) => coef_diffs += 1,
                        None => coef_diffs += 1,
                        _ => {}
                    }
                }
                for k in m2.keys() {
                    if !m1.contains_key(k) {
                        coef_diffs += 1;
                    }
                }
                if coef_diffs > 0 {
                    changes.push(format!("{coef_diffs} coefficient change(s)"));
                }
            }
            (Constraint::SOS { .. }, Constraint::SOS { .. }) => {
                if c1 != c2 {
                    changes.push("SOS definition changed".to_string());
                }
            }
            _ => changes.push("constraint kind changed (Standard <-> SOS)".to_string()),
        }
        if !changes.is_empty() {
            cons_modified.push((name.clone(), changes));
        }
    }

    let mut objs_modified: Vec<(String, Vec<String>)> = Vec::new();
    for name in objs1.intersection(&objs2) {
        let o1 = &p1.objectives[&cobjs1[name]];
        let o2 = &p2.objectives[&cobjs2[name]];
        let m1 = coeff_map(&p1, &o1.coefficients);
        let m2 = coeff_map(&p2, &o2.coefficients);
        let mut coef_diffs = 0usize;
        for (k, v1) in &m1 {
            match m2.get(k) {
                Some(v2) if differs(*v1, *v2) => coef_diffs += 1,
                None => coef_diffs += 1,
                _ => {}
            }
        }
        for k in m2.keys() {
            if !m1.contains_key(k) {
                coef_diffs += 1;
            }
        }
        if coef_diffs > 0 {
            objs_modified.push((name.clone(), vec![format!("{coef_diffs} coefficient change(s)")]));
        }
    }

    let mut vars_type_changed: Vec<(String, String, String)> = Vec::new();
    for name in vars1.intersection(&vars2) {
        let t1 = &p1.variables[&cvars1[name]].var_type;
        let t2 = &p2.variables[&cvars2[name]].var_type;
        if t1 != t2 {
            vars_type_changed.push((name.clone(), format!("{t1:?}"), format!("{t2:?}")));
        }
    }

    let mut writer = OutputWriter::new(args.output)?;
    match args.format {
        OutputFormat::Text => {
            writeln!(writer, "=== LP Diff ===")?;
            writeln!(writer, "file1: {}", args.file1.display())?;
            writeln!(writer, "file2: {}", args.file2.display())?;
            writeln!(writer, "abs_tol: {abs_tol}  rel_tol: {rel_tol}  rename_rules: {}", rules.len())?;
            writeln!(writer)?;
            writeln!(writer, "Sense: {:?} -> {:?}", p1.sense, p2.sense)?;
            writeln!(
                writer,
                "Counts: objectives {} -> {}, constraints {} -> {}, variables {} -> {}",
                p1.objective_count(),
                p2.objective_count(),
                p1.constraint_count(),
                p2.constraint_count(),
                p1.variable_count(),
                p2.variable_count()
            )?;
            writeln!(writer)?;

            let limit = 50usize;
            let fmt_list = |w: &mut OutputWriter, label: &str, items: &[String]| -> io::Result<()> {
                writeln!(w, "{label} ({}):", items.len())?;
                for name in items.iter().take(limit) {
                    writeln!(w, "  {name}")?;
                }
                if items.len() > limit {
                    writeln!(w, "  ... ({} more)", items.len() - limit)?;
                }
                Ok(())
            };

            fmt_list(&mut writer, "Variables added", &vars_added)?;
            fmt_list(&mut writer, "Variables removed", &vars_removed)?;
            writeln!(writer, "Variables with changed type ({}):", vars_type_changed.len())?;
            for (n, a, b) in vars_type_changed.iter().take(limit) {
                writeln!(writer, "  {n}: {a} -> {b}")?;
            }

            fmt_list(&mut writer, "Constraints added", &cons_added)?;
            fmt_list(&mut writer, "Constraints removed", &cons_removed)?;
            writeln!(writer, "Constraints modified ({}):", cons_modified.len())?;
            for (n, changes) in cons_modified.iter().take(limit) {
                writeln!(writer, "  {n}: {}", changes.join("; "))?;
            }
            if cons_modified.len() > limit {
                writeln!(writer, "  ... ({} more)", cons_modified.len() - limit)?;
            }

            fmt_list(&mut writer, "Objectives added", &objs_added)?;
            fmt_list(&mut writer, "Objectives removed", &objs_removed)?;
            writeln!(writer, "Objectives modified ({}):", objs_modified.len())?;
            for (n, changes) in objs_modified.iter().take(limit) {
                writeln!(writer, "  {n}: {}", changes.join("; "))?;
            }
        }
        #[cfg(feature = "serde")]
        OutputFormat::Json | OutputFormat::Yaml => {
            let summary = serde_json::json!({
                "file1": args.file1.display().to_string(),
                "file2": args.file2.display().to_string(),
                "abs_tol": abs_tol,
                "rel_tol": rel_tol,
                "rename_rule_count": rules.len(),
                "counts": {
                    "objectives": [p1.objective_count(), p2.objective_count()],
                    "constraints": [p1.constraint_count(), p2.constraint_count()],
                    "variables": [p1.variable_count(), p2.variable_count()],
                },
                "variables_added": vars_added,
                "variables_removed": vars_removed,
                "variables_type_changed": vars_type_changed,
                "constraints_added": cons_added,
                "constraints_removed": cons_removed,
                "constraints_modified": cons_modified,
                "objectives_added": objs_added,
                "objectives_removed": objs_removed,
                "objectives_modified": objs_modified,
            });
            match args.format {
                OutputFormat::Json => {
                    if args.pretty {
                        serde_json::to_writer_pretty(&mut writer, &summary)?;
                    } else {
                        serde_json::to_writer(&mut writer, &summary)?;
                    }
                    writeln!(writer)?;
                }
                OutputFormat::Yaml => {
                    serde_yaml::to_writer(&mut writer, &summary)?;
                }
                OutputFormat::Text => unreachable!(),
            }
        }
    }

    Ok(())
}

fn cmd_convert(args: ConvertArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    use lp_parser_rs::writer::{LpWriterOptions, write_lp_string_with_options};

    let content = parse_file(&args.file)?;
    let problem = LpProblem::parse(&content)?;

    if !quiet && verbose > 0 {
        eprintln!("Converting file: {}", args.file.display());
    }

    match args.format {
        ConvertFormat::Lp => {
            let options = LpWriterOptions {
                include_problem_name: !args.no_problem_name,
                max_line_length: args.max_line_length,
                decimal_precision: args.precision,
                include_section_spacing: !args.compact,
            };
            let output = write_lp_string_with_options(&problem, &options)?;

            let mut writer = OutputWriter::new(args.output)?;
            write!(writer, "{output}")?;
        }
        #[cfg(feature = "csv")]
        ConvertFormat::Csv => {
            use lp_parser_rs::csv::LpCsvWriter;

            let dir = args.output.ok_or("CSV output requires --output directory")?;
            if !dir.exists() {
                fs::create_dir_all(&dir)?;
            }
            problem.to_csv(&dir)?;

            if !quiet {
                eprintln!("CSV files written to: {}", dir.display());
            }
        }
        #[cfg(feature = "serde")]
        ConvertFormat::Json => {
            let mut writer = OutputWriter::new(args.output)?;
            if args.pretty {
                serde_json::to_writer_pretty(&mut writer, &problem)?;
            } else {
                serde_json::to_writer(&mut writer, &problem)?;
            }
            writeln!(writer)?;
        }
        #[cfg(feature = "serde")]
        ConvertFormat::Yaml => {
            let mut writer = OutputWriter::new(args.output)?;
            serde_yaml::to_writer(&mut writer, &problem)?;
        }
    }

    Ok(())
}

#[cfg(feature = "lp-solvers")]
fn cmd_solve(args: SolveArgs, verbose: u8, quiet: bool) -> Result<(), BoxError> {
    use lp_parser_rs::compat::lp_solvers::ToLpSolvers;
    use lp_solvers::solvers::{CbcSolver, GlpkSolver, SolverTrait, Status};

    let content = parse_file(&args.file)?;
    let problem = LpProblem::parse(&content)?;

    if !quiet && verbose > 0 {
        eprintln!("Loading problem: {}", args.file.display());
    }

    let compat = problem.to_lp_solvers()?;

    // Print warnings
    for warning in compat.warnings() {
        if !quiet {
            eprintln!("Warning: {warning}");
        }
    }

    if !quiet && verbose > 0 {
        eprintln!("Solving with {:?}...", args.solver);
    }

    let solution = match args.solver {
        Solver::Cbc => {
            let solver = CbcSolver::new();
            solver.run(&compat)?
        }
        Solver::Glpk => {
            let solver = GlpkSolver::new();
            solver.run(&compat)?
        }
        Solver::Gurobi | Solver::Cplex => {
            return Err(
                format!("{:?} solver requires commercial license - use 'cbc' or 'glpk' for open-source alternatives", args.solver).into()
            );
        }
    };

    let mut writer = OutputWriter::new(args.output)?;

    match args.format {
        OutputFormat::Text => {
            writeln!(writer, "=== Solution ===")?;
            writeln!(writer, "Status: {:?}", solution.status)?;
            match solution.status {
                Status::Optimal | Status::SubOptimal => {
                    writeln!(writer)?;
                    writeln!(writer, "Variables:")?;
                    for (name, value) in &solution.results {
                        writeln!(writer, "  {name} = {value}")?;
                    }
                }
                Status::TimeLimit | Status::MipGap | Status::Infeasible | Status::Unbounded | Status::NotSolved => {}
            }
        }
        #[cfg(feature = "serde")]
        OutputFormat::Json => {
            let status_str = match solution.status {
                Status::Optimal => "optimal",
                Status::SubOptimal => "suboptimal",
                Status::Infeasible => "infeasible",
                Status::Unbounded => "unbounded",
                Status::NotSolved => "not_solved",
                Status::TimeLimit => "time_limit",
                Status::MipGap => "mip_gap",
            };
            let solution_json = serde_json::json!({
                "status": status_str,
                "variables": solution.results
            });
            if args.pretty {
                serde_json::to_writer_pretty(&mut writer, &solution_json)?;
            } else {
                serde_json::to_writer(&mut writer, &solution_json)?;
            }
            writeln!(writer)?;
        }
        #[cfg(feature = "serde")]
        OutputFormat::Yaml => {
            let status_str = match solution.status {
                Status::Optimal => "optimal",
                Status::SubOptimal => "suboptimal",
                Status::Infeasible => "infeasible",
                Status::Unbounded => "unbounded",
                Status::NotSolved => "not_solved",
                Status::TimeLimit => "time_limit",
                Status::MipGap => "mip_gap",
            };
            let solution_yaml = serde_json::json!({
                "status": status_str,
                "variables": solution.results
            });
            serde_yaml::to_writer(&mut writer, &solution_yaml)?;
        }
    }

    Ok(())
}

enum OutputWriter {
    Stdout(Stdout),
    File(fs::File),
}

impl OutputWriter {
    fn new(path: Option<PathBuf>) -> io::Result<Self> {
        match path {
            Some(p) => Ok(Self::File(fs::File::create(p)?)),
            None => Ok(Self::Stdout(io::stdout())),
        }
    }
}

impl Write for OutputWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            Self::Stdout(s) => s.write(buf),
            Self::File(f) => f.write(buf),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match self {
            Self::Stdout(s) => s.flush(),
            Self::File(f) => f.flush(),
        }
    }
}

fn main() -> Result<(), BoxError> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Parse(args) => cmd_parse(args, cli.verbose, cli.quiet),
        Commands::Info(args) => cmd_info(&args, cli.verbose, cli.quiet),
        Commands::Analyze(args) => cmd_analyze(args, cli.verbose, cli.quiet),
        #[cfg(feature = "diff")]
        Commands::Diff(args) => cmd_diff(args, cli.verbose, cli.quiet),
        Commands::Convert(args) => cmd_convert(args, cli.verbose, cli.quiet),
        #[cfg(feature = "lp-solvers")]
        Commands::Solve(args) => cmd_solve(args, cli.verbose, cli.quiet),
    }
}