rustledger-ffi-wasi 0.7.2

Rustledger FFI via WASI - JSON API for embedding in any language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Clamp and filter commands (clamp, filter-entries, clamp-entries).

use std::collections::HashMap;

use rustledger_core::{Cost, Directive, NaiveDate};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::convert::directive_to_json;
use crate::helpers::load_source;
use crate::types::{Amount, DirectiveJson, Error, Meta, Posting, PostingCost};
use crate::{API_VERSION, output_json, parse_json_error};

/// Output for clamp command.
#[derive(Serialize)]
pub struct ClampOutput {
    pub api_version: &'static str,
    pub entries: Vec<DirectiveJson>,
    /// Opening balances synthesized for the begin date.
    pub opening_balances: Vec<OpeningBalance>,
    pub errors: Vec<Error>,
}

#[derive(Serialize)]
pub struct OpeningBalance {
    pub account: String,
    pub date: String,
    pub balance: InventoryJson,
}

#[derive(Serialize)]
pub struct InventoryJson {
    pub positions: Vec<PositionJson>,
}

#[derive(Serialize, Clone)]
pub struct PositionJson {
    pub units: Amount,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cost: Option<CostJson>,
}

#[derive(Serialize, Clone)]
pub struct CostJson {
    pub number: String,
    pub currency: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
}

/// Input for filter-entries command.
#[derive(Deserialize)]
pub struct FilterEntriesInput {
    /// Array of entry objects (same format as load output).
    pub entries: Vec<serde_json::Value>,
    /// Begin date (inclusive) in ISO format (YYYY-MM-DD).
    pub begin_date: String,
    /// End date (exclusive) in ISO format (YYYY-MM-DD).
    pub end_date: String,
}

/// Output for filter-entries command.
#[derive(Serialize)]
pub struct FilterEntriesOutput {
    pub api_version: &'static str,
    pub entries: Vec<serde_json::Value>,
    pub errors: Vec<Error>,
}

/// Input for clamp-entries command.
#[derive(Deserialize)]
pub struct ClampEntriesInput {
    /// Array of entry objects (same format as load output).
    pub entries: Vec<serde_json::Value>,
    /// Begin date (inclusive) in ISO format (YYYY-MM-DD).
    pub begin_date: String,
    /// End date (exclusive) in ISO format (YYYY-MM-DD).
    pub end_date: String,
}

/// Output for clamp-entries command.
#[derive(Serialize)]
pub struct ClampEntriesOutput {
    pub api_version: &'static str,
    pub entries: Vec<serde_json::Value>,
    pub errors: Vec<Error>,
}

/// Check if an account is a balance sheet account (Assets, Liabilities, Equity).
pub fn is_balance_sheet_account(account: &str) -> bool {
    let account_type = account.split(':').next().unwrap_or("");
    matches!(account_type, "Assets" | "Liabilities" | "Equity")
}

/// Check if an account is an income statement account (Income, Expenses).
pub fn is_income_statement_account(account: &str) -> bool {
    let account_type = account.split(':').next().unwrap_or("");
    matches!(account_type, "Income" | "Expenses")
}

