-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from niklasdewally/issue/86
Better Error Handling
- Loading branch information
Showing
12 changed files
with
343 additions
and
133 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
//! Top-level error types for Conjure-Oxide. | ||
|
||
use serde_json::Error as JsonError; | ||
use thiserror::Error; | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("JSON parsing error: {0}")] | ||
JsonError(#[from] JsonError), | ||
#[error("Error constructing model: {0}")] | ||
ModelConstructError(String), | ||
#[error("JSON error: {0}")] | ||
JSON(#[from] JsonError), | ||
|
||
#[error("Error parsing model: {0}")] | ||
Parse(String), | ||
|
||
#[error("{0} is not yet implemented.")] | ||
NotImplemented(String), | ||
|
||
#[error(transparent)] | ||
Other(#[from] anyhow::Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,47 @@ | ||
use std::collections::HashMap; | ||
|
||
use conjure_oxide::ast::*; | ||
|
||
fn main() { | ||
// find a,b,c : int(1..3) | ||
// such that a + b + c = 4 | ||
// such that a >= b | ||
|
||
println!("Manual model:"); | ||
|
||
let a = Name::UserName(String::from("a")); | ||
let b = Name::UserName(String::from("b")); | ||
let c = Name::UserName(String::from("c")); | ||
|
||
let mut variables = HashMap::new(); | ||
variables.insert( | ||
a.clone(), | ||
DecisionVariable { | ||
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]), | ||
}, | ||
); | ||
variables.insert( | ||
b.clone(), | ||
DecisionVariable { | ||
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]), | ||
}, | ||
); | ||
variables.insert( | ||
c.clone(), | ||
DecisionVariable { | ||
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]), | ||
}, | ||
); | ||
|
||
let m = Model { | ||
variables, | ||
constraints: vec![ | ||
Expression::Eq( | ||
Box::new(Expression::Sum(vec![ | ||
Expression::Reference(a.clone()), | ||
Expression::Reference(b.clone()), | ||
Expression::Reference(c.clone()), | ||
])), | ||
Box::new(Expression::ConstantInt(4)), | ||
), | ||
Expression::Geq( | ||
Box::new(Expression::Reference(a.clone())), | ||
Box::new(Expression::Reference(b.clone())), | ||
), | ||
], | ||
}; | ||
|
||
println!("\n{:?}\n", m); | ||
// (niklasdewally): temporary, gut this if you want! | ||
|
||
use anyhow::{anyhow, bail}; | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result as AnyhowResult; | ||
use clap::{arg, command, Parser}; | ||
use conjure_oxide::parse::parse_json; | ||
#[derive(Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Cli { | ||
#[arg(long, value_name = "SOLVER")] | ||
solver: Option<String>, | ||
|
||
#[arg(value_name = "INPUT_ESSENCE")] | ||
input_file: PathBuf, | ||
} | ||
|
||
pub fn main() -> AnyhowResult<()> { | ||
let cli = Cli::parse(); | ||
println!("Input file: {}", cli.input_file.display()); | ||
let input_file: &str = cli.input_file.to_str().ok_or(anyhow!( | ||
"Given input_file could not be converted to a string" | ||
))?; | ||
|
||
/******************************************************/ | ||
/* Parse essence to json using Conjure */ | ||
/******************************************************/ | ||
|
||
let mut cmd = std::process::Command::new("conjure"); | ||
let output = cmd | ||
.arg("pretty") | ||
.arg("--output-format=astjson") | ||
.arg(input_file) | ||
.output()?; | ||
|
||
let conjure_stderr = String::from_utf8(output.stderr)?; | ||
if !conjure_stderr.is_empty() { | ||
bail!(conjure_stderr); | ||
} | ||
|
||
let astjson = String::from_utf8(output.stdout)?; | ||
|
||
let model = parse_json(&astjson)?; | ||
println!("{:?}", model); | ||
Ok(()) | ||
} |
Oops, something went wrong.