Skip to content

Commit f92f147

Browse files
committed
Better builder signatures
1 parent 70d9105 commit f92f147

File tree

11 files changed

+91
-89
lines changed

11 files changed

+91
-89
lines changed

.clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.56.0"
1+
msrv = "1.59.0"

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
### Enhancements
66

7+
* Added `dialouger::Result` and `dialouger::Error`
8+
79
### Breaking
810

911
* Updated MSRV to `1.59.0`
1012
* Removed deprecated `Confirm::with_text`
11-
* Operations now return `dialouger::Result` instead of `std::io::Result`
13+
* Prompt builder functions now take `mut self` instead of `&mut self`
14+
* Prompt builder functions now return `Self` instead of `&mut Self`
15+
* Prompt interaction functions now take `self` instead of `&self`
16+
* Prompt interaction functions and other operations now return `dialouger::Result` instead of `std::io::Result`
1217

1318
## 0.10.4
1419

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ name = "editor"
4141
required-features = ["editor"]
4242

4343
[[example]]
44-
name = "fuzzyselect"
44+
name = "fuzzy_select"
4545
required-features = ["fuzzy-select"]
4646

4747
[[example]]
File renamed without changes.

src/prompts/confirm.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ impl Confirm<'static> {
5151

5252
impl Confirm<'_> {
5353
/// Sets the confirm prompt.
54-
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
54+
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
5555
self.prompt = prompt.into();
5656
self
5757
}
5858

