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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::{num, fmt, error::Error};
use crate::{
    ContextHandle, Contextual, Face, AtomID, NodeID, PortID, LinkID, ForkID, JoinID, Multiplicity,
    Capacity,
};

#[derive(Clone, Debug)]
pub enum AcesErrorKind {
    ContextMismatch,
    PolynomialFaceMismatch,
    HarcNotAForkMismatch(JoinID),
    HarcNotAJoinMismatch(ForkID),
    NodeMissingForID(NodeID),
    AtomMissingForID(AtomID),
    PortMissingForID(PortID),
    LinkMissingForID(LinkID),
    HarcMissingForID(AtomID),
    ForkMissingForID(ForkID),
    JoinMissingForID(JoinID),
    BottomAtomAccess,
    AtomicsNotOrdered,

    NodeMissingForPort(Face),
    NodeMissingForLink(Face),
    NodeMissingForFork(Face),
    NodeMissingForJoin(Face),
    FiringNodeMissing(Face),
    FiringNodeDuplicated(Face),
    IncoherentStructure(String, u32, (Face, String, String)),

    LeakedInhibitor(NodeID, Multiplicity),
    StateUnderflow(NodeID, Multiplicity, Multiplicity),
    StateOverflow(NodeID, Multiplicity, Multiplicity),
    CapacityOverflow(NodeID, Capacity, Multiplicity),
    MultiplicityOverflow(String),
    ParseIntError(num::ParseIntError),
    ParseFloatError(num::ParseFloatError),

    EmptyClauseRejectedByFormula(String),
    EmptyClauseRejectedBySolver(String),
    EmptyCausesOfInternalNode(String),
    EmptyEffectsOfInternalNode(String),

    UnlistedAtomicInMonomial,
    IncoherencyLeak,
    NoModelToInhibit,

    UnknownScriptFormat,
}

