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
use crate::parse::Parser;
pub(crate) use chrono::NaiveDate;
use getset::{CopyGetters, Getters};
use rust_decimal::Decimal;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::convert::From;
use std::fmt;
use std::ops::{Div, Mul};
use std::sync::Arc;

/// Representing a location, line number and column number, in a source file.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Location {
    pub line: usize,
    pub col: usize,
}

impl Location {
    pub fn advance(&self, width: usize) -> Self {
        Location {
            col: self.col + width,
            line: self.line,
        }
    }
}

impl From<(usize, usize)> for Location {
    fn from(tuple: (usize, usize)) -> Self {
        Location {
            line: tuple.0,
            col: tuple.1,
        }
    }
}

/// A string wrapped in [`Arc`](std::sync::Arc)
/// representing the source file path.
pub type SrcFile = Arc<String>;

/// Represents a range in a source file. This struct is used to track the origins
/// of any information in the generated [`Ledger`], as well as for locating errors.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Source {
    pub file: SrcFile,
    pub start: Location,
    pub end: Location,
}

impl fmt::Display for Source {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}:{}", self.file, self.start.line, self.start.col)
    }
}

/// Kinds of errors that `lumi` encountered during generating [`Ledger`] from
/// files input text.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorType {
    /// IO error, e.g., the context of an input file cannot be read.
    Io,
    /// Syntax error in the source file.
    Syntax,
    /// Indicates a transactions is not balanced.
    NotBalanced,
    /// A transaction missing too much information such that `lumi` cannot infer
    /// for the context.
    Incomplete,
    /// An unopened or already closed account is referred.
    Account,
    /// `lumi` cannot find a position in the running balance sheet that matching
    /// the cost basis provided in the posting.
    NoMatch,
    /// Multiple Positions are founded in the running balance sheet that matching
    /// the cost basis provided in the posting.
    Ambiguous,
    /// Duplicate information, such as two identical tags in a single transaction.
    Duplicate,
}

/// The level of an error. Any information in the source file resulting an
/// [`ErrorLevel::Error`] are dropped.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ErrorLevel {
    Info,
    Warning,
    Error,
}
/// Contains the full information of an error.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Error {
    pub msg: String,
    pub src: Source,
    pub r#type: ErrorType,
    pub level: ErrorLevel,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:?}: {}\n  {}:{}:{}",
            self.level, self.msg, self.src.file, self.src.start.line, self.src.start.col
        )
    }
}

pub type Currency = String;

/// A [`Decimal`] number plus the currency.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Amount {
    pub number: Decimal,
    pub currency: Currency,
}

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

impl<'a> Div<Decimal> for &'a Amount {
    type Output = Amount;

    fn div(self, rhs: Decimal) -> Self::Output {
        Amount {
            number: self.number / rhs,
            currency: self.currency.clone(),
        }
    }
}

impl<'a> Mul<Decimal> for &'a Amount {
    type Output = Amount;

    fn mul(self, rhs: Decimal) -> Self::Output {
        Amount {
            number: self.number * rhs,
            currency: self.currency.clone(),
        }
    }
}

/// The unit price (`@`) or total price (`@@`) of the amount in a posting.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Price {
    Unit(Amount),
    Total(Amount),
}

impl fmt::Display for Price {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Price::Unit(amount) => write!(f, "@ {}", amount),
            Price::Total(amount) => write!(f, "@@ {}", amount),
        }
    }
}

/// The cost basis information (unit cost and transaction date) used to identify
/// a position in the running balances.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnitCost {
    /// The unit cost basis.
    pub amount: Amount,
    /// The transaction date.
    pub date: NaiveDate,
}

impl fmt::Display for UnitCost {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{ {}, {} }}", self.amount, self.date)
    }
}

/// The flag of a [`Transaction`].
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TxnFlag {
    /// transactions flagged by `?`.
    Pending,
    /// transactions flagged by `txn` or `*`.
    Posted,
    /// `pad` directives.
    Pad,
    /// `balance` directives.
    Balance,
}

impl fmt::Display for TxnFlag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TxnFlag::Pending => write!(f, "!"),
            TxnFlag::Posted | TxnFlag::Pad => write!(f, "*"),
            TxnFlag::Balance => write!(f, "balance"),
        }
    }
}

/// A string wrapped in [`Arc`](std::sync::Arc)
/// representing the account name.
pub type Account = Arc<String>;

/// A posting like `Assets::Bank -100 JPY` inside a [`Transaction`].
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Posting {
    pub account: Account,
    pub amount: Amount,
    pub cost: Option<UnitCost>,
    pub price: Option<Price>,
    pub meta: Meta,
    pub src: Source,
}

impl fmt::Display for Posting {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let num_str = self.amount.to_string();
        let index = num_str.find(|c| c == ' ' || c == '.').unwrap();
        let width = f.width().unwrap_or(46) - 1;
        let account_width = std::cmp::max(self.account.len() + 1, width - index);
        write!(
            f,
            "{:width$}{}",
            self.account,
            num_str,
            width = account_width
        )?;
        if let Some(cost) = &self.cost {
            write!(f, " {}", cost)?;
        }
        if let Some(ref price) = self.price {
            write!(f, " {}", price)?;
        }
        Ok(())
    }
}

pub type Payee = String;
pub type Narration = String;
pub type Link = String;
pub type Tag = String;

