use getset::{CopyGetters, Getters};
use super::Confirmation;
#[derive(Clone, Eq, PartialEq, Hash, Debug, CopyGetters, Getters)]
pub struct Confirm {
#[getset(get = "pub")]
question: String,
#[getset(get_copy = "pub")]
default: Option<Confirmation>,
}
impl Confirm {
pub fn new(question: impl Into<String>) -> Self {
Self {
question: question.into(),
default: None,
}
}
#[must_use]
pub fn with_default(mut self, default: Confirmation) -> Self {
self.default = Some(default);
self
}
}
impl From<&str> for Confirm {
fn from(question: &str) -> Self {
Self::new(question)
}
}
impl From<String> for Confirm {
fn from(question: String) -> Self {
Self::new(question)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn from_a_string_slice_has_no_default() {
let question = Confirm::from("Delete the branch?");
assert_eq!(question, Confirm::new("Delete the branch?"));
}
#[test]
fn new_has_no_default() {
let question = Confirm::new("Delete the branch?");
assert_eq!(question.default(), None);
}
#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<Confirm>();
}
#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Confirm>();
}
#[test]
fn trait_unpin() {
fn assert_unpin<T: Unpin>() {}
assert_unpin::<Confirm>();
}
#[test]
fn with_default_keeps_the_question() {
let question = Confirm::new("Delete the branch?").with_default(Confirmation::Yes);
assert_eq!(question.question(), "Delete the branch?");
}
#[test]
fn with_default_sets_the_default() {
let question = Confirm::new("Delete the branch?").with_default(Confirmation::Yes);
assert_eq!(question.default(), Some(Confirmation::Yes));
}
}