impl fmt::Display for AcesErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use AcesErrorKind::*;

        match self {
            ContextMismatch => write!(f, "Context mismatch"),
            PolynomialFaceMismatch => {
                write!(f, "Attempt to combine polynomials attached to opposite faces")
            }
            HarcNotAForkMismatch(join_id) => write!(f, "Expected fork, but harc is {:?}", join_id),
            HarcNotAJoinMismatch(fork_id) => write!(f, "Expected join, but harc is {:?}", fork_id),
            NodeMissingForID(node_id) => write!(f, "There is no node with {:?}", node_id),
            AtomMissingForID(atom_id) => write!(f, "There is no atom with {:?}", atom_id),
            PortMissingForID(port_id) => write!(f, "There is no port with {:?}", port_id),
            LinkMissingForID(link_id) => write!(f, "There is no link with {:?}", link_id),
            HarcMissingForID(harc_id) => write!(f, "There is no harc with {:?}", harc_id),
            ForkMissingForID(fork_id) => write!(f, "There is no fork with {:?}", fork_id),
            JoinMissingForID(join_id) => write!(f, "There is no join with {:?}", join_id),
            BottomAtomAccess => write!(f, "Attempt to access the bottom atom"),
            AtomicsNotOrdered => write!(f, "Atomics have to be given in strictly increasing order"),

            NodeMissingForPort(face) => write!(
                f,
                "Missing node for {} port",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            NodeMissingForLink(face) => write!(
                f,
                "Missing {} node for link",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            NodeMissingForFork(face) => write!(
                f,
                "Missing {} node for fork",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            NodeMissingForJoin(face) => write!(
                f,
                "Missing {} node for join",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            FiringNodeMissing(face) => write!(
                f,
                "Missing {} node in firing component",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            FiringNodeDuplicated(face) => write!(
                f,
                "Duplicated {} node in firing component",
                if *face == Face::Tx { "sending" } else { "receiving" }
            ),
            IncoherentStructure(ces_name, num_thin_links, (face, tx_name, rx_name)) => {
                if *num_thin_links == 1 {
                    write!(
                        f,
                        "Structure '{}' is incoherent; there is one thin link: ({} {} {})",
                        ces_name, tx_name, face, rx_name
                    )
                } else {
                    write!(
                        f,
                        "Structure '{}' is incoherent; there are thin links: ({} {} {}) and {} \
                         more..",
                        ces_name,
                        tx_name,
                        face,
                        rx_name,
                        *num_thin_links - 1,
                    )
                }
            }

            LeakedInhibitor(node_id, tokens_before) => {
                write!(f, "Leaked inhibitor at {:?} firing from {} tokens", node_id, tokens_before)
            }
            StateUnderflow(node_id, tokens_before, num_tokens) => write!(
                f,
                "State underflow at {:?} after subtracting {} from {}",
                node_id, num_tokens, tokens_before
            ),
            StateOverflow(node_id, tokens_before, num_tokens) => write!(
                f,
                "State overflow at {:?} after adding {} to {}",
                node_id, num_tokens, tokens_before
            ),
            CapacityOverflow(node_id, capacity, num_tokens) => {
                write!(f, "Capacity overflow at {:?} set to {} > {}", node_id, num_tokens, capacity)
            }
            MultiplicityOverflow(when_happened) => {
                write!(f, "Multiplicity overflow when {}", when_happened)
            }
            ParseIntError(err) => err.fmt(f),
            ParseFloatError(err) => err.fmt(f),

            EmptyClauseRejectedByFormula(name) => {
                write!(f, "Empty {} clause rejected by formula", name)
            }
            EmptyClauseRejectedBySolver(name) => {
                write!(f, "Empty {} clause rejected by solver", name)
            }
            EmptyCausesOfInternalNode(name) => {
                write!(f, "Empty cause polynomial of internal node '{}'", name)
            }
            EmptyEffectsOfInternalNode(name) => {
                write!(f, "Empty effect polynomial of internal node '{}'", name)
            }

            UnlistedAtomicInMonomial => write!(f, "Monomial contains an unlisted atomic"),
            IncoherencyLeak => write!(f, "Unexpected incoherence of a c-e structure"),
            NoModelToInhibit => write!(f, "Attempt to inhibit a nonexistent model"),

            UnknownScriptFormat => write!(f, "Unrecognized script format"),
        }
    }
}

impl AcesErrorKind {
    pub fn with_context(self, context: &ContextHandle) -> AcesError {
        AcesError { context: Some(context.clone()), kind: self }
    }
}

impl From<num::ParseIntError> for AcesErrorKind {
    #[inline]
    fn from(kind: num::ParseIntError) -> Self {
        AcesErrorKind::ParseIntError(kind)
    }
}

impl From<num::ParseFloatError> for AcesErrorKind {
    #[inline]
    fn from(kind: num::ParseFloatError) -> Self {
        AcesErrorKind::ParseFloatError(kind)
    }
}

#[derive(Clone, Debug)]
pub struct AcesError {
    context: Option<ContextHandle>,
    kind:    AcesErrorKind,
}

impl From<AcesErrorKind> for AcesError {
    #[inline]
    fn from(kind: AcesErrorKind) -> Self {
        AcesError { context: None, kind }
    }
}

impl fmt::Display for AcesError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use AcesErrorKind::*;

        if let Some(ref ctx) = self.context {
            match self.kind {
                HarcNotAForkMismatch(join_id) => {
                    write!(f, "Expected fork, but harc is a join {}", join_id.with(ctx))
                }
                HarcNotAJoinMismatch(fork_id) => {
                    write!(f, "Expected join, but harc is a fork {}", fork_id.with(ctx))
                }
                LeakedInhibitor(node_id, tokens_before) => write!(
                    f,
                    "Leaked inhibitor at node \"{}\" firing from {} tokens",
                    node_id.with(ctx),
                    tokens_before
                ),
                StateUnderflow(node_id, tokens_before, num_tokens) => write!(
                    f,
                    "State underflow at node \"{}\" after subtracting {} from {}",
                    node_id.with(ctx),
                    num_tokens,
                    tokens_before
                ),
                StateOverflow(node_id, tokens_before, num_tokens) => write!(
                    f,
                    "State overflow at node \"{}\" after adding {} to {}",
                    node_id.with(ctx),
                    num_tokens,
                    tokens_before
                ),
                CapacityOverflow(node_id, capacity, num_tokens) => write!(
                    f,
                    "Capacity overflow at node \"{}\" set to {} > {}",
                    node_id.with(ctx),
                    num_tokens,
                    capacity
                ),

                ref kind => kind.fmt(f),
            }
        } else {
            self.kind.fmt(f)
        }
    }
}

impl Error for AcesError {}