Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Display and Error for all error types #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use std::fmt;
use std::error::Error;

/// The possible error conditions that `Solver::add_constraint` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum AddConstraintError {
/// The constraint specified has already been added to the solver.
DuplicateConstraint,
/// The constraint is required, but it is unsatisfiable in conjunction with the existing constraints.
UnsatisfiableConstraint,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

impl fmt::Display for AddConstraintError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let AddConstraintError::InternalSolverError(msg) = self {
write!(f, "internal solver error '{}'", msg)
} else {
write!(f, "{}", self.description())
}
}
}

impl Error for AddConstraintError {
fn description(&self) -> &str {
match self {
AddConstraintError::DuplicateConstraint =>
"constraint already added to the solver",
AddConstraintError::UnsatisfiableConstraint =>
"could not satisfy all required constraints",
AddConstraintError::InternalSolverError(_) =>
"internal solver error",
}
}
}

/// The possible error conditions that `Solver::remove_constraint` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum RemoveConstraintError {
/// The constraint specified was not already in the solver, so cannot be removed.
UnknownConstraint,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

impl fmt::Display for RemoveConstraintError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let RemoveConstraintError::InternalSolverError(msg) = self {
write!(f, "internal solver error '{}'", msg)
} else {
write!(f, "{}", self.description())
}
}
}

impl Error for RemoveConstraintError {
fn description(&self) -> &str {
match self {
RemoveConstraintError::UnknownConstraint =>
"constraint is not in the solver",
RemoveConstraintError::InternalSolverError(_) =>
"internal solver error",
}
}
}

/// The possible error conditions that `Solver::add_edit_variable` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum AddEditVariableError {
/// The specified variable is already marked as an edit variable in the solver.
DuplicateEditVariable,
/// The specified strength was `REQUIRED`. This is illegal for edit variable strengths.
BadRequiredStrength
}

impl fmt::Display for AddEditVariableError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}

impl Error for AddEditVariableError {
fn description(&self) -> &str {
match self {
AddEditVariableError::DuplicateEditVariable =>
"variable already marked as an edit variable",
AddEditVariableError::BadRequiredStrength =>
"invalid strength for edit variable",
}
}
}

/// The possible error conditions that `Solver::remove_edit_variable` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum RemoveEditVariableError {
/// The specified variable was not an edit variable in the solver, so cannot be removed.
UnknownEditVariable,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

impl fmt::Display for RemoveEditVariableError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let RemoveEditVariableError::InternalSolverError(msg) = self {
write!(f, "internal solver error '{}'", msg)
} else {
write!(f, "{}", self.description())
}
}
}

impl Error for RemoveEditVariableError {
fn description(&self) -> &str {
match self {
RemoveEditVariableError::UnknownEditVariable =>
"variable is not marked as an edit variable",
RemoveEditVariableError::InternalSolverError(_) =>
"internal solver error",
}
}
}

/// The possible error conditions that `Solver::suggest_value` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum SuggestValueError {
/// The specified variable was not an edit variable in the solver, so cannot have its value suggested.
UnknownEditVariable,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

impl fmt::Display for SuggestValueError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let SuggestValueError::InternalSolverError(msg) = self {
write!(f, "internal solver error '{}'", msg)
} else {
write!(f, "{}", self.description())
}
}
}

impl Error for SuggestValueError {
fn description(&self) -> &str {
match self {
SuggestValueError::UnknownEditVariable =>
"variable is not marked as an edit variable",
SuggestValueError::InternalSolverError(_) =>
"internal solver error",
}
}
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct InternalSolverError(pub &'static str);
59 changes: 5 additions & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,15 @@
//! One thing that this example exposes is that this crate is a rather low level library. It does not have
//! any inherent knowledge of user interfaces, directions or boxes. Thus for use in a user interface this
//! crate should ideally be wrapped by a higher level API, which is outside the scope of this crate.
pub use error::{AddConstraintError, RemoveConstraintError, AddEditVariableError,
RemoveEditVariableError, SuggestValueError};
pub use solver_impl::Solver;

use std::sync::Arc;
use std::collections::HashMap;
use std::collections::hash_map::{Entry};

mod error;
mod solver_impl;
mod operators;

Expand Down Expand Up @@ -566,58 +571,4 @@ impl Row {
}
}

/// The possible error conditions that `Solver::add_constraint` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum AddConstraintError {
/// The constraint specified has already been added to the solver.
DuplicateConstraint,
/// The constraint is required, but it is unsatisfiable in conjunction with the existing constraints.
UnsatisfiableConstraint,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

/// The possible error conditions that `Solver::remove_constraint` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum RemoveConstraintError {
/// The constraint specified was not already in the solver, so cannot be removed.
UnknownConstraint,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

/// The possible error conditions that `Solver::add_edit_variable` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum AddEditVariableError {
/// The specified variable is already marked as an edit variable in the solver.
DuplicateEditVariable,
/// The specified strength was `REQUIRED`. This is illegal for edit variable strengths.
BadRequiredStrength
}

/// The possible error conditions that `Solver::remove_edit_variable` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum RemoveEditVariableError {
/// The specified variable was not an edit variable in the solver, so cannot be removed.
UnknownEditVariable,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

/// The possible error conditions that `Solver::suggest_value` can fail with.
#[derive(Debug, Copy, Clone)]
pub enum SuggestValueError {
/// The specified variable was not an edit variable in the solver, so cannot have its value suggested.
UnknownEditVariable,
/// The solver entered an invalid state. If this occurs please report the issue. This variant specifies
/// additional details as a string.
InternalSolverError(&'static str)
}

#[derive(Debug, Copy, Clone)]
struct InternalSolverError(&'static str);

pub use solver_impl::Solver;
2 changes: 1 addition & 1 deletion src/solver_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use {
Row,
AddConstraintError,
RemoveConstraintError,
InternalSolverError,
SuggestValueError,
AddEditVariableError,
RemoveEditVariableError,
RelationalOperator,
near_zero
};
use error::InternalSolverError;

use ::std::rc::Rc;
use ::std::cell::RefCell;
Expand Down