1
//! Delivery of a routed reply to the window or dialog it belongs to.
2

            
3
use crate::app::App;
4
use crate::route::{Route, RouteCtx};
5
use crate::tabs::config::ConfigTab;
6
use crate::tabs::fetch::Fetch;
7
use crate::tabs::list::ListTab;
8
use crate::tabs::nms::{ConsoleState, console_lines};
9
use crate::tabs::reports::ReportsTab;
10
use crate::view::ViewId;
11
use crate::window::WindowId;
12

            
13
impl App {
14
    /// Dispatch a completed reply. A single match handles every `RouteCtx` in
15
    /// one place — `route.ctx` is read exactly once, never re-inspected inside a
16
    /// nested target match.
17
947
    pub(super) fn deliver_reply(&mut self, route: Route, wire: &str) {
18
947
        let refresh = refresh_after(&route.ctx, wire);
19
947
        match route {
20
            Route {
21
                target: Some(window),
22
                ctx: RouteCtx::AccountBalances { seq },
23
26
            } => self.deliver_account_balances(window, seq, wire),
24
            Route {
25
                ctx: RouteCtx::Mutation { .. },
26
                ..
27
26
            } => self.deliver_mutation_reply(wire),
28
            Route {
29
                target,
30
                ctx: RouteCtx::DeleteTransaction { id },
31
7
            } => self.deliver_delete_reply(target, &id, wire),
32
            Route {
33
                ctx: RouteCtx::BulkDelete { seq, id },
34
                ..
35
13
            } => self.deliver_bulk_delete_reply(seq, &id, wire),
36
            Route {
37
                ctx: RouteCtx::FormOptions { seq, source },
38
                ..
39
106
            } => self.deliver_form_options_reply(wire, seq, source),
40
            Route {
41
                ctx: RouteCtx::Suggestion { seq, hint },
42
                ..
43
5
            } => self.deliver_tag_hints(seq, &hint, wire),
44
            Route {
45
                ctx: RouteCtx::MergePreview,
46
                ..
47
1
            } => {}
48
            Route {
49
                target,
50
                ctx: RouteCtx::TransactionEdit,
51
2
            } => self.deliver_transaction_edit_reply(target, wire),
52
            Route {
53
                ctx: RouteCtx::TransactionDetail { seq, id },
54
                ..
55
31
            } => self.deliver_transaction_detail(seq, id, wire),
56
            Route {
57
                target,
58
                ctx: RouteCtx::AccountEdit,
59
            } => self.deliver_account_edit_reply(target, wire),
60
            Route {
61
                target,
62
                ctx: RouteCtx::CommodityEdit,
63
            } => self.deliver_commodity_edit_reply(target, wire),
64
            Route {
65
                target: Some(window),
66
                ctx:
67
                    RouteCtx::TransactionFilter {
68
                        seq,
69
                        filter,
70
                        summary,
71
                    },
72
19
            } => self.deliver_filter_reply(window, seq, &filter, summary, wire),
73
            Route {
74
                ctx: RouteCtx::Lookup { lookup, source },
75
                ..
76
60
            } => self.deliver_lookup(lookup, source, wire),
77
            Route {
78
                target: Some(window),
79
                ctx: RouteCtx::ConfigSet { key },
80
            } => {
81
5
                if let Some(config) = self.win_mut::<ConfigTab>(window) {
82
5
                    config.follow = Some(key);
83
                }
84
5
                self.read_ui_config();
85
            }
86
            Route {
87
                target: Some(window),
88
                ctx: RouteCtx::SshKeys { seq },
89
16
            } => self.deliver_ssh_keys(window, seq, wire),
90
            Route {
91
                target: Some(window),
92
                ctx: RouteCtx::Tags { seq },
93
28
            } => self.deliver_tags(window, seq, wire),
94
            Route {
95
                target: Some(window),
96
                ctx: RouteCtx::TagCreate,
97
2
            } => self.deliver_tag_created(window, wire),
98
            Route {
99
                target,
100
                ctx: RouteCtx::TagEdit,
101
            } => self.deliver_tag_edit_reply(target, wire),
102
            Route {
103
                ctx: RouteCtx::TagDelete { label },
104
                ..
105
3
            } => self.deliver_tag_deleted(&label, wire),
106
            Route {
107
                target,
108
                ctx: RouteCtx::Template(route),
109
60
            } => self.deliver_template(target, route, wire),
110
            Route {
111
                target,
112
                ctx: RouteCtx::Script(route),
113
34
            } => self.deliver_script(target, route, wire),
114
            Route {
115
                target: Some(window),
116
                ctx: RouteCtx::SshKeyAdd,
117
2
            } => self.deliver_ssh_key_added(window, wire),
118
            Route {
119
                ctx: RouteCtx::SshKeyRemove { fingerprint },
120
                ..
121
3
            } => self.deliver_ssh_key_removed(&fingerprint, wire),
122
            Route {
123
                ctx:
124
                    RouteCtx::ConvertQuery {
125
                        amount_str,
126
                        from_label,
127
                        to_label,
128
                    },
129
                ..
130
2
            } => self.deliver_convert_reply(wire, &amount_str, &from_label, &to_label),
131
            Route {
132
                target: Some(window),
133
                ctx: RouteCtx::Reports { kind, seq, values },
134
            } => {
135
104
                if let Some(reports) = self.win_mut::<ReportsTab>(window)
136
104
                    && reports.run_seq.awaits(seq)
137
                {
138
101
                    reports.on_reply(wire, kind, values);
139
101
                    self.refetch_report_chart(window);
140
3
                }
141
            }
142
            Route {
143
                target: Some(window),
144
                ctx: RouteCtx::ReportChart { seq, cells },
145
            } => {
146
45
                let env = self.terminal.env();
147
45
                if let Some(reports) = self.win_mut::<ReportsTab>(window) {
148
45
                    reports.chart.accept(seq, cells, env, window, wire);
149
                }
150
            }
151
            Route {
152
                target: Some(window),
153
                ctx: RouteCtx::ReportCsv { seq, file_name },
154
11
            } => self.deliver_report_csv(window, seq, file_name, wire),
155
            Route {
156
                target: Some(window),
157
                ctx: RouteCtx::ChartOverride { seq },
158
61
            } => self.deliver_chart_override(window, seq, wire),
159
            Route {
160
                ctx: RouteCtx::UiConfig { seq },
161
                ..
162
121
            } => self.deliver_ui_config(seq, wire),
163
            Route {
164
                ctx: RouteCtx::OptionsSave { changes },
165
                ..
166
8
            } => self.deliver_options_saved(&changes, wire),
167
            Route {
168
                target: Some(window),
169
                ctx: RouteCtx::Config { seq },
170
            } => {
171
12
                if let Some(config) = self.win_mut::<ConfigTab>(window)
172
12
                    && config.seq.awaits(seq)
173
                {
174
10
                    config.on_reply(wire);
175
2
                }
176
            }
177
            Route {
178
                target: Some(window),
179
                ctx: RouteCtx::None,
180
134
            } => self.deliver_to_window(window, wire),
181
            Route { target: None, .. } => {}
182
        }
183
947
        if let Some(kind) = refresh {
184
55
            self.refresh_windows(kind);
185
892
        }
186
947
    }
187

            
188
    /// A reply with no special context goes to the window that asked, by the view it shows.
189
134
    fn deliver_to_window(&mut self, window: WindowId, wire: &str) {
190
134
        match self.window_view(window) {
191
            Some(ViewId::Console) => {
192
25
                if let Some(console) = self.win_mut::<ConsoleState>(window) {
193
25
                    for line in console_lines(wire) {
194
26
                        console.push_scrollback(line);
195
                    }
196
                }
197
            }
198
            Some(ViewId::Accounts) => {
199
40
                let loaded = self.win_mut::<ListTab>(window).is_some_and(|list| {
200
40
                    list.on_reply(wire);
201
40
                    matches!(list.state, Fetch::Loaded(_))
202
40
                });
203
40
                if loaded {
204
39
                    self.fetch_account_balances(window);
205
1
                }
206
            }
207
66
            Some(ViewId::Transactions) => self.deliver_transactions_reply(window, wire),
208
            Some(ViewId::Commodities) => {
209
3
                if let Some(list) = self.win_mut::<ListTab>(window) {
210
3
                    list.on_reply(wire);
211
                }
212
            }
213
            Some(
214
                ViewId::Reports
215
                | ViewId::Config
216
                | ViewId::SshKeys
217
                | ViewId::Tags
218
                | ViewId::Templates
219
                | ViewId::Scripts
220
                | ViewId::Viewer,
221
            )
222
            | None => {}
223
        }
224
134
    }
225

            
226
7
    fn deliver_delete_reply(&mut self, target: Option<WindowId>, id: &str, wire: &str) {
227
7
        if cli_core::render::parse_wire(wire).is_ok() {
228
5
            self.forget_transaction(target, id);
229
2
        }
230
7
        self.deliver_mutation_reply(wire);
231
7
    }
232

            
233
    /// A successful mutation shows its notices.
234
33
    fn deliver_mutation_reply(&mut self, wire: &str) {
235
33
        match cli_core::render::parse_wire(wire) {
236
            Ok(_) => {
237
24
                let notices = rpc::parse_notices(wire);
238
24
                if !notices.is_empty() {
239
5
                    self.status = notices.join("; ");
240
19
                }
241
            }
242
9
            Err(e) => self.status = format!("mutation failed: {e}"),
243
        }
244
33
    }
245
}
246

            
247
/// The view whose windows go stale once `ctx`'s mutation has succeeded.
248
968
pub(super) fn refresh_after(ctx: &RouteCtx, wire: &str) -> Option<ViewId> {
249
968
    ctx.refreshes()
250
87
        .filter(|_| cli_core::render::parse_wire(wire).is_ok())
251
968
}