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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/// Only those types in the enum Directives are direct members of the Ledger.
/// The rest are children of other elements.
use std::collections::HashMap;
use std::fmt;

use chrono::NaiveDate;
use pest::iterators::{Pair, Pairs};
use pyo3::pyclass;
use rust_decimal::Decimal;

use crate::grammar::Rule;

const BASE_DATE: &str = "0001-01-01";
pub const DATE_FMT: &str = "%Y-%m-%d";

type Ccy = String;
pub type Account = String;

pub type CcyBal = HashMap<Ccy, Decimal>;
pub type AccBal = HashMap<Account, CcyBal>;
pub type AccStatuses = HashMap<Account, (bool, Vec<Ccy>)>;

#[pyclass]
#[derive(Clone, Debug)]
pub struct Options {
    #[pyo3(get)]
    pub title: String,
    #[pyo3(get)]
    pub operating_currency: String,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            title: "".to_string(),
            operating_currency: "".to_string(),
        }
    }
}

impl Options {
    pub fn update_from_entry(&mut self, entry: Pair<Rule>) {
        let mut pairs = entry.clone().into_inner();
        let key = pairs.next().unwrap().as_str();
        let val = pairs.next().unwrap().as_str().to_string();
        match key {
            "title" => self.title = val,
            "operating_currency" => self.operating_currency = val,
            _ => panic!("Other options not handled yet"),
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct DebugLine {
    pub line: usize,
}

impl DebugLine {
    pub fn new(line: usize) -> Self {
        Self { line }
    }
}

impl PartialEq for DebugLine {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl fmt::Display for DebugLine {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "line:{line}", line = self.line)
    }
}

#[derive(Clone, Debug)]
pub struct Amount {
    pub number: Decimal,
    pub ccy: Ccy,
}

impl PartialEq for Amount {
    fn eq(&self, other: &Self) -> bool {
        // TODO get precision from context
        self.ccy == other.ccy && (self.number - other.number).abs() > Decimal::new(1, 3)
    }
}
impl Eq for Amount {}

impl fmt::Display for Amount {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{number} {ccy}", number = self.number, ccy = self.ccy,)
    }
}

impl Amount {
    pub fn new(number: Decimal, ccy: Ccy) -> Self {
        Self { number, ccy }
    }
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let mut number: String = pairs.next().unwrap().as_str().to_string();
        if number.contains(',') {
            number = number.replace(',', "");
        }
        let number: Decimal = match number.parse() {
            Ok(num) => num,
            Err(_) => {
                let (line, _) = entry.line_col();
                panic!("Un-parseable decimal at line:{line}");
            }
        };
        let ccy = pairs.next().unwrap().as_str().to_string();
        Self { number, ccy }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct ConfigCustom {
    pub date: NaiveDate,
    pub debug: DebugLine,
}

impl ConfigCustom {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        let date = NaiveDate::parse_from_str(BASE_DATE, DATE_FMT).unwrap();
        Self { date, debug }
    }
}

impl fmt::Display for ConfigCustom {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "-- ignore custom")
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Metadata {
    pub key: String,
    pub val: String,
    pub debug: DebugLine,
}

impl Metadata {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let key = pairs.next().unwrap().as_str().to_string();
        let val = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self { key, val, debug }
    }
}

impl fmt::Display for Metadata {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "  {key}:{val}", key = self.key, val = self.val,)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Commodity {
    pub date: NaiveDate,
    pub ccy: String,
    pub meta: Vec<Metadata>,
    pub debug: DebugLine,
}

impl Commodity {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let ccy = pairs.next().unwrap().as_str().to_string();
        let mut meta: Vec<Metadata> = Vec::new();
        for pair in pairs {
            if pair.as_rule() == Rule::metadata {
                let p = Metadata::from_entry(pair);
                meta.push(p)
            }
        }
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            ccy,
            meta,
            debug,
        }
    }
}

