use thiserror::Error;
use super::AnswerPromptError;
use crate::event::SendError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PromptUserError {
#[error("no user is present to answer the prompt `{question}`")]
#[non_exhaustive]
AbsentUser {
question: String,
},
#[error("the prompt `{question}` was cancelled before the user answered")]
#[non_exhaustive]
CancelledPrompt {
question: String,
},
#[error("the prompt `{question}` has no options to select from")]
#[non_exhaustive]
MissingOptions {
question: String,
},
#[error("failed to get an answer to the prompt `{question}`")]
#[non_exhaustive]
UnansweredPrompt {
question: String,
source: AnswerPromptError,
},
#[error(
"the answer to the prompt `{question}` names option {index}, and the prompt has {count} options"
)]
#[non_exhaustive]
UnknownOption {
question: String,
index: usize,
count: usize,
},
#[error("failed to send the prompt `{question}` to the presenter")]
#[non_exhaustive]
UndeliverablePrompt {
question: String,
source: SendError,
},
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn display_with_a_cancelled_prompt_names_the_question() {
let error = PromptUserError::CancelledPrompt {
question: "Release?".to_owned(),
};
let message = error.to_string();
assert_eq!(
message,
"the prompt `Release?` was cancelled before the user answered"
);
}
#[test]
fn display_with_an_absent_user_names_the_question() {
let error = PromptUserError::AbsentUser {
question: "Release?".to_owned(),
};
let message = error.to_string();
assert_eq!(
message,
"no user is present to answer the prompt `Release?`"
);
}
#[test]
fn display_with_an_unanswered_prompt_names_the_question() {
let error = PromptUserError::UnansweredPrompt {
question: "Release?".to_owned(),
source: AnswerPromptError::ClosedInput,
};
let message = error.to_string();
assert_eq!(message, "failed to get an answer to the prompt `Release?`");
}
#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<PromptUserError>();
}
#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<PromptUserError>();
}
#[test]
fn trait_unpin() {
fn assert_unpin<T: Unpin>() {}
assert_unpin::<PromptUserError>();
}
}