Lines
95 %
Functions
83.33 %
Branches
100 %
//! Eval reply routing types.
//!
//! A `Route` is inserted into `App::pending_routes` when a nomiscript form is
//! submitted; `drain_eval` removes it on reply and calls `deliver_reply` with
//! it. `RouteCtx` carries the per-request context needed to parse the reply
//! correctly when the target view requires more than the wire frame alone.
use cli_core::forms::FormOption;
use forms::{FormValues, Source};
use ratatui::layout::Size;
use scripting::nomiscript::Value;
use crate::app::Lookup;
use crate::tabs::reports::ReportKind;
use crate::view::ViewId;
use crate::widgets::Hint;
use crate::window::{Origin, WindowId};
/// Which select columns a `FormOptions` reply populates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormOptionsSource {
/// `(form-options "accounts")`, for the transaction forms.
FormAccounts,
/// `(form-options "commodities")`, for the transaction forms.
FormCommodities,
}
/// A tested draft waiting for the labels of its accounts and commodities.
pub struct DraftLabels {
pub seq: u64,
pub in_form: bool,
pub draft: Value,
/// Set once the accounts arrived; the next reply holds the commodities.
pub accounts: Option<Vec<FormOption>>,
/// The replies of the Templates tab and the template forms.
pub enum TemplateRoute {
/// A `form-options "templates"` reply; a reply of an older `seq` is dropped.
List { seq: u64 },
/// A `template-create` form reply: the id of the new template.
Created,
/// `load-form` reply: opens the template-edit form on success.
Edit,
/// The `load-form` of the template whose stored source is tested; a reply of an older test is dropped.
Source { seq: u64 },
/// The `template-test` reply; `in_form` when it tested the source being edited.
Tested { seq: u64, in_form: bool },
/// The `form-options` reply that labels a draft's accounts, then its commodities.
Labels(DraftLabels),
/// The `form-options` reply that fills the template picker, opened over `origin`; a reply
/// of an older `seq` is dropped.
Pick { seq: u64, origin: Origin },
/// The `load-form` reply that fills the create form from the picked template, opened over
/// `origin`.
Load { origin: Origin },
/// The reply to the `delete-template` of the template shown as `label`.
Delete { label: String },
/// The replies of the Scripts tab and the script forms.
pub enum ScriptRoute {
/// A `form-options "scripts"` reply; a reply of an older `seq` is dropped.
/// A `script-create` form reply: the id of the new script.
/// `load-form` reply: opens the script-edit form on success.
/// `load-form` reply of the script being switched on or off.
Toggle { label: String },
/// The reply to the `script-edit` that switched the script shown as `label` on or off.
Toggled { label: String, enabled: bool },
/// The reply to the `delete-script` of the script shown as `label`.
/// Per-reply context a routed eval reply needs to be parsed correctly.
pub enum RouteCtx {
None,
Reports {
kind: ReportKind,
seq: u64,
/// The submitted values, adopted as the last report once accepted.
values: FormValues,
},
/// The `report-chart` PNG of the shown report; a reply of an older `seq` is dropped.
ReportChart {
/// The pane size the picture was asked for, in cells.
cells: Size,
/// The `report-csv` of the shown category report; a reply of an older `seq` is dropped.
ReportCsv {
file_name: String,
/// The `get-config` of the chart backend override; a reply of an older `seq` is dropped.
ChartOverride {
/// The `get-config` of the UI keys (theme and keymap); a reply of an older `seq` is dropped.
UiConfig {
/// The reply to the `config-set` calls of the Options dialog: the keys and texts it wrote.
OptionsSave {
changes: Vec<(String, String)>,
/// A `list-config` reply; a reply of an older `seq` is dropped.
Config {
/// A `balance-report` reply for the Accounts Balance column; an older `seq` is dropped.
AccountBalances {
/// A create mutation — on success, refresh the given list tab.
Mutation {
refresh: ViewId,
/// The reply to the single `delete-transaction` of `id`; its mark goes only on success.
DeleteTransaction {
id: String,
/// The reply to one `delete-transaction` of the bulk delete numbered `seq`.
BulkDelete {
/// Fetch of select options to populate a currently-open form.
///
/// `seq` correlates the reply with the form instance that requested it;
/// a late reply for a closed/superseded form is dropped.
FormOptions {
source: FormOptionsSource,
/// A `form-options` suggestion reply for the split tag list; an older `seq`
/// of its kind is dropped.
Suggestion {
hint: Hint,
/// The `merge-preview` reply of the locked merge form; a reply that is not
/// its pending form's own is dropped.
MergePreview,
/// `load-form` reply — opens the transaction-edit form on success.
TransactionEdit,
/// A `detail_form` reply — opens the read-only detail of `id`, or refills the open one in
/// place; a reply older than its viewer's `seq` is dropped.
TransactionDetail {
/// `load-form` reply — opens the account-edit form on success.
AccountEdit,
/// `load-form` reply — opens the commodity-edit form on success.
CommodityEdit,
/// A `list-ssh-keys` reply; a reply of an older `seq` is dropped.
SshKeys {
/// A `list-tags` reply; a reply of an older `seq` is dropped.
Tags {
/// A `tag-create` form reply: the id of the new tag.
TagCreate,
/// `load-form` reply: opens the tag-edit form on success.
TagEdit,
/// The reply to the `delete-tag` of the tag shown as `label`.
TagDelete {
label: String,
Template(TemplateRoute),
Script(ScriptRoute),
/// An `ssh-key-add` form reply: the id of the new key.
SshKeyAdd,
/// The reply to the `remove-ssh-key` of `fingerprint`.
SshKeyRemove {
fingerprint: String,
/// A `config-set` form reply — reloads the config list onto `key`.
ConfigSet {
key: String,
/// A `transaction-query` reply for a filter the server has not accepted yet.
TransactionFilter {
filter: FormValues,
summary: String,
/// A `form-options` reply that resolves the select values a palette command typed.
Lookup {
lookup: Lookup,
source: Source,
/// A `convert` form reply — formats a status message, no view refresh.
ConvertQuery {
amount_str: String,
from_label: String,
to_label: String,
impl RouteCtx {
/// Whether the request changes data, so a failed reply is an error to keep on screen.
#[must_use]
pub fn mutates(&self) -> bool {
self.refreshes().is_some() || matches!(self, Self::BulkDelete { .. })
/// The view whose windows go stale when this request's mutation succeeds.
pub fn refreshes(&self) -> Option<ViewId> {
match self {
Self::Mutation { refresh } => Some(*refresh),
Self::DeleteTransaction { .. } => Some(ViewId::Transactions),
Self::TagCreate | Self::TagDelete { .. } => Some(ViewId::Tags),
Self::Template(TemplateRoute::Created | TemplateRoute::Delete { .. }) => {
Some(ViewId::Templates)
Self::Script(
ScriptRoute::Created | ScriptRoute::Toggled { .. } | ScriptRoute::Delete { .. },
) => Some(ViewId::Scripts),
Self::SshKeyAdd | Self::SshKeyRemove { .. } => Some(ViewId::SshKeys),
Self::ConfigSet { .. } | Self::OptionsSave { .. } => Some(ViewId::Config),
_ => None,
/// Whether `other` asks for the same thing, so a second one is a duplicate.
pub fn is_same_request(&self, other: &Self) -> bool {
match (self, other) {
(Self::Template(a), Self::Template(b)) => {
std::mem::discriminant(a) == std::mem::discriminant(b)
(Self::Script(a), Self::Script(b)) => {
_ => std::mem::discriminant(self) == std::mem::discriminant(other),
/// Maps a pending eval reply id to its destination window (`None`: app or
/// dialog level) and any parse context.
pub struct Route {
pub target: Option<WindowId>,
pub ctx: RouteCtx,