Skip to main content

cnvx_parse/
lib.rs

1//! # CNVX Parse
2//!
3//! This crate provides simple parsers for various LP file formats, including AMPL, GMPL, and MPS,
4//! built on top of [`cnvx_core`]. It defines a common [`LanguageParser`] trait and specific parsers for
5//! each format.
6//!
7//! # Modules
8//!
9//! - [`ampl`]: Parser for AMPL format.
10//! - [`gmpl`]: Parser for GMPL format.
11//! - [`mps`]: Parser for MPS format.
12
13pub mod ampl;
14pub mod gmpl;
15pub mod mps;
16
17use cnvx_core::Model;
18
19pub use ampl::AMPLLanguage;
20pub use gmpl::GMPLLanguage;
21pub use mps::MPSLanguage;
22
23/// Trait for parsers
24pub trait LanguageParser {
25    fn parse(&self, src: &str) -> Result<Model, String>;
26}
27
28pub fn parse(contents: &str, file_type: &str) -> Result<Model, String> {
29    let model = match file_type {
30        "ampl" => AMPLLanguage::new().parse(contents)?,
31        "gmpl" => GMPLLanguage::new().parse(contents)?,
32        "mps" => MPSLanguage::new().parse(contents)?,
33        _ => return Err(format!("unsupported file type: {}", file_type)),
34    };
35
36    Ok(model)
37}