impl fmt::Display for Commodity {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut meta_string: String = String::new();
        let m_slice = &self.meta[..];
        for m in m_slice {
            let line: &str = &format!("\n{m}");
            meta_string.push_str(line);
        }
        write!(
            f,
            "{date} commodity {ccy}{meta}",
            date = self.date,
            ccy = self.ccy,
            meta = meta_string,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Open {
    pub date: NaiveDate,
    pub account: Account,
    pub ccys: Vec<Ccy>,
    pub meta: Vec<Metadata>,
    pub debug: DebugLine,
}

impl Open {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };

        let mut ccys: Vec<Ccy> = Vec::new();
        let mut meta: Vec<Metadata> = Vec::new();

        for pair in pairs {
            match pair.as_rule() {
                Rule::ccy => {
                    let c = pair.as_str().to_owned();
                    ccys.push(c);
                }
                Rule::metadata => {
                    let m = Metadata::from_entry(pair);
                    meta.push(m);
                }
                _ => (),
            }
        }

        Self {
            date,
            account,
            ccys,
            meta,
            debug,
        }
    }
}

impl fmt::Display for Open {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} {account}",
            date = self.date,
            account = self.account,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Close {
    pub date: NaiveDate,
    pub account: Account,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Close {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            account,
            debug,
        }
    }
}

impl fmt::Display for Close {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} {account}",
            date = self.date,
            account = self.account,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Balance {
    pub date: NaiveDate,
    pub account: Account,
    pub amount: Amount,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Balance {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account = pairs.next().unwrap().as_str().to_string();
        let amount_entry = pairs.next().unwrap();
        let amount = Amount::from_entry(amount_entry);
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            account,
            amount,
            debug,
        }
    }
}

impl fmt::Display for Balance {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} {account} {amount}",
            date = self.date,
            account = self.account,
            amount = self.amount,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Pad {
    pub date: NaiveDate,
    pub account_to: Account,
    pub account_from: Account,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Pad {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account_to = pairs.next().unwrap().as_str().to_string();
        let account_from = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            account_to,
            account_from,
            debug,
        }
    }
}

impl fmt::Display for Pad {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} {account_to} {account_from}",
            date = self.date,
            account_to = self.account_to,
            account_from = self.account_from,
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Price {
    pub date: NaiveDate,
    pub commodity: String,
    pub amount: Amount,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Price {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let commodity = pairs.next().unwrap().as_str().to_string();
        let amount_entry = pairs.next().unwrap();
        let amount = Amount::from_entry(amount_entry);
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            commodity,
            amount,
            debug,
        }
    }
}

impl fmt::Display for Price {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} {commodity} {amount}",
            date = self.date,
            commodity = self.commodity,
            amount = self.amount,
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Document {
    pub date: NaiveDate,
    pub account: Account,
    pub path: String,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Document {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account = pairs.next().unwrap().as_str().to_string();
        let path = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            account,
            path,
            debug,
        }
    }
}

impl fmt::Display for Document {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} document {account} {path}",
            date = self.date,
            account = self.account,
            path = self.path,
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Note {
    pub date: NaiveDate,
    pub account: Account,
    pub note: String,
    // TODO can also have Meta
    pub debug: DebugLine,
}

impl Note {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let account = pairs.next().unwrap().as_str().to_string();
        let note = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            account,
            note,
            debug,
        }
    }
}

impl fmt::Display for Note {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} note {account} {note}",
            date = self.date,
            account = self.account,
            note = self.note,
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Query {
    pub date: NaiveDate,
    pub name: String,
    pub query: String,
    pub debug: DebugLine,
}

impl Query {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let name = pairs.next().unwrap().as_str().to_string();
        let query = pairs.next().unwrap().as_str().to_string();
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            name,
            query,
            debug,
        }
    }
}

