Skip to content

Commit

Permalink
Merge pull request #181 from stargazing-dino/no_program_error
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim authored Feb 21, 2024
2 parents 17cee36 + 42c5ee3 commit 9712d96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/runtime/src/dialogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum DialogueError {
ContinueOnOptionSelectionError,
#[error("Cannot continue running dialogue. No node has been selected.")]
NoNodeSelectedOnContinue,
#[error("No program has been loaded. Cannot continue running dialogue.")]
NoProgramLoaded,
#[error("No node named \"{node_name}\" has been loaded.")]
InvalidNode { node_name: String },
#[error(transparent)]
Expand Down
20 changes: 12 additions & 8 deletions crates/runtime/src/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ impl VirtualMachine {
pub(crate) fn set_node(&mut self, node_name: impl Into<String>) -> Result<()> {
let node_name = node_name.into();
debug!("Loading node \"{node_name}\"");
let Some(current_node) = self.get_node_from_name(&node_name) else {
return Err(DialogueError::InvalidNode { node_name });
};
let current_node = self.get_node_from_name(&node_name)?;
self.current_node = Some(current_node.clone());

self.reset_state();
Expand Down Expand Up @@ -173,16 +171,22 @@ impl VirtualMachine {
}
}

fn get_node_from_name(&self, node_name: &str) -> Option<&Node> {
let program = self.program.as_ref().unwrap_or_else(|| {
panic!("Cannot load node \"{node_name}\": No program has been loaded.")
});
fn get_node_from_name(&self, node_name: &str) -> Result<&Node> {
let program = self
.program
.as_ref()
.ok_or_else(|| DialogueError::NoProgramLoaded)?;
assert!(
!program.nodes.is_empty(),
"Cannot load node \"{node_name}\": No nodes have been loaded.",
);

program.nodes.get(node_name)
program
.nodes
.get(node_name)
.ok_or_else(|| DialogueError::InvalidNode {
node_name: node_name.to_owned(),
})
}

/// Resumes execution.
Expand Down

0 comments on commit 9712d96

Please sign in to comment.