git_revise/revise/prompts/
commit_subject.rs

1use inquire::{
2    validator::{ErrorMessage, Validation},
3    Text,
4};
5
6use super::Inquire;
7use crate::error::ReviseResult;
8
9#[derive(Debug, Clone)]
10pub struct Part {
11    pub msg: String,
12    pub ans: Option<String>,
13}
14
15impl Part {
16    pub fn new() -> Self {
17        Self {
18            msg: "Write a SHORT, IMPERATIVE tense description of the change:\n"
19                .to_string(),
20            ans: None,
21        }
22    }
23}
24
25impl Default for Part {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl Inquire for Part {
32    fn inquire(&mut self) -> ReviseResult<()> {
33        let ans = Text::new(&self.msg)
34            .with_help_message("Infinity more chars allowed")
35            .with_validator(|s: &str| {
36                if s.is_empty() {
37                    return Ok(Validation::Invalid(ErrorMessage::Custom(
38                        "[ERROR] Subject is required and cannot be empty"
39                            .to_string(),
40                    )));
41                }
42                Ok(Validation::Valid)
43            })
44            .prompt()?;
45        self.ans = Some(ans);
46        Ok(())
47    }
48}