impl fmt::Display for Query {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{date} query {name} {query}",
            date = self.date,
            name = self.name,
            query = self.query,
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Posting {
    pub account: Account,
    pub amount: Option<Amount>,
    pub debug: Option<DebugLine>,
}

impl Posting {
    pub fn new(account: Account, number: Decimal, ccy: Ccy) -> Self {
        let amount = Some(Amount { number, ccy });
        let debug = Default::default();
        Self {
            account,
            amount,
            debug,
        }
    }
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let account = pairs.next().unwrap().as_str().to_string();
        let amount = if pairs.peek().is_some() {
            Some(Amount::from_entry(pairs.next().unwrap()))
        } else {
            None
        };
        let (line, _) = entry.line_col();
        let debug = Some(DebugLine { line });
        Self {
            account,
            amount,
            debug,
        }
    }
}

impl fmt::Display for Posting {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let amount_str = match &self.amount {
            Some(amount) => amount.to_string(),
            None => String::new(),
        };

        write!(
            f,
            "  {account} {amount}",
            account = self.account,
            amount = amount_str,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Transaction {
    pub date: NaiveDate,
    pub ty: String,
    pub payee: Option<String>,
    pub narration: String,
    pub tag: Option<String>,  // TODO can have multiple
    pub link: Option<String>, // TODO can have multiple
    pub postings: Vec<Posting>,
    pub meta: Vec<Metadata>,
    pub debug: DebugLine,
    // TODO add at_cost, at_price
}

fn get_payee_narration(pairs: &mut Pairs<Rule>) -> (Option<String>, String) {
    let first_val = pairs.next().unwrap().as_str().to_string();
    if let Some(pair) = pairs.peek() {
        if pair.as_rule() == Rule::narration {
            let narration = pairs.next().unwrap().as_str().to_string();
            return (Some(first_val), narration);
        }
    }
    (None, first_val)
}

impl Transaction {
    pub fn from_entry(entry: Pair<Rule>) -> Self {
        let mut pairs = entry.clone().into_inner();
        let date = pairs.next().unwrap().as_str();
        let date = NaiveDate::parse_from_str(date, DATE_FMT).unwrap();
        let ty = pairs.next().unwrap().as_str().to_string();
        let (payee, narration) = get_payee_narration(&mut pairs);
        let mut postings: Vec<Posting> = Vec::new();
        let mut meta: Vec<Metadata> = Vec::new();
        let mut link: Option<String> = None;
        let mut tag: Option<String> = None;
        for pair in pairs {
            match pair.as_rule() {
                Rule::posting => {
                    postings.push(Posting::from_entry(pair));
                }
                Rule::metadata => {
                    meta.push(Metadata::from_entry(pair));
                }
                Rule::link => {
                    link = Some(entry.as_str().to_owned());
                }
                Rule::tag => {
                    tag = Some(entry.as_str().to_owned());
                }
                _ => {
                    let (line, _) = entry.line_col();
                    let debug = DebugLine::new(line);
                    unreachable!("Unexpected entry in Transaction, abort.\n{debug}");
                }
            }
        }
        let (line, _) = entry.line_col();
        let debug = DebugLine { line };
        Self {
            date,
            ty,
            payee,
            narration,
            tag,
            link,
            postings,
            meta,
            debug,
        }
    }
    pub fn from_pad(pad: Pad, amount: Amount) -> Self {
        let date = pad.date;
        let ty = String::from("pad");
        let payee = None;
        let narration = String::new();
        let debug: DebugLine = DebugLine::default();
        let link = None;
        let tag = None;
        let amount2 = Some(Amount {
            number: -amount.clone().number,
            ccy: amount.clone().ccy,
        });
        let amount = Some(amount);
        let p1 = Posting {
            account: pad.account_to,
            amount: amount.clone(),
            debug: Some(debug.clone()),
        };
        let p2 = Posting {
            account: pad.account_from,
            amount: amount2,
            debug: Some(debug.clone()),
        };
        let postings = vec![p1, p2];
        let meta: Vec<Metadata> = Vec::new();
        Self {
            date,
            ty,
            payee,
            narration,
            link,
            tag,
            postings,
            meta,
            debug: debug.clone(),
        }
    }
}

impl fmt::Display for Transaction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let payee_str = match &self.payee {
            Some(payee) => payee.as_str(),
            None => "",
        };

        let mut posting_string = String::new();
        let slice = &self.postings[..];
        for p in slice {
            let line: &str = &format!("\n{p}");
            posting_string.push_str(line);
        }

        let mut meta_string = String::new();
        let m_slice = &self.meta[..];
        for m in m_slice {
            let line: &str = &format!("\n{m}");
            meta_string.push_str(line);
        }

        write!(
            f,
            "{date} {ty} {payee} {narration}{meta}{postings}",
            date = self.date,
            ty = self.ty,
            payee = payee_str,
            narration = self.narration,
            meta = meta_string,
            postings = posting_string,
        )
    }
}