/// Clamp beancount source by date range.
pub fn cmd_clamp(
    source: &str,
    filename: &str,
    begin_date: Option<&str>,
    end_date: Option<&str>,
) -> i32 {
    let load = load_source(source);

    // Parse date arguments
    let begin: Option<NaiveDate> =
        begin_date.and_then(|s| NaiveDate::parse_from_str(s, "%Y-%m-%d").ok());
    let end: Option<NaiveDate> =
        end_date.and_then(|s| NaiveDate::parse_from_str(s, "%Y-%m-%d").ok());

    // Track account balances for opening balances
    let mut account_balances: HashMap<String, rustledger_core::Inventory> = HashMap::new();
    let mut opening_balances: Vec<OpeningBalance> = Vec::new();

    // Track most recent price per commodity before begin_date
    let mut latest_prices: HashMap<(String, String), (NaiveDate, Directive, u32)> = HashMap::new();

    // Filter directives
    let mut filtered_directives: Vec<(Directive, u32)> = Vec::new();

    for (directive, &line) in load.directives.iter().zip(load.directive_lines.iter()) {
        let directive_date = directive.date();

        // Check if directive is before begin date
        if let Some(begin) = begin {
            if directive_date < begin {
                // Accumulate transaction postings for opening balances
                if let Directive::Transaction(txn) = directive {
                    for posting in &txn.postings {
                        if let Some(rustledger_core::IncompleteAmount::Complete(amount)) =
                            &posting.units
                        {
                            let inv = account_balances
                                .entry(posting.account.to_string())
                                .or_default();
                            let position = if let Some(cost_spec) = &posting.cost {
                                let cost = Cost {
                                    number: cost_spec.number_per.unwrap_or(amount.number),
                                    currency: cost_spec
                                        .currency
                                        .clone()
                                        .unwrap_or_else(|| amount.currency.clone()),
                                    date: cost_spec.date.or(Some(txn.date)),
                                    label: cost_spec.label.clone(),
                                };
                                rustledger_core::Position::with_cost(amount.clone(), cost)
                            } else {
                                rustledger_core::Position::simple(amount.clone())
                            };
                            inv.add(position);
                        }
                    }
                }

                // Track most recent price per commodity before begin_date
                if let Directive::Price(price) = directive {
                    let key = (
                        price.currency.to_string(),
                        price.amount.currency.to_string(),
                    );
                    let should_update = latest_prices
                        .get(&key)
                        .map_or(true, |(existing_date, _, _)| {
                            directive_date >= *existing_date
                        });
                    if should_update {
                        latest_prices.insert(key, (directive_date, directive.clone(), line));
                    }
                }

                // Keep Open directives before begin date
                if let Directive::Open(_) = directive {
                    filtered_directives.push((directive.clone(), line));
                }
                continue;
            }
        }

        // Check if directive is after end date
        if let Some(end) = end {
            if directive_date >= end {
                continue;
            }
        }

        // Exclude Commodity entries from output
        if let Directive::Commodity(_) = directive {
            continue;
        }

        filtered_directives.push((directive.clone(), line));
    }

    // Add most recent prices before begin_date
    let mut price_entries: Vec<(Directive, u32)> = latest_prices
        .into_values()
        .map(|(_, directive, line)| (directive, line))
        .collect();
    price_entries.sort_by(|(a, _), (b, _)| a.date().cmp(&b.date()));

    // Generate opening balance summarization transactions
    let mut summarization_entries: Vec<DirectiveJson> = Vec::new();
    if let Some(begin) = begin {
        let summarize_date = begin.pred_opt().unwrap_or(begin);
        let summarize_date_str = summarize_date.to_string();

        let mut balance_sheet_accounts: Vec<(&String, &rustledger_core::Inventory)> = Vec::new();
        let mut retained_earnings: rustledger_core::Inventory = rustledger_core::Inventory::new();

        for (account, inventory) in &account_balances {
            if inventory.is_empty() {
                continue;
            }

            if is_balance_sheet_account(account) {
                balance_sheet_accounts.push((account, inventory));
            } else if is_income_statement_account(account) {
                for position in inventory.positions() {
                    retained_earnings.add(position.clone());
                }
            }
        }

        balance_sheet_accounts.sort_by_key(|(account, _)| *account);

        // Create summarization transactions for balance sheet accounts
        for (index, (account, inventory)) in balance_sheet_accounts.iter().enumerate() {
            let positions: Vec<PositionJson> = inventory
                .positions()
                .iter()
                .map(|p| PositionJson {
                    units: Amount {
                        number: p.units.number.to_string(),
                        currency: p.units.currency.to_string(),
                    },
                    cost: p.cost.as_ref().map(|c| CostJson {
                        number: c.number.to_string(),
                        currency: c.currency.to_string(),
                        date: c.date.map(|d| d.to_string()),
                        label: c.label.clone(),
                    }),
                })
                .collect();

            opening_balances.push(OpeningBalance {
                account: (*account).clone(),
                date: begin.to_string(),
                balance: InventoryJson {
                    positions: positions.clone(),
                },
            });

            let postings: Vec<Posting> = inventory
                .positions()
                .iter()
                .map(|p| Posting {
                    account: (*account).clone(),
                    units: Some(Amount {
                        number: p.units.number.to_string(),
                        currency: p.units.currency.to_string(),
                    }),
                    cost: p.cost.as_ref().map(|c| PostingCost {
                        number: Some(c.number.to_string()),
                        number_total: None,
                        currency: Some(c.currency.to_string()),
                        date: c.date.map(|d| d.to_string()),
                        label: c.label.clone(),
                    }),
                    price: None,
                    meta: HashMap::new(),
                })
                .collect();

            let hash_input = format!(
                "S|{}|Opening balance for '{}' (Summarization)|{}",
                summarize_date_str, account, index
            );
            let hash = format!("{:x}", Sha256::digest(hash_input.as_bytes()));

            summarization_entries.push(DirectiveJson::Transaction {
                date: summarize_date_str.clone(),
                flag: "S".to_string(),
                payee: None,
                narration: Some(format!("Opening balance for '{}' (Summarization)", account)),
                tags: vec![],
                links: vec![],
                postings,
                meta: Meta {
                    filename: "<summarize>".to_string(),
                    lineno: index as u32,
                    hash,
                    user: HashMap::new(),
                },
            });
        }

        // Create aggregated Equity:Earnings:Previous transaction
        if !retained_earnings.is_empty() {
            let earnings_account = "Equity:Earnings:Previous";
            let index = balance_sheet_accounts.len();

            let positions: Vec<PositionJson> = retained_earnings
                .positions()
                .iter()
                .map(|p| PositionJson {
                    units: Amount {
                        number: p.units.number.to_string(),
                        currency: p.units.currency.to_string(),
                    },
                    cost: p.cost.as_ref().map(|c| CostJson {
                        number: c.number.to_string(),
                        currency: c.currency.to_string(),
                        date: c.date.map(|d| d.to_string()),
                        label: c.label.clone(),
                    }),
                })
                .collect();

            opening_balances.push(OpeningBalance {
                account: earnings_account.to_string(),
                date: begin.to_string(),
                balance: InventoryJson {
                    positions: positions.clone(),
                },
            });

            let postings: Vec<Posting> = retained_earnings
                .positions()
                .iter()
                .map(|p| Posting {
                    account: earnings_account.to_string(),
                    units: Some(Amount {
                        number: p.units.number.to_string(),
                        currency: p.units.currency.to_string(),
                    }),
                    cost: p.cost.as_ref().map(|c| PostingCost {
                        number: Some(c.number.to_string()),
                        number_total: None,
                        currency: Some(c.currency.to_string()),
                        date: c.date.map(|d| d.to_string()),
                        label: c.label.clone(),
                    }),
                    price: None,
                    meta: HashMap::new(),
                })
                .collect();

            let hash_input = format!(
                "S|{}|Opening balance for '{}' (Summarization)|{}",
                summarize_date_str, earnings_account, index
            );
            let hash = format!("{:x}", Sha256::digest(hash_input.as_bytes()));

            summarization_entries.push(DirectiveJson::Transaction {
                date: summarize_date_str.clone(),
                flag: "S".to_string(),
                payee: None,
                narration: Some(format!(
                    "Opening balance for '{}' (Summarization)",
                    earnings_account
                )),
                tags: vec![],
                links: vec![],
                postings,
                meta: Meta {
                    filename: "<summarize>".to_string(),
                    lineno: index as u32,
                    hash,
                    user: HashMap::new(),
                },
            });
        }
    }

    // Convert filtered directives to JSON
    let mut entries: Vec<DirectiveJson> = filtered_directives
        .iter()
        .map(|(d, line)| directive_to_json(d, *line, filename))
        .collect();

    let price_json: Vec<DirectiveJson> = price_entries
        .iter()
        .map(|(d, line)| directive_to_json(d, *line, filename))
        .collect();

    entries.splice(0..0, summarization_entries);
    entries.splice(0..0, price_json);

    let output = ClampOutput {
        api_version: API_VERSION,
        entries,
        opening_balances,
        errors: load.errors,
    };
    output_json(&output)
}

/// Filter already-parsed entries by date range.
pub fn cmd_filter_entries(json_str: &str) -> i32 {
    let input: FilterEntriesInput = match serde_json::from_str(json_str) {
        Ok(i) => i,
        Err(e) => {
            let output = FilterEntriesOutput {
                api_version: API_VERSION,
                entries: vec![],
                errors: vec![parse_json_error(&e)],
            };
            return output_json(&output);
        }
    };

    let Ok(begin) = NaiveDate::parse_from_str(&input.begin_date, "%Y-%m-%d") else {
        let output = FilterEntriesOutput {
            api_version: API_VERSION,
            entries: vec![],
            errors: vec![
                Error::new(format!(
                    "Invalid begin_date format: {}. Expected YYYY-MM-DD",
                    input.begin_date
                ))
                .with_field("begin_date"),
            ],
        };
        return output_json(&output);
    };

    let Ok(end) = NaiveDate::parse_from_str(&input.end_date, "%Y-%m-%d") else {
        let output = FilterEntriesOutput {
            api_version: API_VERSION,
            entries: vec![],
            errors: vec![
                Error::new(format!(
                    "Invalid end_date format: {}. Expected YYYY-MM-DD",
                    input.end_date
                ))
                .with_field("end_date"),
            ],
        };
        return output_json(&output);
    };

    let mut filtered: Vec<serde_json::Value> = Vec::new();

    for entry in input.entries {
        let entry_type = entry.get("type").and_then(|t| t.as_str()).unwrap_or("");
        let date_str = entry.get("date").and_then(|d| d.as_str()).unwrap_or("");

        let entry_date = match NaiveDate::parse_from_str(date_str, "%Y-%m-%d") {
            Ok(d) => d,
            Err(_) => continue,
        };

        let include = match entry_type {
            "commodity" => false,
            "open" => entry_date < end,
            "close" => entry_date >= begin,
            _ => entry_date >= begin && entry_date < end,
        };

        if include {
            filtered.push(entry);
        }
    }

    let output = FilterEntriesOutput {
        api_version: API_VERSION,
        entries: filtered,
        errors: vec![],
    };
    output_json(&output)
}

/// Clamp already-parsed entries by date range with summarization.
pub fn cmd_clamp_entries(json_str: &str) -> i32 {
    let input: ClampEntriesInput = match serde_json::from_str(json_str) {
        Ok(i) => i,
        Err(e) => {
            let output = ClampEntriesOutput {
                api_version: API_VERSION,
                entries: vec![],
                errors: vec![parse_json_error(&e)],
            };
            return output_json(&output);
        }
    };

    let Ok(begin) = NaiveDate::parse_from_str(&input.begin_date, "%Y-%m-%d") else {
        let output = ClampEntriesOutput {
            api_version: API_VERSION,
            entries: vec![],
            errors: vec![
                Error::new(format!(
                    "Invalid begin_date format: {}. Expected YYYY-MM-DD",
                    input.begin_date
                ))
                .with_field("begin_date"),
            ],
        };
        return output_json(&output);
    };

    let Ok(end) = NaiveDate::parse_from_str(&input.end_date, "%Y-%m-%d") else {
        let output = ClampEntriesOutput {
            api_version: API_VERSION,
            entries: vec![],
            errors: vec![
                Error::new(format!(
                    "Invalid end_date format: {}. Expected YYYY-MM-DD",
                    input.end_date
                ))
                .with_field("end_date"),
            ],
        };
        return output_json(&output);
    };

    let mut account_balances: HashMap<String, rustledger_core::Inventory> = HashMap::new();
    let mut latest_prices: HashMap<(String, String), (NaiveDate, serde_json::Value)> =
        HashMap::new();
    let mut filtered_entries: Vec<serde_json::Value> = Vec::new();

    for entry in input.entries {
        let entry_type = entry.get("type").and_then(|t| t.as_str()).unwrap_or("");
        let date_str = entry.get("date").and_then(|d| d.as_str()).unwrap_or("");

        let entry_date = match NaiveDate::parse_from_str(date_str, "%Y-%m-%d") {
            Ok(d) => d,
            Err(_) => continue,
        };

        if entry_date < begin {
            if entry_type == "transaction" {
                if let Some(postings) = entry.get("postings").and_then(|p| p.as_array()) {
                    for posting in postings {
                        let account = posting
                            .get("account")
                            .and_then(|a| a.as_str())
                            .unwrap_or("");
                        if account.is_empty() {
                            continue;
                        }

                        if let Some(units) = posting.get("units") {
                            let number_str =
                                units.get("number").and_then(|n| n.as_str()).unwrap_or("0");
                            let currency =
                                units.get("currency").and_then(|c| c.as_str()).unwrap_or("");

                            if let Ok(number) = rustledger_core::Decimal::from_str_exact(number_str)
                            {
                                let amount = rustledger_core::Amount::new(number, currency);
                                let inv = account_balances.entry(account.to_string()).or_default();

                                let position = if let Some(cost) = posting.get("cost") {
                                    let cost_number_str =
                                        cost.get("number").and_then(|n| n.as_str()).unwrap_or("0");
                                    let cost_currency =
                                        cost.get("currency").and_then(|c| c.as_str()).unwrap_or("");
                                    let cost_date_str = cost.get("date").and_then(|d| d.as_str());
                                    let cost_label = cost
                                        .get("label")
                                        .and_then(|l| l.as_str())
                                        .map(String::from);

                                    if let Ok(cost_number) =
                                        rustledger_core::Decimal::from_str_exact(cost_number_str)
                                    {
                                        let cost_date = cost_date_str.and_then(|s| {
                                            NaiveDate::parse_from_str(s, "%Y-%m-%d").ok()
                                        });
                                        let cost = Cost {
                                            number: cost_number,
                                            currency: cost_currency.into(),
                                            date: cost_date,
                                            label: cost_label,
                                        };
                                        rustledger_core::Position::with_cost(amount, cost)
                                    } else {
                                        rustledger_core::Position::simple(amount)
                                    }
                                } else {
                                    rustledger_core::Position::simple(amount)
                                };

                                inv.add(position);
                            }
                        }
                    }
                }
            }

            if entry_type == "price" {
                let currency = entry.get("currency").and_then(|c| c.as_str()).unwrap_or("");
                let quote_currency = entry
                    .get("amount")
                    .and_then(|a| a.get("currency"))
                    .and_then(|c| c.as_str())
                    .unwrap_or("");

                if !currency.is_empty() && !quote_currency.is_empty() {
                    let key = (currency.to_string(), quote_currency.to_string());
                    let should_update = latest_prices
                        .get(&key)
                        .map_or(true, |(existing_date, _)| entry_date >= *existing_date);
                    if should_update {
                        latest_prices.insert(key, (entry_date, entry.clone()));
                    }
                }
            }

            if entry_type == "open" {
                filtered_entries.push(entry);
            }
            continue;
        }

        if entry_date >= end {
            continue;
        }

        if entry_type == "commodity" {
            continue;
        }

        filtered_entries.push(entry);
    }

    let mut price_entries: Vec<serde_json::Value> = latest_prices
        .into_values()
        .map(|(_, entry)| entry)
        .collect();
    price_entries.sort_by(|a, b| {
        let date_a = a.get("date").and_then(|d| d.as_str()).unwrap_or("");
        let date_b = b.get("date").and_then(|d| d.as_str()).unwrap_or("");
        date_a.cmp(date_b)
    });

    let mut summarization_entries: Vec<serde_json::Value> = Vec::new();
    let summarize_date = begin.pred_opt().unwrap_or(begin);
    let summarize_date_str = summarize_date.to_string();

    let mut balance_sheet_accounts: Vec<(&String, &rustledger_core::Inventory)> = Vec::new();
    let mut retained_earnings: rustledger_core::Inventory = rustledger_core::Inventory::new();

    for (account, inventory) in &account_balances {
        if inventory.is_empty() {
            continue;
        }

        if is_balance_sheet_account(account) {
            balance_sheet_accounts.push((account, inventory));
        } else if is_income_statement_account(account) {
            for position in inventory.positions() {
                retained_earnings.add(position.clone());
            }
        }
    }

    balance_sheet_accounts.sort_by_key(|(account, _)| *account);

    for (index, (account, inventory)) in balance_sheet_accounts.iter().enumerate() {
        let postings: Vec<serde_json::Value> = inventory
            .positions()
            .iter()
            .map(|p| {
                let mut posting = serde_json::json!({
                    "account": account,
                    "units": {
                        "number": p.units.number.to_string(),
                        "currency": p.units.currency.to_string()
                    }
                });
                if let Some(cost) = &p.cost {
                    posting["cost"] = serde_json::json!({
                        "number": cost.number.to_string(),
                        "currency": cost.currency.to_string()
                    });
                    if let Some(date) = cost.date {
                        posting["cost"]["date"] = serde_json::json!(date.to_string());
                    }
                    if let Some(label) = &cost.label {
                        posting["cost"]["label"] = serde_json::json!(label);
                    }
                }
                posting
            })
            .collect();

        let hash_input = format!(
            "S|{}|Opening balance for '{}' (Summarization)|{}",
            summarize_date_str, account, index
        );
        let hash = format!("{:x}", Sha256::digest(hash_input.as_bytes()));

        summarization_entries.push(serde_json::json!({
            "type": "transaction",
            "date": summarize_date_str,
            "flag": "S",
            "narration": format!("Opening balance for '{}' (Summarization)", account),
            "tags": [],
            "links": [],
            "postings": postings,
            "meta": {
                "filename": "<summarize>",
                "lineno": index,
                "hash": hash
            }
        }));
    }

    if !retained_earnings.is_empty() {
        let earnings_account = "Equity:Earnings:Previous";
        let index = balance_sheet_accounts.len();

        let postings: Vec<serde_json::Value> = retained_earnings
            .positions()
            .iter()
            .map(|p| {
                let mut posting = serde_json::json!({
                    "account": earnings_account,
                    "units": {
                        "number": p.units.number.to_string(),
                        "currency": p.units.currency.to_string()
                    }
                });
                if let Some(cost) = &p.cost {
                    posting["cost"] = serde_json::json!({
                        "number": cost.number.to_string(),
                        "currency": cost.currency.to_string()
                    });
                    if let Some(date) = cost.date {
                        posting["cost"]["date"] = serde_json::json!(date.to_string());
                    }
                    if let Some(label) = &cost.label {
                        posting["cost"]["label"] = serde_json::json!(label);
                    }
                }
                posting
            })
            .collect();

        let hash_input = format!(
            "S|{}|Opening balance for '{}' (Summarization)|{}",
            summarize_date_str, earnings_account, index
        );
        let hash = format!("{:x}", Sha256::digest(hash_input.as_bytes()));

        summarization_entries.push(serde_json::json!({
            "type": "transaction",
            "date": summarize_date_str,
            "flag": "S",
            "narration": format!("Opening balance for '{}' (Summarization)", earnings_account),
            "tags": [],
            "links": [],
            "postings": postings,
            "meta": {
                "filename": "<summarize>",
                "lineno": index,
                "hash": hash
            }
        }));
    }

    let mut result_entries: Vec<serde_json::Value> = Vec::new();
    result_entries.extend(price_entries);
    result_entries.extend(summarization_entries);
    result_entries.extend(filtered_entries);

    let output = ClampEntriesOutput {
        api_version: API_VERSION,
        entries: result_entries,
        errors: vec![],
    };
    output_json(&output)
}