Skip to main content

gurobi/
error.rs

1// Copyright (c) 2016 Yusuke Sasaki
2//
3// This software is released under the MIT License.
4// See http://opensource.org/licenses/mit-license.php or <LICENSE>.
5
6use ffi;
7use std;
8
9/// The error type for operations in Gurobi Rust API
10#[derive(Debug)]
11pub enum Error {
12  /// An exception returned from Gurobi C API
13  FromAPI(String, ffi::c_int),
14
15  /// See https://doc.rust-lang.org/std/ffi/struct.NulError.html
16  NulError(std::ffi::NulError),
17
18  /// Inconsistent argument dimensions
19  InconsitentDims,
20}
21
22impl From<std::ffi::NulError> for Error {
23  fn from(err: std::ffi::NulError) -> Error { Error::NulError(err) }
24}
25
26impl std::fmt::Display for Error {
27  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
28    match *self {
29      Error::FromAPI(ref message, code) => write!(f, "Error from API: {} ({})", message, code),
30      Error::InconsitentDims => write!(f, "Inconsistent argument dimensions"),
31      Error::NulError(ref err) => write!(f, "NulError: {}", err),
32    }
33  }
34}
35
36impl std::error::Error for Error {
37  fn description(&self) -> &str {
38    match *self {
39      Error::FromAPI(..) => "error from C API",
40      Error::NulError(ref err) => err.description(),
41      Error::InconsitentDims => "Inconsistent argument dimensions",
42    }
43  }
44}
45
46
47/// A specialized
48/// [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)
49/// type for operations in Gurobi Rust API
50pub type Result<T> = std::result::Result<T, Error>;