/// The "ledger" is made up of Directives
/// Most operations will be done by looping through a Vec of these
#[derive(Clone, Debug)]
pub enum Directive {
    ConfigCustom(ConfigCustom),
    Commodity(Commodity),
    Open(Open),
    Close(Close),
    Balance(Balance),
    Pad(Pad),
    Price(Price),
    Document(Document),
    Note(Note),
    Query(Query),
    Transaction(Transaction),
}

impl Directive {
    pub fn date(&self) -> &NaiveDate {
        match self {
            Directive::ConfigCustom(d) => &d.date,
            Directive::Commodity(d) => &d.date,
            Directive::Open(d) => &d.date,
            Directive::Close(d) => &d.date,
            Directive::Balance(d) => &d.date,
            Directive::Pad(d) => &d.date,
            Directive::Price(d) => &d.date,
            Directive::Document(d) => &d.date,
            Directive::Note(d) => &d.date,
            Directive::Query(d) => &d.date,
            Directive::Transaction(d) => &d.date,
        }
    }
    /// This follows beancount's ordering logic, that always evaluates
    /// opens -> balances -> the rest -> documents -> closes
    pub fn order(&self) -> i8 {
        match self {
            Directive::Open(_) => -2,
            Directive::Balance(_) => -1,
            Directive::ConfigCustom(_) => 0,
            Directive::Commodity(_) => 0,
            Directive::Pad(_) => 0,
            Directive::Price(_) => 0,
            Directive::Transaction(_) => 0,
            Directive::Document(_) => 1,
            Directive::Note(_) => 1,
            Directive::Query(_) => 1,
            Directive::Close(_) => 2,
        }
    }
}

impl fmt::Display for Directive {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Directive::ConfigCustom(d) => write!(f, "{d}"),
            Directive::Commodity(d) => write!(f, "{d}"),
            Directive::Open(d) => write!(f, "{d}"),
            Directive::Close(d) => write!(f, "{d}"),
            Directive::Balance(d) => write!(f, "{d}"),
            Directive::Pad(d) => write!(f, "{d}"),
            Directive::Price(d) => write!(f, "{d}"),
            Directive::Document(d) => write!(f, "{d}"),
            Directive::Note(d) => write!(f, "{d}"),
            Directive::Query(d) => write!(f, "{d}"),
            Directive::Transaction(d) => write!(f, "{d}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ledger::Ledger, loader};

    #[test]
    fn test_open() {
        let text = r#"2023-01-01 open Assets:Bank GBP"#;
        let entries = loader::load(&text);
        let Ledger {
            dirs,
            errs: _,
            opts: _,
        } = loader::consume(entries);
        let date = NaiveDate::parse_from_str("2023-01-01", DATE_FMT).unwrap();
        let a = &Open {
            date,
            account: String::from("Assets:Bank"),
            ccys: vec!["GBP".to_owned()],
            meta: Vec::new(),
            debug: DebugLine { line: 2 },
        };
        let got = &dirs[0];
        match got {
            Directive::Open(i) => {
                assert!(i == a);
            }
            _ => assert!(false, "Found wrong directive type"),
        }
    }

    #[test]
    #[should_panic]
    fn test_bad_amount() {
        let text = r#"
            2023-01-01 price FOO 1,.0.0 BAR
        "#;
        let mut entries = loader::load(&text);
        let entry = entries.next().unwrap();
        Price::from_entry(entry);
    }
}