[ledger] Core: haléře, dates, the event log and apply(), merchant keys and rules, one-to-one matching, import planning

This commit is contained in:
2026-11-03 18:52:36 +01:00
parent da672b132e
commit 6572f31b1d
14 changed files with 1731 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
//! The event log. Append only: a correction is a new event, nothing is ever edited or removed.
use crate::date::{Date, Timestamp};
use crate::import::StatementLine;
use crate::money::Haler;
use serde::{Deserialize, Serialize};
/// Ids are opaque strings the app generates (random, unguessable); the core never makes one up.
pub type EventId = String;
pub type PaymentId = String;
pub type BudgetId = String;
pub type RuleId = String;
/// A category id, e.g. `groceries` (see [`crate::categories`]).
pub type Category = String;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event {
pub id: EventId,
/// When the event was written (the app's clock), for display and sync; never for ordering.
pub at: Timestamp,
#[serde(flatten)]
pub body: EventBody,
}
impl Event {
pub fn new(id: impl Into<EventId>, at: Timestamp, body: EventBody) -> Event {
Event { id: id.into(), at, body }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Decision {
Keep,
Cancel,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum EventBody {
/// A phone saw a card payment (○).
#[serde(rename = "payment.captured")]
PaymentCaptured { payment: PaymentId, amount: Haler, date: Date, merchant: String, card: Option<String> },
/// One statement line (●). `line_key` identifies the line across imports.
#[serde(rename = "payment.imported")]
PaymentImported { payment: PaymentId, line: StatementLine, line_key: String },
/// Added by hand (◆). The merchant may be empty (cash, "groceries").
#[serde(rename = "payment.added")]
PaymentAdded {
payment: PaymentId,
amount: Haler,
date: Date,
merchant: String,
category: Option<Category>,
note: Option<String>,
},
/// A captured or hand-added payment and the statement line that confirms it are one payment.
#[serde(rename = "payment.confirmed")]
PaymentConfirmed { payment: PaymentId, line: PaymentId },
#[serde(rename = "payment.categorised")]
PaymentCategorised { payment: PaymentId, category: Category },
#[serde(rename = "payment.assigned")]
PaymentAssigned { payment: PaymentId, budget: BudgetId },
#[serde(rename = "payment.trial_marked")]
PaymentTrialMarked { payment: PaymentId, ends: Date },
#[serde(rename = "budget.created")]
BudgetCreated { budget: BudgetId, name: String, limit: Haler },
#[serde(rename = "budget.limit_set")]
BudgetLimitSet { budget: BudgetId, limit: Haler },
/// "Always <merchant key> → <category>".
#[serde(rename = "rule.created")]
RuleCreated { rule: RuleId, merchant_key: String, category: Category },
/// Keep or cancel a subscription (by its key), seen at `amount` (a positive cost) on `on`.
#[serde(rename = "subscription.decided")]
SubscriptionDecided { subscription: String, decision: Decision, amount: Haler, on: Date },
}