Skip to content

Commit d338c9b

Browse files
committed
refactor: removes unnecessary generics from the Prompt trait
BREAKING CHANGE: It used to be an interface that accepts `std::io::Write`, but this was an unnecessary generic added in the initial implementation. It has been removed after refactoring.
1 parent e3ce16a commit d338c9b

File tree

12 files changed

+35
-95
lines changed

12 files changed

+35
-95
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ struct CustomConfirm {
293293
value: bool,
294294
}
295295

296-
impl<W: std::io::Write> Prompt<W> for CustomConfirm {
296+
impl Prompt for CustomConfirm {
297297
type Output = bool;
298298

299299
// TODO
@@ -316,7 +316,7 @@ use promptuity::{Prompt, PromptState};
316316

317317
// ...
318318

319-
impl<W: std::io::Write> Prompt<W> for CustomConfirm {
319+
impl Prompt for CustomConfirm {
320320
// ...
321321

322322
fn handle(&mut self, code: KeyCode, modifiers: KeyModifiers) -> PromptState {
@@ -353,7 +353,7 @@ use promptuity::{Prompt, PromptState, RenderPayload};
353353

354354
// ...
355355

356-
impl<W: std::io::Write> Prompt<W> for CustomConfirm {
356+
impl Prompt for CustomConfirm {
357357
// ...
358358

359359
fn render(&mut self, state: &PromptState) -> Result<RenderPayload, String> {
@@ -386,7 +386,7 @@ This is the final step in constructing a custom prompt.
386386
Implement the [`Prompt::submit`](https://docs.rs/promptuity/latest/promptuity/trait.Prompt.html#tymethod.submit) method, which returns the final value for the received key input.
387387

388388
```rust
389-
impl<W: std::io::Write> Prompt<W> for CustomConfirm {
389+
impl Prompt for CustomConfirm {
390390
// ...
391391

392392
fn submit(&mut self) -> Self::Output {

examples/autocomplete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl AsMut<Autocomplete> for Autocomplete {
6161
}
6262
}
6363

64-
impl<W: std::io::Write> Prompt<W> for Autocomplete {
64+
impl Prompt for Autocomplete {
6565
type Output = String;
6666

6767
fn setup(&mut self) -> Result<(), Error> {

examples/extend_prompt.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl AsMut<ExtendedConfirm> for ExtendedConfirm {
2121
}
2222
}
2323

24-
impl<W: std::io::Write> Prompt<W> for ExtendedConfirm {
24+
impl Prompt for ExtendedConfirm {
2525
type Output = bool;
2626

2727
fn handle(
@@ -32,28 +32,28 @@ impl<W: std::io::Write> Prompt<W> for ExtendedConfirm {
3232
match (code, modifiers) {
3333
(KeyCode::Left, KeyModifiers::NONE) => {
3434
// forward to `y` key
35-
Prompt::<W>::handle(&mut self.original, KeyCode::Char('y'), KeyModifiers::NONE)
35+
Prompt::handle(&mut self.original, KeyCode::Char('y'), KeyModifiers::NONE)
3636
}
3737
(KeyCode::Right, KeyModifiers::NONE) => {
3838
// forward to `n` key
39-
Prompt::<W>::handle(&mut self.original, KeyCode::Char('n'), KeyModifiers::NONE)
39+
Prompt::handle(&mut self.original, KeyCode::Char('n'), KeyModifiers::NONE)
4040
}
4141
_ => {
4242
// forward to original handler
43-
Prompt::<W>::handle(&mut self.original, code, modifiers)
43+
Prompt::handle(&mut self.original, code, modifiers)
4444
}
4545
}
4646
}
4747

4848
fn submit(&mut self) -> Self::Output {
49-
Prompt::<W>::submit(&mut self.original)
49+
Prompt::submit(&mut self.original)
5050
}
5151

5252
fn render(
5353
&mut self,
5454
state: &promptuity::PromptState,
5555
) -> Result<promptuity::RenderPayload, String> {
56-
Prompt::<W>::render(&mut self.original, state)
56+
Prompt::render(&mut self.original, state)
5757
}
5858
}
5959

src/prompt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl RenderPayload {
390390
}
391391

392392
/// A trait representing the behavior of a prompt.
393-
pub trait Prompt<W: std::io::Write> {
393+
pub trait Prompt {
394394
/// The type returned as a result by the prompt.
395395
type Output;
396396

@@ -575,7 +575,7 @@ impl<'a, W: std::io::Write> Promptuity<'a, W> {
575575
}
576576

577577
/// Executes the specified prompt and returns the input result.
578-
pub fn prompt<O>(&mut self, prompt: &mut dyn Prompt<W, Output = O>) -> Result<O, Error> {
578+
pub fn prompt<O>(&mut self, prompt: &mut dyn Prompt<Output = O>) -> Result<O, Error> {
579579
prompt.setup()?;
580580

581581
self.state = PromptState::Active;
@@ -616,7 +616,7 @@ impl<'a, W: std::io::Write> Promptuity<'a, W> {
616616
}
617617
}
618618

619-
fn render<O>(&mut self, prompt: &mut dyn Prompt<W, Output = O>) -> Result<(), Error> {
619+
fn render<O>(&mut self, prompt: &mut dyn Prompt<Output = O>) -> Result<(), Error> {
620620
let res = prompt.render(&self.state).map_err(Error::Prompt)?;
621621

622622
self.theme.render(

src/prompts/confirm.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl AsMut<Confirm> for Confirm {
130130
}
131131
}
132132

133-
impl<W: std::io::Write> Prompt<W> for Confirm {
133+
impl Prompt for Confirm {
134134
type Output = bool;
135135

136136
fn handle(&mut self, code: KeyCode, modifiers: KeyModifiers) -> PromptState {
@@ -190,28 +190,24 @@ mod tests {
190190

191191
test_prompt!(
192192
test_hint,
193-
Confirm,
194193
Confirm::new("test message").with_hint("hint message"),
195194
vec![]
196195
);
197196

198197
test_prompt!(
199198
test_default_enter,
200-
Confirm,
201199
Confirm::new("test message").as_mut(),
202200
vec![(KeyCode::Enter, KeyModifiers::NONE)]
203201
);
204202

205203
test_prompt!(
206204
test_default_yes,
207-
Confirm,
208205
Confirm::new("test message").with_default(true),
209206
vec![(KeyCode::Enter, KeyModifiers::NONE)]
210207
);
211208

212209
test_prompt!(
213210
test_move,
214-
Confirm,
215211
Confirm::new("test message").as_mut(),
216212
vec![
217213
(KeyCode::Left, KeyModifiers::NONE),
@@ -228,28 +224,24 @@ mod tests {
228224

229225
test_prompt!(
230226
test_direct_lower_yes,
231-
Confirm,
232227
Confirm::new("test message").with_default(false),
233228
vec![(KeyCode::Char('y'), KeyModifiers::NONE)]
234229
);
235230

236231
test_prompt!(
237232
test_direct_upper_yes,
238-
Confirm,
239233
Confirm::new("test message").with_default(false),
240234
vec![(KeyCode::Char('Y'), KeyModifiers::NONE)]
241235
);
242236

243237
test_prompt!(
244238
test_direct_lower_no,
245-
Confirm,
246239
Confirm::new("test message").with_default(true),
247240
vec![(KeyCode::Char('n'), KeyModifiers::NONE)]
248241
);
249242

250243
test_prompt!(
251244
test_direct_upper_no,
252-
Confirm,
253245
Confirm::new("test message").with_default(true),
254246
vec![(KeyCode::Char('N'), KeyModifiers::NONE)]
255247
);

src/prompts/input.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl AsMut<Input> for Input {
126126
}
127127
}
128128

129-
impl<W: std::io::Write> Prompt<W> for Input {
129+
impl Prompt for Input {
130130
type Output = String;
131131

132132
fn handle(&mut self, code: KeyCode, modifiers: KeyModifiers) -> PromptState {
@@ -210,21 +210,18 @@ mod tests {
210210

211211
test_prompt!(
212212
test_hint,
213-
Input,
214213
Input::new("test message").with_hint("hint message"),
215214
vec![]
216215
);
217216

218217
test_prompt!(
219218
test_placeholder,
220-
Input,
221219
Input::new("test message").with_placeholder("placeholder message"),
222220
vec![]
223221
);
224222

225223
test_prompt!(
226224
test_default,
227-
Input,
228225
Input::new("test message").as_mut(),
229226
vec![
230227
(KeyCode::Char('a'), KeyModifiers::NONE),
@@ -239,21 +236,18 @@ mod tests {
239236

240237
test_prompt!(
241238
test_required_error,
242-
Input,
243239
Input::new("test message").as_mut(),
244240
vec![(KeyCode::Enter, KeyModifiers::NONE)]
245241
);
246242

247243
test_prompt!(
248244
test_non_required_empty_submit,
249-
Input,
250245
Input::new("test message").with_required(false),
251246
vec![(KeyCode::Enter, KeyModifiers::NONE)]
252247
);
253248

254249
test_prompt!(
255250
test_move,
256-
Input,
257251
Input::new("test message").with_default("abcdef"),
258252
vec![
259253
(KeyCode::Left, KeyModifiers::NONE),
@@ -268,7 +262,6 @@ mod tests {
268262

269263
test_prompt!(
270264
test_editing,
271-
Input,
272265
Input::new("test message").with_default("abcdef"),
273266
vec![
274267
(KeyCode::Backspace, KeyModifiers::NONE),
@@ -298,7 +291,6 @@ mod tests {
298291

299292
test_prompt!(
300293
test_validation,
301-
Input,
302294
Input::new("test message").with_validator(|v: &String| {
303295
if v.as_str() == "abc" {
304296
Err("Error Message".into())

src/prompts/multi_select.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<T: Default + Clone> AsMut<MultiSelect<T>> for MultiSelect<T> {
269269
}
270270
}
271271

272-
impl<T: Default + Clone, W: std::io::Write> Prompt<W> for MultiSelect<T> {
272+
impl<T: Default + Clone> Prompt for MultiSelect<T> {
273273
type Output = Vec<T>;
274274

275275
fn setup(&mut self) -> Result<(), Error> {
@@ -415,27 +415,24 @@ mod tests {
415415

416416
test_prompt!(
417417
test_hint,
418-
MultiSelect<String>,
419418
MultiSelect::new("test message", options!(3)).with_hint("hint message"),
420419
vec![]
421420
);
422421

423422
test_prompt!(
424423
test_10_items_with_5_page_size,
425-
MultiSelect<String>,
426424
MultiSelect::new("test message", options!(10)).with_page_size(5),
427425
vec![]
428426
);
429427

430428
test_prompt!(
431429
test_option_hint,
432-
MultiSelect<String>,
433430
MultiSelect::new(
434431
"test message",
435432
vec![
436-
MultiSelectOption::new("Value1", "value1".into()).with_hint("hint1"),
437-
MultiSelectOption::new("Value2", "value2".into()),
438-
MultiSelectOption::new("Value3", "value3".into()).with_hint("hint3"),
433+
MultiSelectOption::new("Value1", "value1".to_string()).with_hint("hint1"),
434+
MultiSelectOption::new("Value2", "value2".to_string()),
435+
MultiSelectOption::new("Value3", "value3".to_string()).with_hint("hint3"),
439436
]
440437
)
441438
.with_page_size(5),
@@ -444,7 +441,6 @@ mod tests {
444441

445442
test_prompt!(
446443
test_move,
447-
MultiSelect<String>,
448444
MultiSelect::new("test message", options!(10)).with_page_size(5),
449445
vec![
450446
(KeyCode::Char('j'), KeyModifiers::NONE),
@@ -476,7 +472,6 @@ mod tests {
476472

477473
test_prompt!(
478474
test_select_2_and_5,
479-
MultiSelect<String>,
480475
MultiSelect::new("test message", options!(10)).with_page_size(5),
481476
vec![
482477
(KeyCode::Down, KeyModifiers::NONE),
@@ -491,7 +486,6 @@ mod tests {
491486

492487
test_prompt!(
493488
test_select_all_and_inverse,
494-
MultiSelect<String>,
495489
MultiSelect::new("test message", options!(5)).as_mut(),
496490
vec![
497491
(KeyCode::Char('a'), KeyModifiers::NONE),
@@ -507,21 +501,18 @@ mod tests {
507501

508502
test_prompt!(
509503
test_required_error,
510-
MultiSelect<String>,
511504
MultiSelect::new("test message", options!(5)).with_required(true),
512505
vec![(KeyCode::Enter, KeyModifiers::NONE)]
513506
);
514507

515508
test_prompt!(
516509
test_non_required_empty_submit,
517-
MultiSelect<String>,
518510
MultiSelect::new("test message", options!(5)).with_required(false),
519511
vec![(KeyCode::Enter, KeyModifiers::NONE)]
520512
);
521513

522514
test_prompt!(
523515
test_min_error,
524-
MultiSelect<String>,
525516
MultiSelect::new("test message", options!(5)).with_min(2),
526517
vec![
527518
(KeyCode::Enter, KeyModifiers::NONE),
@@ -535,7 +526,6 @@ mod tests {
535526

536527
test_prompt!(
537528
test_max_error,
538-
MultiSelect<String>,
539529
MultiSelect::new("test message", options!(5)).with_max(3),
540530
vec![
541531
(KeyCode::Enter, KeyModifiers::NONE),

0 commit comments

Comments
 (0)