1
//! Eval reply routing types.
2
//!
3
//! A `Route` is inserted into `App::pending_routes` when a nomiscript form is
4
//! submitted; `drain_eval` removes it on reply and calls `deliver_reply` with
5
//! it.  `RouteCtx` carries the per-request context needed to parse the reply
6
//! correctly when the target view requires more than the wire frame alone.
7

            
8
use cli_core::forms::FormOption;
9
use forms::{FormValues, Source};
10
use ratatui::layout::Size;
11
use scripting::nomiscript::Value;
12

            
13
use crate::app::Lookup;
14
use crate::tabs::reports::ReportKind;
15
use crate::view::ViewId;
16
use crate::widgets::Hint;
17
use crate::window::{Origin, WindowId};
18

            
19
/// Which select columns a `FormOptions` reply populates.
20
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21
pub enum FormOptionsSource {
22
    /// `(form-options "accounts")`, for the transaction forms.
23
    FormAccounts,
24
    /// `(form-options "commodities")`, for the transaction forms.
25
    FormCommodities,
26
}
27

            
28
/// A tested draft waiting for the labels of its accounts and commodities.
29
pub struct DraftLabels {
30
    pub seq: u64,
31
    pub in_form: bool,
32
    pub draft: Value,
33
    /// Set once the accounts arrived; the next reply holds the commodities.
34
    pub accounts: Option<Vec<FormOption>>,
35
}
36

            
37
/// The replies of the Templates tab and the template forms.
38
pub enum TemplateRoute {
39
    /// A `form-options "templates"` reply; a reply of an older `seq` is dropped.
40
    List { seq: u64 },
41
    /// A `template-create` form reply: the id of the new template.
42
    Created,
43
    /// `load-form` reply: opens the template-edit form on success.
44
    Edit,
45
    /// The `load-form` of the template whose stored source is tested; a reply of an older test is dropped.
46
    Source { seq: u64 },
47
    /// The `template-test` reply; `in_form` when it tested the source being edited.
48
    Tested { seq: u64, in_form: bool },
49
    /// The `form-options` reply that labels a draft's accounts, then its commodities.
50
    Labels(DraftLabels),
51
    /// The `form-options` reply that fills the template picker, opened over `origin`; a reply
52
    /// of an older `seq` is dropped.
53
    Pick { seq: u64, origin: Origin },
54
    /// The `load-form` reply that fills the create form from the picked template, opened over
55
    /// `origin`.
56
    Load { origin: Origin },
57
    /// The reply to the `delete-template` of the template shown as `label`.
58
    Delete { label: String },
59
}
60

            
61
/// The replies of the Scripts tab and the script forms.
62
pub enum ScriptRoute {
63
    /// A `form-options "scripts"` reply; a reply of an older `seq` is dropped.
64
    List { seq: u64 },
65
    /// A `script-create` form reply: the id of the new script.
66
    Created,
67
    /// `load-form` reply: opens the script-edit form on success.
68
    Edit,
69
    /// `load-form` reply of the script being switched on or off.
70
    Toggle { label: String },
71
    /// The reply to the `script-edit` that switched the script shown as `label` on or off.
72
    Toggled { label: String, enabled: bool },
73
    /// The reply to the `delete-script` of the script shown as `label`.
74
    Delete { label: String },
75
}
76

            
77
/// Per-reply context a routed eval reply needs to be parsed correctly.
78
pub enum RouteCtx {
79
    None,
80
    Reports {
81
        kind: ReportKind,
82
        seq: u64,
83
        /// The submitted values, adopted as the last report once accepted.
84
        values: FormValues,
85
    },
86
    /// The `report-chart` PNG of the shown report; a reply of an older `seq` is dropped.
87
    ReportChart {
88
        seq: u64,
89
        /// The pane size the picture was asked for, in cells.
90
        cells: Size,
91
    },
92
    /// The `report-csv` of the shown category report; a reply of an older `seq` is dropped.
93
    ReportCsv {
94
        seq: u64,
95
        file_name: String,
96
    },
97
    /// The `get-config` of the chart backend override; a reply of an older `seq` is dropped.
98
    ChartOverride {
99
        seq: u64,
100
    },
101
    /// The `get-config` of the UI keys (theme and keymap); a reply of an older `seq` is dropped.
102
    UiConfig {
103
        seq: u64,
104
    },
105
    /// The reply to the `config-set` calls of the Options dialog: the keys and texts it wrote.
106
    OptionsSave {
107
        changes: Vec<(String, String)>,
108
    },
109
    /// A `list-config` reply; a reply of an older `seq` is dropped.
110
    Config {
111
        seq: u64,
112
    },
113
    /// A `balance-report` reply for the Accounts Balance column; an older `seq` is dropped.
114
    AccountBalances {
115
        seq: u64,
116
    },
117
    /// A create mutation — on success, refresh the given list tab.
118
    Mutation {
119
        refresh: ViewId,
120
    },
121
    /// The reply to the single `delete-transaction` of `id`; its mark goes only on success.
122
    DeleteTransaction {
123
        id: String,
124
    },
125
    /// The reply to one `delete-transaction` of the bulk delete numbered `seq`.
126
    BulkDelete {
127
        seq: u64,
128
        id: String,
129
    },
130
    /// Fetch of select options to populate a currently-open form.
131
    ///
132
    /// `seq` correlates the reply with the form instance that requested it;
133
    /// a late reply for a closed/superseded form is dropped.
134
    FormOptions {
135
        seq: u64,
136
        source: FormOptionsSource,
137
    },
138
    /// A `form-options` suggestion reply for the split tag list; an older `seq`
139
    /// of its kind is dropped.
140
    Suggestion {
141
        seq: u64,
142
        hint: Hint,
143
    },
144
    /// The `merge-preview` reply of the locked merge form; a reply that is not
145
    /// its pending form's own is dropped.
146
    MergePreview,
147
    /// `load-form` reply — opens the transaction-edit form on success.
148
    TransactionEdit,
149
    /// A `detail_form` reply — opens the read-only detail of `id`, or refills the open one in
150
    /// place; a reply older than its viewer's `seq` is dropped.
151
    TransactionDetail {
152
        seq: u64,
153
        id: String,
154
    },
155
    /// `load-form` reply — opens the account-edit form on success.
156
    AccountEdit,
157
    /// `load-form` reply — opens the commodity-edit form on success.
158
    CommodityEdit,
159
    /// A `list-ssh-keys` reply; a reply of an older `seq` is dropped.
160
    SshKeys {
161
        seq: u64,
162
    },
163
    /// A `list-tags` reply; a reply of an older `seq` is dropped.
164
    Tags {
165
        seq: u64,
166
    },
167
    /// A `tag-create` form reply: the id of the new tag.
168
    TagCreate,
169
    /// `load-form` reply: opens the tag-edit form on success.
170
    TagEdit,
171
    /// The reply to the `delete-tag` of the tag shown as `label`.
172
    TagDelete {
173
        label: String,
174
    },
175
    Template(TemplateRoute),
176
    Script(ScriptRoute),
177
    /// An `ssh-key-add` form reply: the id of the new key.
178
    SshKeyAdd,
179
    /// The reply to the `remove-ssh-key` of `fingerprint`.
180
    SshKeyRemove {
181
        fingerprint: String,
182
    },
183
    /// A `config-set` form reply — reloads the config list onto `key`.
184
    ConfigSet {
185
        key: String,
186
    },
187
    /// A `transaction-query` reply for a filter the server has not accepted yet.
188
    TransactionFilter {
189
        seq: u64,
190
        filter: FormValues,
191
        summary: String,
192
    },
193
    /// A `form-options` reply that resolves the select values a palette command typed.
194
    Lookup {
195
        lookup: Lookup,
196
        source: Source,
197
    },
198
    /// A `convert` form reply — formats a status message, no view refresh.
199
    ConvertQuery {
200
        amount_str: String,
201
        from_label: String,
202
        to_label: String,
203
    },
204
}
205

            
206
impl RouteCtx {
207
    /// Whether the request changes data, so a failed reply is an error to keep on screen.
208
    #[must_use]
209
797
    pub fn mutates(&self) -> bool {
210
797
        self.refreshes().is_some() || matches!(self, Self::BulkDelete { .. })
211
797
    }
212

            
213
    /// The view whose windows go stale when this request's mutation succeeds.
214
    #[must_use]
215
1765
    pub fn refreshes(&self) -> Option<ViewId> {
216
1765
        match self {
217
56
            Self::Mutation { refresh } => Some(*refresh),
218
15
            Self::DeleteTransaction { .. } => Some(ViewId::Transactions),
219
15
            Self::TagCreate | Self::TagDelete { .. } => Some(ViewId::Tags),
220
            Self::Template(TemplateRoute::Created | TemplateRoute::Delete { .. }) => {
221
13
                Some(ViewId::Templates)
222
            }
223
            Self::Script(
224
                ScriptRoute::Created | ScriptRoute::Toggled { .. } | ScriptRoute::Delete { .. },
225
24
            ) => Some(ViewId::Scripts),
226
14
            Self::SshKeyAdd | Self::SshKeyRemove { .. } => Some(ViewId::SshKeys),
227
32
            Self::ConfigSet { .. } | Self::OptionsSave { .. } => Some(ViewId::Config),
228
1596
            _ => None,
229
        }
230
1765
    }
231

            
232
    /// Whether `other` asks for the same thing, so a second one is a duplicate.
233
    #[must_use]
234
3
    pub fn is_same_request(&self, other: &Self) -> bool {
235
3
        match (self, other) {
236
            (Self::Template(a), Self::Template(b)) => {
237
1
                std::mem::discriminant(a) == std::mem::discriminant(b)
238
            }
239
            (Self::Script(a), Self::Script(b)) => {
240
                std::mem::discriminant(a) == std::mem::discriminant(b)
241
            }
242
2
            _ => std::mem::discriminant(self) == std::mem::discriminant(other),
243
        }
244
3
    }
245
}
246

            
247
/// Maps a pending eval reply id to its destination window (`None`: app or
248
/// dialog level) and any parse context.
249
pub struct Route {
250
    pub target: Option<WindowId>,
251
    pub ctx: RouteCtx,
252
}