aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/confirm/predicate.rs
diff options
context:
space:
mode:
Diffstat (limited to 'mingling/src/confirm/predicate.rs')
-rw-r--r--mingling/src/confirm/predicate.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/mingling/src/confirm/predicate.rs b/mingling/src/confirm/predicate.rs
new file mode 100644
index 0000000..f812608
--- /dev/null
+++ b/mingling/src/confirm/predicate.rs
@@ -0,0 +1,60 @@
+/// Defines how to parse user confirmation input.
+///
+/// A type implementing this trait determines which user input strings are treated as "yes" or "no".
+pub trait ConfirmerPredicate {
+ /// Parses the user's input string, returning whether it is "yes".
+ ///
+ /// Returns `Some(true)` for yes, `Some(false)` for no,
+ /// and `None` if the input cannot be parsed (requiring re-entry).
+ fn is_yes(str: &str) -> Option<bool>;
+}
+
+/// A `ConfirmerPredicate` implementation that accepts "y"/"yes" as yes and "n"/"no" as no.
+///
+/// Input comparison is case-insensitive and automatically trims leading/trailing whitespace.
+///
+/// # Examples
+///
+/// ```
+/// use mingling::res::Confirmer;
+/// use mingling::confirm::YesConfirm;
+///
+/// let confirmer = Confirmer::default();
+/// let confirmed = confirmer.ask::<YesConfirm>("Continue? [y/n] ");
+/// ```
+pub struct YesConfirm;
+
+/// A `ConfirmerPredicate` implementation that accepts "true"/"t" as yes and "false"/"f" as no.
+///
+/// Input comparison is case-insensitive and automatically trims leading/trailing whitespace.
+///
+/// # Examples
+///
+/// ```
+/// use mingling::res::Confirmer;
+/// use mingling::confirm::TrueConfirm;
+///
+/// let confirmer = Confirmer::default();
+/// let confirmed = confirmer.ask::<TrueConfirm>("Enable this feature? [true/false] ");
+/// ```
+pub struct TrueConfirm;
+
+impl ConfirmerPredicate for YesConfirm {
+ fn is_yes(str: &str) -> Option<bool> {
+ match str.trim().to_lowercase().as_str() {
+ "y" | "yes" => Some(true),
+ "n" | "no" => Some(false),
+ _ => None,
+ }
+ }
+}
+
+impl ConfirmerPredicate for TrueConfirm {
+ fn is_yes(str: &str) -> Option<bool> {
+ match str.trim().to_lowercase().as_str() {
+ "true" | "t" => Some(true),
+ "false" | "f" => Some(false),
+ _ => None,
+ }
+ }
+}