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
/*
* Copyright (c) 2023 Andrew Rowan Barlow. Licensed under the EUPL-1.2
* or later. You may obtain a copy of the licence at
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12. A copy
* of the EUPL-1.2 licence in English is given in LICENCE.txt which is
* found in the root directory of this repository.
*
* Author: Andrew Rowan Barlow <a.barlow.dev@gmail.com>
*/

//! Custom errors that result from the incorrect use of the quantr library.

use std::error::Error;
use std::fmt;

/// Relays error messages resulting from quantr.
pub struct QuantrError {
    pub(crate) message: String,
}

impl QuantrError {
    /// Returns the error message.
    ///
    /// # Example
    /// ```
    /// use quantr::Circuit;
    ///
    /// if let Err(quantr_err) = Circuit::new(0) {
    ///     assert_eq!("The initiliased circuit must have at least one wire.", quantr_err.get_msg());
    /// }
    /// ```
    pub fn get_msg(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for QuantrError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "\x1b[91m[Quantr Error] {}\x1b[0m ", self.message)
    }
}

impl fmt::Debug for QuantrError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self, f)
    }
}

impl Error for QuantrError {}