5959
/// Indicates whether or not to report the chosen selection after interaction.
6060
///
6161
/// The default is to report the chosen selection.
62-
pub fn report(&mut self, val: bool) -> &mut Self {
62+
pub fn report(mut self, val: bool) -> Self {
6363
self.report = val;
6464
self
6565
}
@@ -73,7 +73,7 @@ impl Confirm<'_> {
7373
/// When `true`, the user must type their choice and hit the Enter key before
7474
/// proceeding. Valid inputs can be "yes", "no", "y", "n", or an empty string
7575
/// to accept the default.
76-
pub fn wait_for_newline(&mut self, wait: bool) -> &mut Self {
76+
pub fn wait_for_newline(mut self, wait: bool) -> Self {
7777
self.wait_for_newline = wait;
7878
self
7979
}
@@ -83,15 +83,15 @@ impl Confirm<'_> {
8383
/// Out of the box the prompt does not have a default and will continue
8484
/// to display until the user inputs something and hits enter. If a default is set the user
8585
/// can instead accept the default with enter.
86-
pub fn default(&mut self, val: bool) -> &mut Self {
86+
pub fn default(mut self, val: bool) -> Self {
8787
self.default = Some(val);
8888
self
8989
}
9090

9191
/// Disables or enables the default value display.
9292
///
9393
/// The default is to append the default value to the prompt to tell the user.
94-
pub fn show_default(&mut self, val: bool) -> &mut Self {
94+
pub fn show_default(mut self, val: bool) -> Self {
9595
self.show_default = val;
9696
self
9797
}
@@ -103,7 +103,7 @@ impl Confirm<'_> {
103103
/// Result contains `bool` if user answered "yes" or "no" or `default` (configured in [`default`](Self::default) if pushes enter.
104104
/// This unlike [`interact_opt`](Self::interact_opt) does not allow to quit with 'Esc' or 'q'.
105105
#[inline]
106-
pub fn interact(&self) -> Result<bool> {
106+
pub fn interact(self) -> Result<bool> {
107107
self.interact_on(&Term::stderr())
108108
}
109109

@@ -131,25 +131,25 @@ impl Confirm<'_> {
131131
/// }
132132
/// ```
133133
#[inline]
134-
pub fn interact_opt(&self) -> Result<Option<bool>> {
134+
pub fn interact_opt(self) -> Result<Option<bool>> {
135135
self.interact_on_opt(&Term::stderr())
136136
}
137137

138138
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
139139
#[inline]
140-
pub fn interact_on(&self, term: &Term) -> Result<bool> {
140+
pub fn interact_on(self, term: &Term) -> Result<bool> {
141141
Ok(self
142142
._interact_on(term, false)?
143143
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
144144
}
145145

146146
/// Like [`interact_opt`](Self::interact_opt) but allows a specific terminal to be set.
147147
#[inline]
148-
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<bool>> {
148+
pub fn interact_on_opt(self, term: &Term) -> Result<Option<bool>> {
149149
self._interact_on(term, true)
150150
}
151151

152-
fn _interact_on(&self, term: &Term, allow_quit: bool) -> Result<Option<bool>> {
152+
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<bool>> {
153153
let mut render = TermThemeRenderer::new(term, self.theme);
154154

155155
let default_if_show = if self.show_default {

src/prompts/fuzzy_select.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,33 @@ impl FuzzySelect<'_> {
6262
/// Sets the clear behavior of the menu.
6363
///
6464
/// The default is to clear the menu.
65-
pub fn clear(&mut self, val: bool) -> &mut Self {
65+
pub fn clear(mut self, val: bool) -> Self {
6666
self.clear = val;
6767
self
6868
}
6969

7070
/// Sets a default for the menu
71-
pub fn default(&mut self, val: usize) -> &mut Self {
71+
pub fn default(mut self, val: usize) -> Self {
7272
self.default = Some(val);
7373
self
7474
}
7575

7676
/// Add a single item to the fuzzy selector.
77-
pub fn item<T: ToString>(&mut self, item: T) -> &mut Self {
77+
pub fn item<T: ToString>(mut self, item: T) -> Self {
7878
self.items.push(item.to_string());
7979
self
8080
}
8181

8282
/// Adds multiple items to the fuzzy selector.
83-
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self {
83+
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
8484
for item in items {
8585
self.items.push(item.to_string());
8686
}
8787
self
8888
}
8989

9090
/// Sets the search text that a fuzzy search starts with.
91-
pub fn with_initial_text<S: Into<String>>(&mut self, initial_text: S) -> &mut Self {
91+
pub fn with_initial_text<S: Into<String>>(mut self, initial_text: S) -> Self {
9292
self.initial_text = initial_text.into();
9393
self
9494
}
@@ -97,31 +97,31 @@ impl FuzzySelect<'_> {
9797
///
9898
/// When a prompt is set the system also prints out a confirmation after
9999
/// the fuzzy selection.
100-
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
100+
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
101101
self.prompt = prompt.into();
102102
self
103103
}
104104

105105
/// Indicates whether to report the selected value after interaction.
106106
///
107107
/// The default is to report the selection.
108-
pub fn report(&mut self, val: bool) -> &mut Self {
108+
pub fn report(mut self, val: bool) -> Self {
109109
self.report = val;
110110
self
111111
}
112112

113113
/// Indicates whether to highlight matched indices
114114
///
115115
/// The default is to highlight the indices
116-
pub fn highlight_matches(&mut self, val: bool) -> &mut Self {
116+
pub fn highlight_matches(mut self, val: bool) -> Self {
117117
self.highlight_matches = val;
118118
self
119119
}
120120

121121
/// Sets the maximum number of visible options.
122122
///
123123
/// The default is the height of the terminal minus 2.
124-
pub fn max_length(&mut self, rows: usize) -> &mut Self {
124+
pub fn max_length(mut self, rows: usize) -> Self {
125125
self.max_length = Some(rows);
126126
self
127127
}
@@ -133,7 +133,7 @@ impl FuzzySelect<'_> {
133133
/// Result contains `index` of selected item if user hit 'Enter'.
134134
/// This unlike [`interact_opt`](Self::interact_opt) does not allow to quit with 'Esc' or 'q'.
135135
#[inline]
136-
pub fn interact(&self) -> Result<usize> {
136+
pub fn interact(self) -> Result<usize> {
137137
self.interact_on(&Term::stderr())
138138
}
139139

@@ -163,25 +163,25 @@ impl FuzzySelect<'_> {
163163
/// }
164164
/// ```
165165
#[inline]
166-
pub fn interact_opt(&self) -> Result<Option<usize>> {
166+
pub fn interact_opt(self) -> Result<Option<usize>> {
167167
self.interact_on_opt(&Term::stderr())
168168
}
169169

170170
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
171171
#[inline]
172-
pub fn interact_on(&self, term: &Term) -> Result<usize> {
172+
pub fn interact_on(self, term: &Term) -> Result<usize> {
173173
Ok(self
174174
._interact_on(term, false)?
175175
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
176176
}
177177

178178
/// Like [`interact_opt`](Self::interact_opt) but allows a specific terminal to be set.
179179
#[inline]
180-
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<usize>> {
180+
pub fn interact_on_opt(self, term: &Term) -> Result<Option<usize>> {
181181
self._interact_on(term, true)
182182
}
183183

184-
fn _interact_on(&self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
184+
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
185185
// Place cursor at the end of the search term
186186
let mut position = self.initial_text.len();
187187
let mut search_term = self.initial_text.to_owned();

src/prompts/input.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,27 @@ impl<T> Input<'_, T> {
7474
}
7575

7676
/// Sets the input prompt.
77-
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
77+
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
7878
self.prompt = prompt.into();
7979
self
8080
}
8181

8282
/// Changes the prompt text to the post completion text after input is complete
83-
pub fn with_post_completion_text<S: Into<String>>(
84-
&mut self,
85-
post_completion_text: S,
86-
) -> &mut Self {
83+
pub fn with_post_completion_text<S: Into<String>>(mut self, post_completion_text: S) -> Self {
8784
self.post_completion_text = Some(post_completion_text.into());
8885
self
8986
}
9087

9188
/// Indicates whether to report the input value after interaction.
9289
///
9390
/// The default is to report the input value.
94-
pub fn report(&mut self, val: bool) -> &mut Self {
91+
pub fn report(mut self, val: bool) -> Self {
9592
self.report = val;
9693
self
9794
}
9895

9996
/// Sets initial text that user can accept or erase.
100-
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Self {
97+
pub fn with_initial_text<S: Into<String>>(mut self, val: S) -> Self {
10198
self.initial_text = Some(val.into());
10299
self
103100
}
@@ -107,15 +104,15 @@ impl<T> Input<'_, T> {
107104
/// Out of the box the prompt does not have a default and will continue
108105
/// to display until the user inputs something and hits enter. If a default is set the user
109106
/// can instead accept the default with enter.
110-
pub fn default(&mut self, value: T) -> &mut Self {
107+
pub fn default(mut self, value: T) -> Self {
111108
self.default = Some(value);
112109
self
113110
}
114111

115112
/// Enables or disables an empty input
116113
///
117114
/// By default, if there is no default value set for the input, the user must input a non-empty string.
118-
pub fn allow_empty(&mut self, val: bool) -> &mut Self {
115+
pub fn allow_empty(mut self, val: bool) -> Self {
119116
self.permit_empty = val;
120117
self
121118
}
@@ -126,7 +123,7 @@ impl<T> Input<'_, T> {
126123
/// user what is the default value.
127124
///
128125
/// This method does not affect existence of default value, only its display in the prompt!
129-
pub fn show_default(&mut self, val: bool) -> &mut Self {
126+
pub fn show_default(mut self, val: bool) -> Self {
130127
self.show_default = val;
131128
self
132129
}
@@ -189,7 +186,7 @@ impl<'a, T> Input<'a, T> {
189186
/// self.history.get(pos).cloned()
190187
/// }
191188
///
192-
/// fn write(&mut self, val: &T)
189+
/// fn write(mut self, val: &T)
193190
/// where
194191
/// {
195192
/// self.history.push_front(val.to_string());
@@ -206,7 +203,7 @@ impl<'a, T> Input<'a, T> {
206203
/// }
207204
/// ```
208205
#[cfg(feature = "history")]
209-
pub fn history_with<H>(&mut self, history: &'a mut H) -> &mut Self
206+
pub fn history_with<H>(mut self, history: &'a mut H) -> Self
210207
where
211208
H: History<T>,
212209
{
@@ -216,7 +213,7 @@ impl<'a, T> Input<'a, T> {
216213

217214
/// Enable completion
218215
#[cfg(feature = "completion")]
219-
pub fn completion_with<C>(&mut self, completion: &'a C) -> &mut Self
216+
pub fn completion_with<C>(mut self, completion: &'a C) -> Self
220217
where
221218
C: Completion,
222219
{
@@ -250,7 +247,7 @@ where
250247
/// .unwrap();
251248
/// }
252249
/// ```
253-
pub fn validate_with<V>(&mut self, mut validator: V) -> &mut Self
250+
pub fn validate_with<V>(mut self, mut validator: V) -> Self
254251
where
255252
V: Validator<T> + 'a,
256253
V::Err: ToString,
@@ -285,12 +282,12 @@ where
285282
/// while [`interact`](Self::interact) allows virtually any character to be used e.g arrow keys.
286283
///
287284
/// The dialog is rendered on stderr.
288-
pub fn interact_text(&mut self) -> Result<T> {
285+
pub fn interact_text(self) -> Result<T> {
289286
self.interact_text_on(&Term::stderr())
290287
}
291288

292289
/// Like [`interact_text`](Self::interact_text) but allows a specific terminal to be set.
293-
pub fn interact_text_on(&mut self, term: &Term) -> Result<T> {
290+
pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
294291
let mut render = TermThemeRenderer::new(term, self.theme);
295292

296293
loop {
@@ -636,12 +633,12 @@ where
636633
///
637634
/// If the user confirms the result is `true`, `false` otherwise.
638635
/// The dialog is rendered on stderr.
639-
pub fn interact(&mut self) -> Result<T> {
636+
pub fn interact(self) -> Result<T> {
640637
self.interact_on(&Term::stderr())
641638
}
642639

643640
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
644-
pub fn interact_on(&mut self, term: &Term) -> Result<T> {
641+
pub fn interact_on(mut self, term: &Term) -> Result<T> {
645642
let mut render = TermThemeRenderer::new(term, self.theme);
646643

647644
loop {

0 commit comments

Comments
 (0)