-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtxns.rs
167 lines (154 loc) · 4.41 KB
/
txns.rs
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
/*
* Tackler-NG 2024-2025
*
* SPDX-License-Identifier: Apache-2.0
*/
use itertools::Itertools;
use winnow::{seq, PResult, Parser};
use crate::model::{Transaction, Txns};
use crate::parser::parts::txn_header::parse_txn_header;
use crate::parser::parts::txn_postings::parse_txn_postings;
use crate::parser::{from_error, make_semantic_error, Stream};
use winnow::ascii::{line_ending, multispace0, space0};
use winnow::combinator::{cut_err, eof, opt, preceded, repeat, repeat_till};
use winnow::error::StrContext;
fn multispace0_line_ending<'s>(is: &mut Stream<'s>) -> PResult<&'s str> {
// space0 can't be multispace0 as it's greedy and eats away the last line ending
repeat(1.., (space0, line_ending))
.map(|()| ())
.parse_next(is)?;
Ok("")
}
fn parse_txn(is: &mut Stream<'_>) -> PResult<Transaction> {
let txn = seq!(
cut_err(parse_txn_header)
.context(StrContext::Label("Txn Header")),
cut_err(parse_txn_postings)
.context(StrContext::Label("Txn Postings")),
_: multispace0,
)
.context(StrContext::Label("Transaction"))
.parse_next(is)?;
if txn.1.iter().map(|p| &p.txn_commodity.name).unique().count() > 1 {
let msg = format!(
"Different commodities without value positions are not allowed inside single transaction.{}",
txn.0.uuid.map(|u| format!("\n txn uuid: {u}")).unwrap_or_default());
return Err(make_semantic_error(is, msg.as_str()));
}
match Transaction::from(txn.0, txn.1) {
Ok(txn) => Ok(txn),
Err(err) => Err(from_error(is, err.as_ref())),
}
}
pub(crate) fn parse_txns(input: &mut Stream<'_>) -> PResult<Txns> {
let txns: (Vec<Transaction>, &str) = preceded(
opt(multispace0_line_ending),
repeat_till(1.., parse_txn, eof),
)
.parse_next(input)?;
Ok(txns.0)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::kernel::Settings;
use indoc::indoc;
use tackler_rs::IndocUtils;
#[test]
fn test_parse_txns() {
#[rustfmt::skip]
let pok_txns = vec![
(indoc!(
"|2025-01-03
| e 1
| a -1
|"
).strip_margin(), 1usize),
(indoc!(
"|
|2025-01-03
| e 1
| a
|"
).strip_margin(), 1usize),
(indoc!(
"| \t \n\
|2025-01-03
| e 1
| a
|"
).strip_margin(), 1usize),
(indoc!(
"|\t \n\
| \t \t
|2025-01-03
| e 1
| a
|"
).strip_margin(), 1usize),
(indoc!(
"|2025-01-03
| e 1
| a
|
|"
).strip_margin(), 1usize),
(indoc!(
"|2025-01-03
| e 1
| a
|\t \n\
| \t \t
|"
).strip_margin(), 1usize),
(indoc!(
"|2025-01-03
| e 1
| a -1
|
|2025-01-03
| e 1
| a
|
|2025-01-03
| e 1
| a
|"
).strip_margin(), 3usize),
(indoc!(
"|2025-01-03
| e 1
| a -1
|\t \n\
| \t \t
|2025-01-03
| e 1
| a
|"
).strip_margin(), 2usize),
];
let mut count = 0;
for t in pok_txns {
let mut settings = Settings::default();
let mut is = Stream {
input: t.0.as_str(),
state: &mut settings,
};
let res = parse_txns(&mut is);
assert!(
res.is_ok(),
"\nPOK is error: Offending test vector item: {}\n",
count + 1
);
let txns = res.unwrap(/*:test:*/);
assert_eq!(
txns.len(),
t.1,
"\nWrong Txns count: Offending test vector item: {}",
count + 1
);
count += 1;
}
assert_eq!(count, 8);
}
}