/// Represents a transaction, or a `pad` directives, or a `balance` directive in
/// the source file.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Getters, CopyGetters)]
pub struct Transaction {
    /// Returns the transaction date.
    #[getset(get_copy = "pub")]
    pub(crate) date: NaiveDate,

    /// Returns the transaction flag.
    #[getset(get_copy = "pub")]
    pub(crate) flag: TxnFlag,

    /// Returns the payee.
    #[getset(get = "pub")]
    pub(crate) payee: Payee,

    /// Returns the narration.
    #[getset(get = "pub")]
    pub(crate) narration: Narration,

    /// Returns the links.
    #[getset(get = "pub")]
    pub(crate) links: Vec<Link>,

    /// Returns the tags.
    #[getset(get = "pub")]
    pub(crate) tags: Vec<Tag>,

    /// Returns the meta data associated with this transaction.
    #[getset(get = "pub")]
    pub(crate) meta: Meta,

    /// Returns the postings of this transaction.
    #[getset(get = "pub")]
    pub(crate) postings: Vec<Posting>,

    /// Returns the source of this transaction.
    #[getset(get = "pub")]
    pub(crate) src: Source,
}

/// Represents a `note` directive
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AccountNote {
    pub date: NaiveDate,
    pub val: String,
    pub src: Source,
}

/// Represents a `document` directive
pub type AccountDoc = AccountNote;

/// Represents the meta data attached to a commodity, a transaction, or a posting.
pub type Meta = HashMap<String, (String, Source)>;

/// Contains the open/close date of an account, as well as the notes and documents.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
pub struct AccountInfo {
    /// Returns the account open date and the source of the `open` directive.
    #[getset(get = "pub")]
    pub(crate) open: (NaiveDate, Source),

    /// Returns the account close date and the source of the `close` directive.
    #[getset(get = "pub")]
    pub(crate) close: Option<(NaiveDate, Source)>,

    /// Returns the allowed currencies of this account. If there are no limitations,
    /// an empty set is returned.
    #[getset(get = "pub")]
    pub(crate) currencies: HashSet<Currency>,

    /// Returns the account notes in `note` directives.
    #[getset(get = "pub")]
    pub(crate) notes: Vec<AccountNote>,

    /// Returns the account documents in `document` directives.
    #[getset(get = "pub")]
    pub(crate) docs: Vec<AccountDoc>,

    /// Returns the account meta data associated with the `open` directive.
    #[getset(get = "pub")]
    pub(crate) meta: Meta,
}

/// Represents an `event` directive.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EventInfo {
    pub date: NaiveDate,
    pub desc: String,
    pub src: Source,
}

impl From<(NaiveDate, String, Source)> for EventInfo {
    fn from(tuple: (NaiveDate, String, Source)) -> Self {
        EventInfo {
            date: tuple.0,
            desc: tuple.1,
            src: tuple.2,
        }
    }
}

/// Represents the final balances of all accounts.
pub type BalanceSheet = HashMap<Account, HashMap<Currency, HashMap<Option<UnitCost>, Decimal>>>;

/// Represents a valid ledger containing all valid accounts and balanced
/// transactions.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Getters)]
pub struct Ledger {
    /// Returns the information of accounts.
    #[getset(get = "pub")]
    pub(crate) accounts: HashMap<Account, AccountInfo>,
    /// Returns all the currencies defined by `commodity` directives.
    #[getset(get = "pub")]
    pub(crate) commodities: HashMap<Currency, (Meta, Source)>,
    /// Returns transactions, `pad` directives, and `balance` directives, sorted
    /// by date.
    #[getset(get = "pub")]
    pub(crate) txns: Vec<Transaction>,
    /// Returns the options as a hash map.
    #[getset(get = "pub")]
    pub(crate) options: HashMap<String, (String, Source)>,
    /// Returns the events.
    #[getset(get = "pub")]
    pub(crate) events: HashMap<String, Vec<EventInfo>>,
    /// Returns the final balances.
    #[getset(get = "pub")]
    pub(crate) balance_sheet: BalanceSheet,
}

impl Ledger {
    pub fn from_file(path: &str) -> (Self, Vec<Error>) {
        let (draft, mut errors) = Parser::parse(path);
        let (ledger, more_errors) = draft.into_ledger();
        errors.extend(more_errors);
        (ledger, errors)
    }
}

impl fmt::Display for Transaction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.flag {
            TxnFlag::Balance => write!(f, "{} {}", self.date, self.flag)?,
            _ => write!(
                f,
                "{} {} \"{}\" \"{}\"",
                self.date, self.flag, self.payee, self.narration
            )?,
        };
        for tag in &self.tags {
            write!(f, " {}", tag)?;
        }
        for link in &self.links {
            write!(f, " {}", link)?;
        }
        for (key, val) in self.meta.iter() {
            write!(f, "\n  {}: {}", key, val.0)?;
        }
        let width = f.width().unwrap_or(50);
        match self.flag {
            TxnFlag::Balance => {
                if self.postings.len() == 1 {
                    write!(f, " {:width$}", self.postings[0], width = width - 19)?;
                } else {
                    for posting in self.postings.iter() {
                        write!(f, "\n    {:width$}", posting, width = width - 4)?;
                    }
                }
            }
            _ => {
                for posting in self.postings.iter() {
                    write!(f, "\n    {:width$}", posting, width = width - 4)?;
                }
            }
        }
        Ok(())
    }
}