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
//! Internal errors from `inkling` itself.

use crate::{
    line::ChoiceData,
    node::{NodeItem, NodeType, Stack},
    story::Choice,
};

use std::{error::Error, fmt};

#[derive(Clone, Debug)]
/// Internal error from walking through a story.
///
/// Most likely due to the `DialogueNode` tree of a story being constructed incorrectly,
/// which will be due to a logical error in the set-up code since the user has no
/// control over it.
pub enum InklingError {
    /// The graph of `DialogueNode`s has an incorrect structure. This can be that `Choice`s
    /// are not properly nested under `ChoiceSet` nodes.
    BadGraph(BadGraphKind),
    /// The current stack is not properly representing the graph or has some indexing problems.
    IncorrectStack {
        kind: IncorrectStackKind,
        stack: Stack,
    },
    /// A choice was made with an internal index that does not match one existing in the set.
    /// Means that the choice set presented to the user was not created to represent the set
    /// of encountered choices, or that somehow a faulty choice was returned to continue
    /// the story with.
    InvalidChoice {
        /// Index of choice that was used internally when the choice was not found.
        index: usize,
        /// Choice input by the user to resume the story with.
        choice: Option<Choice>,
        /// List of choices that were available for the selection and if they were given
        /// to the user in the `Prompt::Choice` set.
        presented_choices: Vec<(bool, Choice)>,
        /// List of all choices that were available in their internal representation.
        internal_choices: Vec<ChoiceData>,
    },
    /// No root knot has been set to begin following the story from.
    NoKnotStack,
    /// Tried to resume a story that has not been started.
    ResumeBeforeStart,
    /// Tried to `start` a story that is already in progress.
    StartOnStoryInProgress,
    /// The story tried to move to a knot that doesn't exist.
    UnknownKnot { knot_name: String },
}

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

        match self {
            BadGraph(BadGraphKind::ExpectedNode {
                index,
                node_level,
                expected,
                found,
            }) => write!(
                f,
                "Expected a `DialogueNode` that is a {:?} but got a {:?} \
                 (node level: {}, index: {})",
                expected, found, node_level, index
            ),
            InvalidChoice {
                index,
                choice,
                presented_choices,
                ..
            } => {
                let presented_choices_string = presented_choices
                    .iter()
                    .map(|(shown, choice)| {
                        if *shown {
                            format!("{:?} (shown as available)", choice)
                        } else {
                            format!("{:?} (not shown)", choice)
                        }
                    })
                    .collect::<Vec<_>>()
                    .join("\n");

                match choice {
                    Some(choice) => {
                        write!(f,
                        "Tried to resume the story with an invalid choice: input choice was {:?}, \
                        while available choices were: \n
                        {}",
                        choice, presented_choices_string
                        )
                    }
                    None => write!(
                        f,
                        "Tried to resume the story with an invalid choice: \
                         input choice cannot be found but its internal index was {}, \
                         available choices were: [{}]",
                        index, presented_choices_string
                    ),
                }
            }
            NoKnotStack => write!(
                f,
                "`Story` object was created but no root `Knot` was set to start \
                 following the story from"
            ),
            ResumeBeforeStart => write!(f, "Tried to resume a story that has not yet been started"),
            StartOnStoryInProgress => {
                write!(f, "Called `start` on a story that is already in progress")
            }
            UnknownKnot { knot_name } => write!(
                f,
                "Tried to follow a knot with name {} but no such knot exists",
                knot_name
            ),
            IncorrectStack { kind, stack } => match kind {
                BadIndices {
                    node_level,
                    index,
                    num_items,
                } => write!(
                    f,
                    "Current stack has invalid index {} at node level {}: size of set is {} \
                     (stack: {:?})",
                    index, node_level, num_items, stack
                ),
                EmptyStack => write!(f, "Tried to advance through a knot with an empty stack"),
                Gap { node_level } => write!(
                    f,
                    "Current stack is too short for the current node level {}: \
                     cannot get or add a stack index because stack indices for one or more \
                     prior nodes are missing, which means the stack is incorrect (stack: {:?})",
                    node_level, stack
                ),
                MissingIndices { node_level, kind } => {
                    let level = match kind {
                        WhichIndex::Parent => *node_level,
                        WhichIndex::Child => *node_level + 1,
                    };

                    write!(f, "Current stack has no index for node level {}", level)?;

                    if let WhichIndex::Child = kind {
                        write!(f, ", which was accessed as a child node during a follow")?;
                    }

                    write!(f, " (stack: {:?}", stack)
                }
                NotTruncated { node_level } => write!(
                    f,
                    "Current stack is not truncated to the current node level {} (stack: {:?})",
                    node_level, stack
                ),
            },
        }
    }
}

impl Error for InklingError {}

#[derive(Clone, Debug)]
/// Error variant associated with the `DialogueNode` graph being poorly constructed.
pub enum BadGraphKind {
    /// Tried to access a `NodeItem` assuming that it was of a particular kind,
    /// but it was not.
    ExpectedNode {
        /// Index of item in parent list.
        index: usize,
        /// Level of parent `DialogueNode`.
        node_level: usize,
        /// Expected kind.
        expected: NodeItemKind,
        /// Encountered kind.
        found: NodeItemKind,
    },
}

#[derive(Clone, Debug)]
/// Simple representation of what a `NodeItem` is.
pub enum NodeItemKind {
    Line,
    Choice,
    ChoiceSet,
}

impl From<&NodeItem> for NodeItemKind {
    fn from(item: &NodeItem) -> Self {
        match item {
            NodeItem::Line(..) => NodeItemKind::Line,
            NodeItem::Node {
                kind: NodeType::Choice(..),
                ..
            } => NodeItemKind::Choice,
            NodeItem::Node {
                kind: NodeType::ChoiceSet,
                ..
            } => NodeItemKind::ChoiceSet,
        }
    }
}

#[derive(Clone, Debug)]
/// Error variant associated with the stack created when walking through a `DialogueNode`
/// tree being poorly constructed.
pub enum IncorrectStackKind {
    /// Stack contains an invalid index for the current node level.
    BadIndices {
        node_level: usize,
        index: usize,
        num_items: usize,
    },
    /// Tried to follow through nodes with an empty stack.
    EmptyStack,
    /// Stack has a gap from the last added node level and the current.
    Gap { node_level: usize },
    /// Stack is missing an index when walking through it, either for the current (parent)
    /// node or for a child node. The parent here *should* be a node which contains lines
    /// and possible choice sets, while the child will be a node in a choice set .
    MissingIndices { node_level: usize, kind: WhichIndex },
    /// Stack was not truncated before following into a new node.
    NotTruncated { node_level: usize },
}

#[derive(Clone, Debug)]
/// Whether the parent or child index caused an error when walking through a `DialogueNode` tree.
pub enum WhichIndex {
    Parent,
    Child,
}