Skip to main content

clawless_core/prompt/
text.rs

1use getset::Getters;
2
3/// A question that a user answers with one line of text
4///
5/// The answer is the line as the user typed it, without the characters that end the line. A
6/// prompt does not trim or validate the answer, and the answer can be empty.
7///
8/// # Examples
9///
10/// ```
11/// use clawless_core::prompt::Text;
12///
13/// let question = Text::new("Title of the change");
14///
15/// assert_eq!(question.question(), "Title of the change");
16/// ```
17#[derive(Clone, Eq, PartialEq, Hash, Debug, Getters)]
18pub struct Text {
19    /// The text of the question
20    #[getset(get = "pub")]
21    question: String,
22}
23
24impl Text {
25    /// Creates a question
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use clawless_core::prompt::Text;
31    ///
32    /// let question = Text::new("Title of the change");
33    /// ```
34    pub fn new(question: impl Into<String>) -> Self {
35        Self {
36            question: question.into(),
37        }
38    }
39}
40
41impl From<&str> for Text {
42    fn from(question: &str) -> Self {
43        Self::new(question)
44    }
45}
46
47impl From<String> for Text {
48    fn from(question: String) -> Self {
49        Self::new(question)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    // An assertion in a test panics by design. A `# Panics` section on every test
56    // would repeat that and give the reader no information.
57    #![allow(clippy::missing_panics_doc)]
58
59    use super::*;
60
61    #[test]
62    fn from_a_string_slice_keeps_the_question() {
63        let question = Text::from("Title");
64
65        assert_eq!(question, Text::new("Title"));
66    }
67
68    #[test]
69    fn trait_send() {
70        fn assert_send<T: Send>() {}
71        assert_send::<Text>();
72    }
73
74    #[test]
75    fn trait_sync() {
76        fn assert_sync<T: Sync>() {}
77        assert_sync::<Text>();
78    }
79
80    #[test]
81    fn trait_unpin() {
82        fn assert_unpin<T: Unpin>() {}
83        assert_unpin::<Text>();
84    }
85}