lexa_prompt/confirm.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX) ┃
3// ┃ SPDX-License-Identifier: MPL-2.0 ┃
4// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
5// ┃ ┃
6// ┃ This Source Code Form is subject to the terms of the Mozilla Public ┃
7// ┃ License, v. 2.0. If a copy of the MPL was not distributed with this ┃
8// ┃ file, You can obtain one at https://mozilla.org/MPL/2.0/. ┃
9// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
10
11use std::fmt;
12
13use crate::choices;
14
15// ----------- //
16// Énumération //
17// ----------- //
18
19choices! {
20 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
21 pub enum Bool
22 {
23 False,
24 True,
25
26 N,
27 Y,
28
29 No,
30 Yes,
31 }
32}
33
34// -------------- //
35// Implémentation //
36// -------------- //
37
38impl Bool
39{
40 /// Vérifie que la valeur de [&Bool](Self) soit vraie.
41 pub fn is_true(&self) -> bool
42 {
43 matches!(self, Self::True | Self::Y | Self::Yes)
44 }
45
46 /// Vérifie que la valeur de [&Bool](Self) soit fausse.
47 pub fn is_false(&self) -> bool
48 {
49 matches!(self, Self::False | Self::N | Self::No)
50 }
51}
52
53// -------------- //
54// Implémentation // -> Interface
55// -------------- //
56
57impl From<Bool> for bool
58{
59 fn from(value: Bool) -> Self
60 {
61 value.is_true()
62 }
63}
64
65// -------- //
66// Fonction //
67// -------- //
68
69/// Invite l'utilisateur à confirmer une question, par oui (y) ou non (n).
70pub fn confirm(question: impl fmt::Display) -> bool
71{
72 let ask = format!("{question} ?");
73
74 let Ok(b) = inquire::Confirm::new(&ask).prompt() else {
75 return false;
76 };
77
78 b
79}