aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-core/src/dependency_resolver/error.rs
blob: e4b9841941b70707a6f2ef296c335821cfdcf61c (plain)
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
use std::fmt;

/// A bug in the solver itself (should never happen in normal operation).
/// Equivalent to Composer's SolverBugException.
#[derive(Debug, Clone)]
pub struct SolverBugError {
    pub message: String,
}

impl fmt::Display for SolverBugError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Solver bug: {}", self.message)
    }
}

impl std::error::Error for SolverBugError {}

/// Errors produced by the SAT solver.
#[derive(Debug)]
pub enum SolverError {
    /// Internal solver bug (should never happen).
    Bug(SolverBugError),
    /// The dependency set is unsolvable. Contains problem descriptions.
    Unsolvable(Vec<String>),
}

impl fmt::Display for SolverError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SolverError::Bug(e) => write!(f, "{e}"),
            SolverError::Unsolvable(problems) => {
                for (i, problem) in problems.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "  Problem {}: {problem}", i + 1)?;
                }
                Ok(())
            }
        }
    }
}

impl std::error::Error for SolverError {}

impl From<SolverBugError> for SolverError {
    fn from(e: SolverBugError) -> Self {
        SolverError::Bug(e)
    }
}