Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no program loaded error #181

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading