rustledger-wasm 0.14.1

Beancount WebAssembly bindings for JavaScript/TypeScript
Documentation
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
//! Stateful ledger classes for WASM.
//!
//! Provides two classes:
//! - [`ParsedLedger`]: Single-file ledger with full editor features (completions, hover, etc.)
//! - [`Ledger`]: Multi-file ledger for queries and validation (no position-based editor features)

use std::collections::HashMap;
use std::path::Path;
use wasm_bindgen::prelude::*;

use rustledger_core::Directive;
use rustledger_parser::ParseResult as ParserResult;

use crate::cache;
use crate::convert::directive_to_json;
use crate::editor;
use crate::helpers::{load_and_book, run_validation, to_js};
#[cfg(feature = "plugins")]
use crate::types::PluginResult;
use crate::types::{Error, FormatResult, LedgerOptions, PadResult, QueryResult};

// =============================================================================
// Shared query/directive logic (used by both ParsedLedger and Ledger)
// =============================================================================

fn execute_query(directives: &[Directive], query_str: &str) -> Result<JsValue, JsError> {
    use crate::convert::value_to_cell;
    use rustledger_query::{Executor, parse as parse_query};

    let query = match parse_query(query_str) {
        Ok(q) => q,
        Err(e) => {
            let result = QueryResult {
                columns: Vec::new(),
                rows: Vec::new(),
                errors: vec![Error::new(e.to_string())],
            };
            return to_js(&result);
        }
    };

    let mut executor = Executor::new(directives);
    match executor.execute(&query) {
        Ok(result) => {
            let rows: Vec<Vec<_>> = result
                .rows
                .iter()
                .map(|row| row.iter().map(value_to_cell).collect())
                .collect();

            let query_result = QueryResult {
                columns: result.columns,
                rows,
                errors: Vec::new(),
            };
            to_js(&query_result)
        }
        Err(e) => {
            let result = QueryResult {
                columns: Vec::new(),
                rows: Vec::new(),
                errors: vec![Error::new(format!("Query execution error: {e}"))],
            };
            to_js(&result)
        }
    }
}

fn execute_expand_pads(directives: &[Directive]) -> Result<JsValue, JsError> {
    use rustledger_booking::process_pads;

    let pad_result = process_pads(directives);

    let result = PadResult {
        directives: pad_result
            .directives
            .iter()
            .map(directive_to_json)
            .collect(),
        padding_transactions: pad_result
            .padding_transactions
            .iter()
            .map(|txn| directive_to_json(&Directive::Transaction(txn.clone())))
            .collect(),
        errors: pad_result
            .errors
            .iter()
            .map(|e| Error::new(e.message.clone()))
            .collect(),
    };
    to_js(&result)
}

#[cfg(feature = "plugins")]
fn execute_plugin(directives: &[Directive], plugin_name: &str) -> Result<JsValue, JsError> {
    use rustledger_plugin::{
        NativePluginRegistry, PluginInput, PluginOptions, directives_to_wrappers,
        wrappers_to_directives,
    };

    let registry = NativePluginRegistry::new();
    let Some(plugin) = registry.find(plugin_name) else {
        let result = PluginResult {
            directives: Vec::new(),
            errors: vec![Error::new(format!("Unknown plugin: {plugin_name}"))],
        };
        return to_js(&result);
    };

    let wrappers = directives_to_wrappers(directives);
    let input = PluginInput {
        directives: wrappers,
        options: PluginOptions::default(),
        config: None,
    };

    let output = plugin.process(input);

    let output_directives = match wrappers_to_directives(&output.directives) {
        Ok(dirs) => dirs,
        Err(e) => {
            let result = PluginResult {
                directives: Vec::new(),
                errors: vec![Error::new(format!("Conversion error: {e}"))],
            };
            return to_js(&result);
        }
    };

    let result = PluginResult {
        directives: output_directives.iter().map(directive_to_json).collect(),
        errors: output
            .errors
            .iter()
            .map(|e| match e.severity {
                rustledger_plugin::PluginErrorSeverity::Warning => {
                    Error::warning(e.message.clone())
                }
                rustledger_plugin::PluginErrorSeverity::Error => Error::new(e.message.clone()),
            })
            .collect(),
    };
    to_js(&result)
}

// =============================================================================
// ParsedLedger: Single-file with full editor features
// =============================================================================

/// A parsed and validated single-file ledger with editor features.
///
/// Use this class for single-file ledgers where you need completions, hover,
/// go-to-definition, and other editor integration features.
///
/// For multi-file ledgers, use [`Ledger`] instead.
///
/// # Example (JavaScript)
///
/// ```javascript
/// const ledger = new ParsedLedger(source);
/// if (ledger.isValid()) {
///     const balances = ledger.query("BALANCES");
///     const completions = ledger.getCompletions(line, char);
/// }
/// ```
#[wasm_bindgen(skip_typescript)]
pub struct ParsedLedger {
    /// The original source text.
    source: String,
    /// The raw parse result (for editor features).
    parse_result: ParserResult,
    /// The booked directives.
    directives: Vec<Directive>,
    /// Ledger options.
    options: LedgerOptions,
    /// Parse errors.
    parse_errors: Vec<Error>,
    /// Validation errors.
    validation_errors: Vec<Error>,
    /// Cached editor data (accounts, currencies, payees, line index).
    editor_cache: editor::EditorCache,
}

#[wasm_bindgen]
impl ParsedLedger {
    /// Create a new `ParsedLedger` from a single source string.
    ///
    /// Parses, books, and validates the source. Call `isValid()` to check for errors.
    #[wasm_bindgen(constructor)]
    pub fn new(source: &str) -> Self {
        let load = load_and_book(source);
        let validation_errors = run_validation(&load);
        let editor_cache = editor::EditorCache::new(source, &load.parse_result);

        Self {
            source: source.to_string(),
            parse_result: load.parse_result,
            directives: load.directives,
            options: load.options,
            parse_errors: load.errors,
            validation_errors,
            editor_cache,
        }
    }

    /// Check if the ledger is valid (no parse or validation errors).
    #[wasm_bindgen(js_name = "isValid")]
    pub fn is_valid(&self) -> bool {
        self.parse_errors.is_empty() && self.validation_errors.is_empty()
    }

    /// Get all errors (parse + validation).
    #[wasm_bindgen(js_name = "getErrors")]
    pub fn get_errors(&self) -> Result<JsValue, JsError> {
        let mut all_errors = self.parse_errors.clone();
        all_errors.extend(self.validation_errors.clone());
        to_js(&all_errors)
    }

    /// Get parse errors only.
    #[wasm_bindgen(js_name = "getParseErrors")]
    pub fn get_parse_errors(&self) -> Result<JsValue, JsError> {
        to_js(&self.parse_errors)
    }

    /// Get validation errors only.
    #[wasm_bindgen(js_name = "getValidationErrors")]
    pub fn get_validation_errors(&self) -> Result<JsValue, JsError> {
        to_js(&self.validation_errors)
    }

    /// Get the parsed directives.
    #[wasm_bindgen(js_name = "getDirectives")]
    pub fn get_directives(&self) -> Result<JsValue, JsError> {
        let directives: Vec<_> = self.directives.iter().map(directive_to_json).collect();
        to_js(&directives)
    }

    /// Get the ledger options.
    #[wasm_bindgen(js_name = "getOptions")]
    pub fn get_options(&self) -> Result<JsValue, JsError> {
        to_js(&self.options)
    }

    /// Get the number of directives.
    #[wasm_bindgen(js_name = "directiveCount")]
    pub fn directive_count(&self) -> usize {
        self.directives.len()
    }

    /// Run a BQL query on this ledger.
    #[wasm_bindgen]
    pub fn query(&self, query_str: &str) -> Result<JsValue, JsError> {
        if !self.parse_errors.is_empty() {
            let result = QueryResult {
                columns: Vec::new(),
                rows: Vec::new(),
                errors: self.parse_errors.clone(),
            };
            return to_js(&result);
        }
        execute_query(&self.directives, query_str)
    }

    /// Get account balances (shorthand for query("BALANCES")).
    #[wasm_bindgen]
    pub fn balances(&self) -> Result<JsValue, JsError> {
        self.query("BALANCES")
    }

    /// Format the ledger source.
    #[wasm_bindgen]
    pub fn format(&self) -> Result<JsValue, JsError> {
        use rustledger_core::{FormatConfig, format_directive};

        if !self.parse_errors.is_empty() {
            let result = FormatResult {
                formatted: None,
                errors: self.parse_errors.clone(),
            };
            return to_js(&result);
        }

        let config = FormatConfig::default();
        let mut formatted = String::new();

        for directive in &self.directives {
            formatted.push_str(&format_directive(directive, &config));
            formatted.push('\n');
        }

        let result = FormatResult {
            formatted: Some(formatted),
            errors: Vec::new(),
        };
        to_js(&result)
    }

    /// Expand pad directives.
    #[wasm_bindgen(js_name = "expandPads")]
    pub fn expand_pads(&self) -> Result<JsValue, JsError> {
        if !self.parse_errors.is_empty() {
            let result = PadResult {
                directives: Vec::new(),
                padding_transactions: Vec::new(),
                errors: self.parse_errors.clone(),
            };
            return to_js(&result);
        }
        execute_expand_pads(&self.directives)
    }

    /// Run a native plugin on this ledger.
    #[cfg(feature = "plugins")]
    #[wasm_bindgen(js_name = "runPlugin")]
    pub fn run_plugin(&self, plugin_name: &str) -> Result<JsValue, JsError> {
        if !self.parse_errors.is_empty() {
            let result = PluginResult {
                directives: Vec::new(),
                errors: self.parse_errors.clone(),
            };
            return to_js(&result);
        }
        execute_plugin(&self.directives, plugin_name)
    }

    // =========================================================================
    // Editor Integration (LSP-like features)
    // =========================================================================

    /// Get completions at the given position.
    #[wasm_bindgen(js_name = "getCompletions")]
    pub fn get_completions(&self, line: u32, character: u32) -> Result<JsValue, JsError> {
        let result =
            editor::get_completions_cached(&self.source, line, character, &self.editor_cache);
        to_js(&result)
    }

    /// Get hover information at the given position.
    #[wasm_bindgen(js_name = "getHoverInfo")]
    pub fn get_hover_info(&self, line: u32, character: u32) -> Result<JsValue, JsError> {
        let result = editor::get_hover_info_cached(
            &self.source,
            line,
            character,
            &self.parse_result,
            &self.editor_cache,
        );
        to_js(&result)
    }

    /// Get the definition location for the symbol at the given position.
    #[wasm_bindgen(js_name = "getDefinition")]
    pub fn get_definition(&self, line: u32, character: u32) -> Result<JsValue, JsError> {
        let result = editor::get_definition_cached(
            &self.source,
            line,
            character,
            &self.parse_result,
            &self.editor_cache,
        );
        to_js(&result)
    }

    /// Get all document symbols for the outline view.
    #[wasm_bindgen(js_name = "getDocumentSymbols")]
    pub fn get_document_symbols(&self) -> Result<JsValue, JsError> {
        let result = editor::get_document_symbols_cached(&self.parse_result, &self.editor_cache);
        to_js(&result)
    }

    /// Find all references to the symbol at the given position.
    #[wasm_bindgen(js_name = "getReferences")]
    pub fn get_references(&self, line: u32, character: u32) -> Result<JsValue, JsError> {
        let result = editor::get_references_cached(
            &self.source,
            line,
            character,
            &self.parse_result,
            &self.editor_cache,
        );
        to_js(&result)
    }

    // =========================================================================
    // Serialization / Caching
    // =========================================================================

    /// Serialize this ledger to a compact binary blob (rkyv).
    ///
    /// Store the bytes in OPFS or `IndexedDB` alongside a source fingerprint
    /// (see [`crate::hash_sources`]) and restore later with [`ParsedLedger::from_cache`].
    #[wasm_bindgen]
    pub fn serialize(&self) -> Result<Vec<u8>, JsError> {
        // Clone fields into the payload. rkyv's Serialize derive requires owned
        // types; a zero-copy borrowed serializer would add significant complexity
        // for minimal gain since serialize() is called once per cache write.
        let payload = cache::ParsedLedgerPayload {
            directives: self.directives.clone(),
            options: self.options.clone(),
            parse_errors: self.parse_errors.clone(),
            validation_errors: self.validation_errors.clone(),
        };
        cache::serialize_parsed(&payload).map_err(|e| JsError::new(&e))
    }

    /// Restore a `ParsedLedger` from bytes produced by [`ParsedLedger::serialize`].
    ///
    /// The `source` parameter must be the same source text used when the cache
    /// was created; it is re-parsed (but not re-booked or re-validated) so that
    /// editor features continue to work.
    ///
    /// # Errors
    ///
    /// Returns an error if the bytes are invalid or were produced by a different
    /// library version.
    #[wasm_bindgen(js_name = "fromCache")]
    pub fn from_cache(bytes: &[u8], source: &str) -> Result<Self, JsError> {
        let mut payload = cache::deserialize_parsed(bytes).map_err(|e| JsError::new(&e))?;

        // Re-intern strings to deduplicate identical Arc<str> allocations.
        rustledger_loader::reintern_plain_directives(&mut payload.directives);

        // Re-parse source for editor spans (cheap; booking is the expensive part).
        let parse_result = rustledger_parser::parse(source);
        let editor_cache = editor::EditorCache::new(source, &parse_result);

        Ok(Self {
            source: source.to_string(),
            parse_result,
            directives: payload.directives,
            options: payload.options,
            parse_errors: payload.parse_errors,
            validation_errors: payload.validation_errors,
            editor_cache,
        })
    }
}

// =============================================================================
// Ledger: Multi-file with queries and cross-file completions
// =============================================================================

/// A fully processed multi-file ledger for queries and validation.
///
/// Use this class for ledgers that span multiple files with `include` directives.
/// Caches the processed result for efficient repeated queries.
///
/// For single-file ledgers with editor features, use [`ParsedLedger`] instead.
///
/// # Example (JavaScript)
///
/// ```javascript
/// const ledger = Ledger.fromFiles({
///     "main.beancount": 'include "accounts.beancount"\n...',
///     "accounts.beancount": "2024-01-01 open Assets:Bank USD\n..."
/// }, "main.beancount");
///
/// if (ledger.isValid()) {
///     const balances = ledger.query("BALANCES");
///     const completions = ledger.getCompletions(currentSource, line, char);
/// }
/// ```
#[wasm_bindgen(skip_typescript)]
pub struct Ledger {
    /// The booked directives from all files.
    directives: Vec<Directive>,
    /// Ledger options.
    options: LedgerOptions,
    /// Processing errors (load, booking, validation).
    errors: Vec<Error>,
    /// Editor cache for cross-file completions.
    editor_cache: editor::EditorCache,
}

#[wasm_bindgen]
impl Ledger {
    /// Create a `Ledger` from multiple files with include resolution.
    ///
    /// Loads, sorts, books, runs plugins, and validates the ledger using the
    /// same processing pipeline as the CLI.
    ///
    /// # Arguments
    ///
    /// * `files` - A JavaScript object mapping file paths to their contents.
    /// * `entry_point` - The main file to start loading from (must exist in `files`).
    #[wasm_bindgen(js_name = "fromFiles")]
    pub fn from_files(files: JsValue, entry_point: &str) -> Result<Self, JsError> {
        use rustledger_loader::{FileSystem, LoadOptions, Loader, VirtualFileSystem, process};

        let file_map: HashMap<String, String> = serde_wasm_bindgen::from_value(files)
            .map_err(|e| JsError::new(&format!("Invalid files object: {e}")))?;

        if file_map.is_empty() {
            return Err(JsError::new("Files map cannot be empty"));
        }

        let vfs = VirtualFileSystem::from_files(file_map);

        if !vfs.exists(Path::new(entry_point)) {
            return Err(JsError::new(&format!(
                "Entry point '{entry_point}' not found in files map"
            )));
        }

        let mut loader = Loader::new().with_filesystem(Box::new(vfs));

        let load_result = match loader.load(Path::new(entry_point)) {
            Ok(result) => result,
            Err(e) => {
                return Ok(Self {
                    directives: Vec::new(),
                    options: LedgerOptions::default(),
                    errors: vec![Error::new(format!("Load error: {e}"))],
                    editor_cache: editor::EditorCache::from_directives(&[]),
                });
            }
        };

        let options = LedgerOptions {
            title: load_result.options.title.clone(),
            operating_currencies: load_result.options.operating_currency.clone(),
        };

        let load_options = LoadOptions {
            validate: true,
            ..Default::default()
        };

        match process(load_result, &load_options) {
            Ok(ledger) => {
                let directives: Vec<Directive> =
                    ledger.directives.into_iter().map(|s| s.value).collect();
                let mut errors: Vec<Error> = ledger.errors.into_iter().map(Error::from).collect();
                // Include option warnings (E7001–E7006) so WASM consumers
                // see the same diagnostics as `rledger check` and the LSP.
                for w in &ledger.options.warnings {
                    errors.push(Error::new(format!("[{}] {}", w.code, w.message)));
                }
                let editor_cache = editor::EditorCache::from_directives(&directives);

                Ok(Self {
                    directives,
                    options,
                    errors,
                    editor_cache,
                })
            }
            Err(e) => Ok(Self {
                directives: Vec::new(),
                options,
                errors: vec![Error::new(format!("Processing error: {e}"))],
                editor_cache: editor::EditorCache::from_directives(&[]),
            }),
        }
    }

    /// Check if the ledger is valid (no errors).
    #[wasm_bindgen(js_name = "isValid")]
    pub fn is_valid(&self) -> bool {
        self.errors.is_empty()
    }

    /// Get all errors.
    #[wasm_bindgen(js_name = "getErrors")]
    pub fn get_errors(&self) -> Result<JsValue, JsError> {
        to_js(&self.errors)
    }

    /// Get the parsed directives.
    #[wasm_bindgen(js_name = "getDirectives")]
    pub fn get_directives(&self) -> Result<JsValue, JsError> {
        let directives: Vec<_> = self.directives.iter().map(directive_to_json).collect();
        to_js(&directives)
    }

    /// Get the ledger options.
    #[wasm_bindgen(js_name = "getOptions")]
    pub fn get_options(&self) -> Result<JsValue, JsError> {
        to_js(&self.options)
    }

    /// Get the number of directives.
    #[wasm_bindgen(js_name = "directiveCount")]
    pub fn directive_count(&self) -> usize {
        self.directives.len()
    }

    /// Run a BQL query on this ledger.
    #[wasm_bindgen]
    pub fn query(&self, query_str: &str) -> Result<JsValue, JsError> {
        execute_query(&self.directives, query_str)
    }

    /// Get account balances (shorthand for query("BALANCES")).
    #[wasm_bindgen]
    pub fn balances(&self) -> Result<JsValue, JsError> {
        self.query("BALANCES")
    }

    /// Expand pad directives.
    #[wasm_bindgen(js_name = "expandPads")]
    pub fn expand_pads(&self) -> Result<JsValue, JsError> {
        execute_expand_pads(&self.directives)
    }

    /// Run a native plugin on this ledger.
    #[cfg(feature = "plugins")]
    #[wasm_bindgen(js_name = "runPlugin")]
    pub fn run_plugin(&self, plugin_name: &str) -> Result<JsValue, JsError> {
        execute_plugin(&self.directives, plugin_name)
    }

    /// Get completions for a source string using cross-file data.
    ///
    /// Pass the source text of the file currently being edited.
    /// Completions use accounts, currencies, and payees from all loaded files.
    #[wasm_bindgen(js_name = "getCompletions")]
    pub fn get_completions(
        &self,
        source: &str,
        line: u32,
        character: u32,
    ) -> Result<JsValue, JsError> {
        let result = editor::get_completions_cached(source, line, character, &self.editor_cache);
        to_js(&result)
    }

    // =========================================================================
    // Serialization / Caching
    // =========================================================================

    /// Serialize this ledger to a compact binary blob (rkyv).
    ///
    /// Store the bytes in OPFS or `IndexedDB` alongside a source fingerprint
    /// (see [`crate::hash_sources`]) and restore later with [`Ledger::from_cache`].
    #[wasm_bindgen]
    pub fn serialize(&self) -> Result<Vec<u8>, JsError> {
        let payload = cache::LedgerPayload {
            directives: self.directives.clone(),
            options: self.options.clone(),
            errors: self.errors.clone(),
        };
        cache::serialize_ledger(&payload).map_err(|e| JsError::new(&e))
    }

    /// Restore a `Ledger` from bytes produced by [`Ledger::serialize`].
    ///
    /// # Errors
    ///
    /// Returns an error if the bytes are invalid or were produced by a different
    /// library version.
    #[wasm_bindgen(js_name = "fromCache")]
    pub fn from_cache(bytes: &[u8]) -> Result<Self, JsError> {
        let mut payload = cache::deserialize_ledger(bytes).map_err(|e| JsError::new(&e))?;

        // Re-intern strings to deduplicate identical Arc<str> allocations.
        rustledger_loader::reintern_plain_directives(&mut payload.directives);

        let editor_cache = editor::EditorCache::from_directives(&payload.directives);

        Ok(Self {
            directives: payload.directives,
            options: payload.options,
            errors: payload.errors,
            editor_cache,
        })
    }
}