Skip to content

Commit

Permalink
Rename feature serialize to serde
Browse files Browse the repository at this point in the history
[C-SERDE](https://rust-lang.github.io/api-guidelines/interoperability.html#c-serde)

However, crate `serde` is still compiled by cargo even if we compile lumi
without enabling feature `serde`. This is similar to
[#1286](rust-lang/cargo#1286). Let us wait
until `namespaced-features` is stable.
  • Loading branch information
Lencerf committed Apr 3, 2021
1 parent 755e35a commit 6cf0116
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ license = "MIT"

[dependencies]
logos = "0.12.0"
rust_decimal = {version = "1.10.3", default-features = false}
chrono = "0.4"
rust_decimal = {version = "1.10.3"}
chrono = {version = "0.4", features = ["serde"]}
num_cpus = "1.13.0"
clap = { version = "2", features = ["wrap_help", "suggestions"] }
serde = { version = "1.0", features = ["derive", "rc"], optional = true }

[features]
serialize = ["serde", "chrono/serde", "rust_decimal/serde"]
clap = {version = "2", features = ["wrap_help", "suggestions"]}
serde = {version = "1.0", features = ["derive", "rc"], optional = true}

[dev-dependencies]
criterion = "0.3"
Expand Down
32 changes: 16 additions & 16 deletions src/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::parse::Parser;
pub use chrono::NaiveDate as Date;
pub use rust_decimal::Decimal;
#[cfg(feature = "serialize")]
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::convert::From;
Expand All @@ -10,7 +10,7 @@ use std::ops::{Div, Mul};
use std::sync::Arc;

/// Representing a location, line number and column number, in a source file.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Location {
pub line: usize,
Expand Down Expand Up @@ -41,7 +41,7 @@ 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 = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Source {
pub file: SrcFile,
Expand All @@ -57,7 +57,7 @@ impl fmt::Display for Source {

/// Kinds of errors that `lumi` encountered during generating [`Ledger`] from
/// files input text.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorType {
/// IO error, e.g., the context of an input file cannot be read.
Expand All @@ -83,15 +83,15 @@ pub enum ErrorType {

/// The level of an error. Any information in the source file resulting an
/// [`ErrorLevel::Error`] are dropped.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorLevel {
Info,
Warning,
Error,
}
/// Contains the full information of an error.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Error {
pub msg: String,
Expand All @@ -113,7 +113,7 @@ impl fmt::Display for Error {
pub type Currency = String;

/// A [`Decimal`] number plus the currency.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Amount {
pub number: Decimal,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> Mul<Decimal> for &'a Amount {
}

/// The unit price (`@`) or total price (`@@`) of the amount in a posting.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub enum Price {
Unit(Amount),
Expand All @@ -167,7 +167,7 @@ impl fmt::Display for Price {

/// The cost basis information (unit cost and transaction date) used to identify
/// a position in the running balances.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct UnitCost {
/// The unit cost basis.
Expand All @@ -183,7 +183,7 @@ impl fmt::Display for UnitCost {
}

/// The flag of a [`Transaction`].
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TxnFlag {
/// transactions flagged by `?`.
Expand Down Expand Up @@ -211,7 +211,7 @@ impl fmt::Display for TxnFlag {
pub type Account = Arc<String>;

/// A posting like `Assets::Bank -100 JPY` inside a [`Transaction`].
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Posting {
pub account: Account,
Expand Down Expand Up @@ -247,7 +247,7 @@ impl fmt::Display for Posting {

/// Represents a transaction, or a `pad` directives, or a `balance` directive in
/// the source file.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Transaction {
pub date: Date,
Expand All @@ -262,7 +262,7 @@ pub struct Transaction {
}

/// Represents a `note` directive
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct AccountNote {
pub date: Date,
Expand All @@ -277,7 +277,7 @@ pub type AccountDoc = AccountNote;
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 = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct AccountInfo {
pub open: (Date, Source),
Expand All @@ -289,7 +289,7 @@ pub struct AccountInfo {
}

/// Represents an `event` directive.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct EventInfo {
pub date: Date,
Expand All @@ -312,7 +312,7 @@ pub type BalanceSheet = HashMap<Account, HashMap<Currency, HashMap<Option<UnitCo

/// Represents a valid ledger containing all valid accounts and balanced
/// transactions.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Ledger {
pub accounts: HashMap<Account, AccountInfo>,
Expand Down
14 changes: 7 additions & 7 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
ErrorType, EventInfo, Location, Meta, Price, Source, SrcFile, TxnFlag, UnitCost,
};

#[cfg(feature = "serialize")]
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet, VecDeque},
Expand All @@ -16,7 +16,7 @@ use std::{

/// Represents the cost basis written in the source file, which might be either
/// unit cost or total cost.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CostBasis {
Total(Amount),
Expand All @@ -40,7 +40,7 @@ impl CostBasis {
}

/// Represents the parsed cost basis information.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct CostLiteral {
pub date: Option<Date>,
Expand Down Expand Up @@ -79,7 +79,7 @@ impl fmt::Display for CostLiteral {

/// Represents the result of a parsed posting, which might miss amounts or
/// referred an invalid account.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct PostingDraft {
pub account: Account,
Expand All @@ -92,7 +92,7 @@ pub struct PostingDraft {

/// Represents a transaction, or a `pad` directive, or a `balance` direction
/// parsed from the source file, which needs further inspections.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct TxnDraft {
pub date: Date,
Expand All @@ -108,7 +108,7 @@ pub struct TxnDraft {

/// Represents the information of an account collected by the parser from the
/// source file, which needs further inspections.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
pub struct AccountInfoDraft {
pub open: Option<(Date, Source)>,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl AccountInfoDraft {

/// Contains the information collected by a parser from the source files,
/// which might include unbalanced transactions or other errors.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
pub struct LedgerDraft {
pub accounts: HashMap<Account, AccountInfoDraft>,
Expand Down

0 comments on commit 6cf0116

Please sign in to comment.