1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Async-friendly helpers for menu event loops.
//!
//! This module is **feature-gated** behind the `async` Cargo feature:
//! ```toml
//! cornichon = { version = "0.1", features = ["async"] }
//! ```
//!
//! Without the `async` feature, use the synchronous `update` method
//! directly in your `tokio::select!` loop — no `await` is required for a
//! pure state-machine call.
//!
//! # Quickstart
//!
//! The [`drive_until_action`] helper loops an async event source,
//! forwards each [`MenuEvent`](crate::ports::MenuEvent) to the menu's
//! `update` method, and returns the first non-[`MenuAction::None`](
//! crate::ports::MenuAction::None) action. Stops if the source closes.
//!
//! ```ignore
//! use cornichon::prelude::*;
//! use cornichon::ports::async_support::drive_until_action;
//!
//! let action = drive_until_action(&mut menu, key_receiver).await;
//! ```
//!
//! # Why a free function, not a trait?
//!
//! Each menu type owns its `update` method directly. A trait would
//! require a `Menu` trait that conflicts with the existing inherent
//! `update` methods. A free generic function that takes an `update`
//! closure keeps this module orthogonal to the rest of the crate
//! while still being generic over any cornichon menu type.
use crateMenuAction;
use crateMenuEvent;
/// Drive a menu's update loop from an async event source.
///
/// `next_event` is polled for each iteration via `next_event().await`;
/// it must yield the next event, or `None` if the source is closed.
///
/// The menu is mutated in place. Mouse events are passed through but
/// default menus return [`MenuAction::None`] for them — the consumer
/// reads the menu's `selection()` etc. afterwards if they need a
/// click position.
///
/// Returns the first `MenuAction::Selected` or `MenuAction::Cancelled`
/// produced by the menu, or `None` if the source closes first.
pub async
/// A small adapter trait so `drive_until_action` can call `dispatch`
/// uniformly across all cornichon menu types.
///
/// Each menu implements this via its inherent `dispatch` method
/// (added in the mouse-support feature work). This trait is
/// **sealed** to the crate — only the menu types in `application::*`
/// implement it.
// Implement `MenuDispatcher` for every menu type. Each is just a
// one-liner that delegates to the inherent `dispatch` method.
//
// We list them by hand here rather than via a blanket impl to keep
// the trait surface minimal and self-documenting at the call site.