Skip to main content

clawless_core/prompt/
answer_prompt_error.rs

1use thiserror::Error;
2
3/// The error that ends a prompt without an answer from the user
4///
5/// The presenter reports this error through the [`Reply`] of a request. No variant is an
6/// answer, so a command must not treat one as a yes, a no, or a default.
7///
8/// A later release can add variants. Match with a wildcard arm.
9///
10/// [`Reply`]: crate::event::prompt::Reply
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum AnswerPromptError {
14    /// The user closed the input instead of answering
15    ///
16    /// On a terminal, Ctrl+D closes the input.
17    #[error("the input ended before the user answered")]
18    ClosedInput,
19
20    /// The presenter dropped the request without an answer
21    ///
22    /// A presenter that cannot ask a user drops every request.
23    #[error("the prompt was dropped without an answer")]
24    DroppedRequest,
25
26    /// The script of a test holds no answer that fits the prompt
27    ///
28    /// A [`ScriptedUser`] reports this when its script has ended, and when the next answer is
29    /// for another kind of prompt.
30    ///
31    /// [`ScriptedUser`]: super::ScriptedUser
32    #[error("the script holds no answer that fits the prompt")]
33    UnscriptedPrompt,
34
35    /// The terminal could not show the prompt or read the answer
36    #[error("failed to use the terminal")]
37    UnusableTerminal {
38        /// The cause of the failure
39        source: std::io::Error,
40    },
41}
42
43#[cfg(test)]
44mod tests {
45    // An assertion in a test panics by design. A `# Panics` section on every test
46    // would repeat that and give the reader no information.
47    #![allow(clippy::missing_panics_doc)]
48
49    use super::*;
50
51    #[test]
52    fn display_with_a_closed_input_states_the_condition() {
53        let error = AnswerPromptError::ClosedInput;
54
55        let message = error.to_string();
56
57        assert_eq!(message, "the input ended before the user answered");
58    }
59
60    #[test]
61    fn trait_send() {
62        fn assert_send<T: Send>() {}
63        assert_send::<AnswerPromptError>();
64    }
65
66    #[test]
67    fn trait_sync() {
68        fn assert_sync<T: Sync>() {}
69        assert_sync::<AnswerPromptError>();
70    }
71
72    #[test]
73    fn trait_unpin() {
74        fn assert_unpin<T: Unpin>() {}
75        assert_unpin::<AnswerPromptError>();
76    }
77}