Skip to main content

rustledger_core/
implicit_prices.rs

1//! Shared implicit-price extraction logic.
2//!
3//! Mirrors Python beancount's `implicit_prices` plugin behavior. Used
4//! by BOTH the BQL query path (`rustledger-query::price`) and the
5//! native `implicit_prices` plugin (`rustledger-plugin`). Centralizing
6//! avoids the parallel-implementations divergence that produced
7//! [issue #992]: the plugin emitted `@@` total amounts as per-unit
8//! prices, while the query path correctly divided them.
9//!
10//! The helper is generic over the currency type (`T`) because the
11//! plugin and query paths use different transaction representations
12//! (`crate::Transaction` with `InternedStr` vs
13//! `rustledger_plugin_types::TransactionData` with `String`). Each
14//! caller assembles its annotation/cost descriptors with its own
15//! currency type and the helper returns the per-unit price already
16//! paired with the matching currency — making mismatched
17//! (number, currency) pairs impossible to construct.
18//!
19//! [issue #992]: https://github.com/rustledger/rustledger/issues/992
20
21use rust_decimal::Decimal;
22
23/// Decide the per-unit price implied by a posting and the quote
24/// currency to pair with it.
25///
26/// The currency is returned alongside the per-unit `Decimal` so that
27/// callers can never accidentally pair a cost-derived value with the
28/// annotation currency. The helper matches each value to the currency
29/// that came in with it.
30///
31/// Resolution order, mirroring upstream beancount's
32/// `beancount.plugins.implicit_prices`:
33///
34/// 1. **Price annotation** (`@` or `@@`) — if a parsed number and
35///    currency are present (`annotation` is `Some`).
36///    For `@@` (`is_total = true`), divides the total by
37///    `units_number.abs()`. For `@` (`is_total = false`), returns the
38///    number directly.
39/// 2. **Cost spec** — only as a fallback when no usable price
40///    annotation. `CostNumber::PerUnit` and `PerUnitFromTotal` carry
41///    a per-unit value directly. `CostNumber::Total` is divided by
42///    `units_number.abs()`. (Pre-#1164 the cost spec could carry a
43///    `number_per` *and* a `number_total` simultaneously; the typed
44///    enum makes that state unrepresentable.)
45/// 3. **No price** — returns `None`.
46///
47/// Edge cases:
48/// - Zero units with a total-form input (annotation `@@` or
49///   `CostNumber::Total`): can't compute per-unit, falls through to
50///   the next priority. If nothing else is available, returns `None`.
51/// - Zero units with a per-unit-form input (annotation `@`,
52///   `CostNumber::PerUnit`, or `PerUnitFromTotal`): the per-unit
53///   amount is returned as-is — "1 share = $X regardless of how many
54///   shares you transacted."
55///
56/// # Parameters
57///
58/// - `units_number`: the posting's unit count (sign-insensitive — the
59///   helper uses `.abs()` internally for total-form division).
60/// - `annotation`: `Some((is_total, amount, currency))` if the posting
61///   has a usable `@`/`@@` annotation. Callers should pass `None` for
62///   incomplete annotations (e.g. `@ EUR` without a number) so the
63///   helper falls through cleanly.
64/// - `cost`: `Some((number, currency))` if the posting has a `{...}`
65///   cost spec. The number is the typed `Option<CostNumber>` from the
66///   spec — `None` means the spec carried no number at all (`{}`),
67///   `Some(CostNumber::PerUnit)` and `Some(CostNumber::Total)` carry
68///   their respective shapes. The mutual-exclusion invariant is
69///   enforced by the type — pre-#1164 this was a `(Option<Decimal>,
70///   Option<Decimal>)` pair that the helper had to defensively
71///   tie-break.
72#[must_use]
73pub fn extract_per_unit_price<T>(
74    units_number: Decimal,
75    annotation: Option<(bool, Decimal, T)>,
76    cost: Option<(Option<crate::CostNumber>, T)>,
77) -> Option<(Decimal, T)> {
78    // Priority 1: price annotation.
79    if let Some((is_total, amount, currency)) = annotation {
80        if is_total {
81            if !units_number.is_zero() {
82                return Some((amount / units_number.abs(), currency));
83            }
84            // Zero units + @@ → can't compute per-unit, fall through
85            // to cost. Currency is dropped along with the value, so the
86            // cost branch picks the cost's currency, not this one.
87        } else {
88            return Some((amount, currency));
89        }
90    }
91
92    // Priority 2: cost spec. The pre-#1164 per-vs-total tie-break
93    // becomes a single match: PerUnit and PerUnitFromTotal already
94    // carry the per-unit value; Total carries a magnitude that
95    // divides by `|units|` here.
96    if let Some((number, currency)) = cost {
97        match number {
98            Some(crate::CostNumber::PerUnit { value: per }) => {
99                return Some((per, currency));
100            }
101            Some(crate::CostNumber::PerUnitFromTotal(b)) => {
102                return Some((b.per_unit, currency));
103            }
104            Some(crate::CostNumber::Total { value: total }) if !units_number.is_zero() => {
105                return Some((total / units_number.abs(), currency));
106            }
107            Some(crate::CostNumber::Total { value: _ }) | None => {}
108        }
109    }
110
111    None
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use rust_decimal_macros::dec;
118
119    // Tests use `&'static str` for the currency type for readability.
120    // Real callers pass `InternedStr` (query path) or `String`
121    // (plugin path).
122
123    // ===== Annotation cases =====
124
125    #[test]
126    fn unit_annotation_returns_amount_directly() {
127        // @ 1.40 EUR with 5 units → 1.40 (per-unit, used as-is).
128        let p = extract_per_unit_price(dec!(5), Some((false, dec!(1.40), "EUR")), None);
129        assert_eq!(p, Some((dec!(1.40), "EUR")));
130    }
131
132    #[test]
133    fn total_annotation_divides_by_unit_count() {
134        // @@ 1500 USD with 10 units → 1500 / 10 = 150.
135        let p = extract_per_unit_price(dec!(10), Some((true, dec!(1500), "USD")), None);
136        assert_eq!(p, Some((dec!(150), "USD")));
137    }
138
139    #[test]
140    fn total_annotation_uses_abs_unit_count() {
141        // The classic #992 reproducer: @@ 15152.07 EUR with -27204.53 BAM
142        // must produce 15152.07 / 27204.53 ≈ 0.557 (NOT -0.557, NOT 15152.07).
143        let p = extract_per_unit_price(dec!(-27204.53), Some((true, dec!(15152.07), "EUR")), None);
144        let expected = dec!(15152.07) / dec!(27204.53);
145        assert_eq!(p, Some((expected, "EUR")));
146        assert!(p.unwrap().0 > dec!(0.55) && p.unwrap().0 < dec!(0.56));
147    }
148
149    #[test]
150    fn total_annotation_with_zero_units_falls_through_to_cost() {
151        // @@ 100 EUR on 0 units → can't compute per-unit, fall through.
152        // Cost is `{50 USD}` so we return (50, "USD"), NOT (50, "EUR")
153        // — that's the Copilot-flagged bug from PR #997. Returning the
154        // currency alongside the value makes the mismatched pair
155        // impossible to construct.
156        let p = extract_per_unit_price(
157            dec!(0),
158            Some((true, dec!(100), "EUR")),
159            Some((Some(crate::CostNumber::PerUnit { value: dec!(50) }), "USD")),
160        );
161        assert_eq!(p, Some((dec!(50), "USD")));
162    }
163
164    #[test]
165    fn total_annotation_with_zero_units_and_no_cost_returns_none() {
166        let p = extract_per_unit_price::<&str>(dec!(0), Some((true, dec!(100), "EUR")), None);
167        assert_eq!(p, None);
168    }
169
170    // ===== Cost cases =====
171
172    #[test]
173    fn cost_per_unit_used_when_no_annotation() {
174        // 10 ABC {50.00 USD} → 50.00.
175        let p = extract_per_unit_price(
176            dec!(10),
177            None,
178            Some((
179                Some(crate::CostNumber::PerUnit { value: dec!(50.00) }),
180                "USD",
181            )),
182        );
183        assert_eq!(p, Some((dec!(50.00), "USD")));
184    }
185
186    #[test]
187    fn cost_total_divides_by_unit_count() {
188        // 10 ABC {{500 USD}} → 500 / 10 = 50.
189        let p = extract_per_unit_price(
190            dec!(10),
191            None,
192            Some((Some(crate::CostNumber::Total { value: dec!(500) }), "USD")),
193        );
194        assert_eq!(p, Some((dec!(50), "USD")));
195    }
196
197    #[test]
198    fn cost_total_with_zero_units_returns_none() {
199        let p = extract_per_unit_price::<&str>(
200            dec!(0),
201            None,
202            Some((Some(crate::CostNumber::Total { value: dec!(500) }), "USD")),
203        );
204        assert_eq!(p, None);
205    }
206
207    // ===== Priority interactions =====
208
209    #[test]
210    fn annotation_wins_over_cost_when_both_present() {
211        // 5 ABC {1.25 EUR} @ 1.40 EUR → 1.40 (annotation wins).
212        // Currency comes from the annotation — but in this case both
213        // happen to be EUR so the test cannot distinguish a buggy
214        // unconditional `annotation_currency.or(cost_currency)` from
215        // the correct source-aware pick. See the zero-units test for
216        // that distinction.
217        let p = extract_per_unit_price(
218            dec!(5),
219            Some((false, dec!(1.40), "EUR")),
220            Some((
221                Some(crate::CostNumber::PerUnit { value: dec!(1.25) }),
222                "EUR",
223            )),
224        );
225        assert_eq!(p, Some((dec!(1.40), "EUR")));
226    }
227
228    #[test]
229    fn total_annotation_wins_over_cost_per_unit() {
230        // -10 ABC {1.25 EUR} @@ 14 EUR → 14 / 10 = 1.40 (annotation wins).
231        let p = extract_per_unit_price(
232            dec!(-10),
233            Some((true, dec!(14), "EUR")),
234            Some((
235                Some(crate::CostNumber::PerUnit { value: dec!(1.25) }),
236                "EUR",
237            )),
238        );
239        assert_eq!(p, Some((dec!(1.4), "EUR")));
240    }
241
242    // Note: the pre-#1164 "cost_per wins over cost_total when both are set"
243    // tie-break test is gone — the type system now forbids the both-set
244    // state at construction time, so there's nothing to tie-break.
245
246    // ===== Empty cases =====
247
248    #[test]
249    fn no_inputs_returns_none() {
250        let p = extract_per_unit_price::<&str>(dec!(10), None, None);
251        assert_eq!(p, None);
252    }
253
254    #[test]
255    fn annotation_without_amount_falls_through_to_cost() {
256        // Incomplete annotation like `@ EUR` (no number) → caller
257        // passes `None` for annotation → fall through. Cost present →
258        // use it. The returned currency is the cost's, not anything
259        // remembered from the dropped annotation.
260        let p = extract_per_unit_price(
261            dec!(10),
262            None,
263            Some((Some(crate::CostNumber::PerUnit { value: dec!(7) }), "USD")),
264        );
265        assert_eq!(p, Some((dec!(7), "USD")));
266    }
267}