aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/confirm
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-15 20:21:45 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-15 20:21:45 +0800
commit20dd9f182ec842a267fc48c72578c6169c121ccb (patch)
tree57ff1690078f295581f3c54c3b0093048b1b5c42 /mingling/src/confirm
parent046ac064b6f4790f9053ad1e6109843bb38f89dd (diff)
refactor: rename Confirmer to ResConfirm and OSC94 to ResOSC94
Rename `Confirmer` resource to `ResConfirm`, `ConfirmerCount` to `ConfirmCount`, and `ConfirmerPredicate` to `ConfirmPredicate`. Update related setups, guards, and documentation references.
Diffstat (limited to 'mingling/src/confirm')
-rw-r--r--mingling/src/confirm/count.rs58
-rw-r--r--mingling/src/confirm/predicate.rs22
2 files changed, 62 insertions, 18 deletions
diff --git a/mingling/src/confirm/count.rs b/mingling/src/confirm/count.rs
index b9753c2..373d42c 100644
--- a/mingling/src/confirm/count.rs
+++ b/mingling/src/confirm/count.rs
@@ -1,23 +1,67 @@
/// Specifies the maximum number of attempts for a confirmation prompt.
+///
+/// # Default Implementations
+///
+/// `ConfirmCount` implements the following traits by default:
+///
+/// - [`Debug`] — for formatted output and debugging.
+/// - [`Clone`] — to create a copy of the value.
+/// - [`Copy`] — since the enum holds no heap-allocated data, it can be trivially copied.
+/// - [`PartialEq`] — allows comparing two `ConfirmCount` values for equality.
+/// - [`Eq`] — provides full equality semantics (as opposed to just partial).
+/// - [`From<T>`] for all primitive integer types (`i8`–`i128`, `isize`, `u8`–`u128`, `usize`),
+/// allowing convenient conversion from a raw number.
+///
+/// # What the Numbers Mean
+///
+/// The numeric value passed to a `From` conversion represents the **maximum number of times**
+/// the confirmation prompt will be shown to the user. For example:
+///
+/// - `ConfirmCount::from(3)` → asks at most **3** times before giving up.
+/// - `ConfirmCount::from(1)` → asks exactly **1** time.
+/// - `ConfirmCount::from(0)` → interpreted as [`Loop`], meaning it will keep asking
+/// indefinitely until a valid answer is parsed.
+///
+/// # Examples
+///
+/// ```
+/// use mingling::confirm::ConfirmCount;
+///
+/// // Convert from a numeric value
+/// let count: ConfirmCount = 3.into();
+/// assert_eq!(count, ConfirmCount::Max(3));
+///
+/// // Zero means loop forever
+/// let loop_count: ConfirmCount = 0.into();
+/// assert_eq!(loop_count, ConfirmCount::Loop);
+///
+/// // Large values are capped at usize::MAX
+/// let big: ConfirmCount = i128::MAX.into();
+/// assert_eq!(big, ConfirmCount::Max(usize::MAX));
+///
+/// // From a usize directly
+/// let from_usize = ConfirmCount::from(5usize);
+/// assert_eq!(from_usize, ConfirmCount::Max(5));
+/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum ConfirmerCount {
+pub enum ConfirmCount {
/// Loop indefinitely until the user gives a parseable answer.
Loop,
/// Ask at most the specified number of times.
Max(usize),
}
-macro_rules! impl_from_for_confirmer_count {
+macro_rules! impl_from_for_Confirm_count {
($($t:ty),*) => {
$(
- impl From<$t> for ConfirmerCount {
+ impl From<$t> for ConfirmCount {
fn from(n: $t) -> Self {
if n == 0 {
- ConfirmerCount::Loop
+ ConfirmCount::Loop
} else {
match usize::try_from(n) {
- Ok(max) => ConfirmerCount::Max(max),
- Err(_) => ConfirmerCount::Max(usize::MAX),
+ Ok(max) => ConfirmCount::Max(max),
+ Err(_) => ConfirmCount::Max(usize::MAX),
}
}
}
@@ -26,6 +70,6 @@ macro_rules! impl_from_for_confirmer_count {
};
}
-impl_from_for_confirmer_count!(
+impl_from_for_Confirm_count!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
);
diff --git a/mingling/src/confirm/predicate.rs b/mingling/src/confirm/predicate.rs
index f812608..786a459 100644
--- a/mingling/src/confirm/predicate.rs
+++ b/mingling/src/confirm/predicate.rs
@@ -1,7 +1,7 @@
/// 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 {
+pub trait ConfirmPredicate {
/// Parses the user's input string, returning whether it is "yes".
///
/// Returns `Some(true)` for yes, `Some(false)` for no,
@@ -9,37 +9,37 @@ pub trait ConfirmerPredicate {
fn is_yes(str: &str) -> Option<bool>;
}
-/// A `ConfirmerPredicate` implementation that accepts "y"/"yes" as yes and "n"/"no" as no.
+/// A `ConfirmPredicate` 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::res::ResConfirm;
/// use mingling::confirm::YesConfirm;
///
-/// let confirmer = Confirmer::default();
-/// let confirmed = confirmer.ask::<YesConfirm>("Continue? [y/n] ");
+/// let confirm = ResConfirm::default();
+/// let confirmed = confirm.ask::<YesConfirm>("Continue? [y/n] ");
/// ```
pub struct YesConfirm;
-/// A `ConfirmerPredicate` implementation that accepts "true"/"t" as yes and "false"/"f" as no.
+/// A `ConfirmPredicate` 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::res::ResConfirm;
/// use mingling::confirm::TrueConfirm;
///
-/// let confirmer = Confirmer::default();
-/// let confirmed = confirmer.ask::<TrueConfirm>("Enable this feature? [true/false] ");
+/// let confirm = ResConfirm::default();
+/// let confirmed = confirm.ask::<TrueConfirm>("Enable this feature? [true/false] ");
/// ```
pub struct TrueConfirm;
-impl ConfirmerPredicate for YesConfirm {
+impl ConfirmPredicate for YesConfirm {
fn is_yes(str: &str) -> Option<bool> {
match str.trim().to_lowercase().as_str() {
"y" | "yes" => Some(true),
@@ -49,7 +49,7 @@ impl ConfirmerPredicate for YesConfirm {
}
}
-impl ConfirmerPredicate for TrueConfirm {
+impl ConfirmPredicate for TrueConfirm {
fn is_yes(str: &str) -> Option<bool> {
match str.trim().to_lowercase().as_str() {
"true" | "t" => Some(true),