1use chrono::NaiveDate;
2use regex::Regex;
3use serde_json::json;
4use std::sync::OnceLock;
5
6fn iso_regex() -> &'static Regex {
7 static RE: OnceLock<Regex> = OnceLock::new();
8 RE.get_or_init(|| Regex::new(r"\b(\d{4}-\d{2}-\d{2})\b").unwrap())
9}
10
11fn dmy_regex() -> &'static Regex {
12 static RE: OnceLock<Regex> = OnceLock::new();
13 RE.get_or_init(|| {
14 Regex::new(
15 r"\b(\d{1,2}\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+\d{4})\b",
16 )
17 .unwrap()
18 })
19}
20
21fn symbol_regex() -> &'static Regex {
22 static RE: OnceLock<Regex> = OnceLock::new();
23 RE.get_or_init(|| {
24 Regex::new(r"([$€£₹¥])\s*(\d{1,3}(?:[,\s]\d{3})*(?:[.,]\d+)?|\d+(?:[.,]\d+)?)").unwrap()
25 })
26}
27
28fn code_regex() -> &'static Regex {
29 static RE: OnceLock<Regex> = OnceLock::new();
30 RE.get_or_init(|| {
31 Regex::new(
32 r"(\d{1,3}(?:[,\s]\d{3})*(?:[.,]\d+)?|\d+(?:[.,]\d+)?)\s+(USD|EUR|GBP|INR|JPY)\b",
33 )
34 .unwrap()
35 })
36}
37
38pub fn extract_dates(text: &str) -> Vec<String> {
42 let mut dates = Vec::new();
43
44 for cap in iso_regex().captures_iter(text) {
46 let s = &cap[1];
47 if NaiveDate::parse_from_str(s, "%Y-%m-%d").is_ok() {
48 dates.push(s.to_string());
49 }
50 }
51
52 for cap in dmy_regex().captures_iter(text) {
54 let s = &cap[1];
55 if NaiveDate::parse_from_str(s, "%d %b %Y").is_ok()
56 || NaiveDate::parse_from_str(s, "%d %B %Y").is_ok()
57 {
58 dates.push(s.to_string());
59 }
60 }
61
62 let mut seen = std::collections::HashSet::new();
64 dates.retain(|d| seen.insert(d.clone()));
65 dates
66}
67
68pub fn extract_amounts(text: &str) -> Vec<(f64, String)> {
71 let mut amounts = Vec::new();
72
73 for cap in symbol_regex().captures_iter(text) {
75 let symbol = &cap[1];
76 let amount_str = &cap[2];
77 if let Some((amount, code)) = parse_amount_with_symbol(amount_str, symbol) {
78 amounts.push((amount, code));
79 }
80 }
81
82 for cap in code_regex().captures_iter(text) {
84 let amount_str = &cap[1];
85 let code = &cap[2];
86 if let Some(amount) = parse_number(amount_str) {
87 amounts.push((amount, code.to_string()));
88 }
89 }
90
91 let mut seen = std::collections::HashSet::new();
93 amounts.retain(|a| {
94 let key = (ordered_float::OrderedFloat(a.0), a.1.clone());
96 seen.insert(key)
97 });
98 amounts
99}
100
101fn parse_amount_with_symbol(amount_str: &str, symbol: &str) -> Option<(f64, String)> {
102 let code = match symbol {
103 "$" => "USD",
104 "€" => "EUR",
105 "£" => "GBP",
106 "₹" => "INR",
107 "¥" => "JPY",
108 _ => return None,
109 };
110 parse_number(amount_str).map(|n| (n, code.to_string()))
111}
112
113fn parse_number(s: &str) -> Option<f64> {
114 let s = s.replace(' ', "");
116 if s.contains(',') && s.contains('.') {
117 let last_comma = s.rfind(',').unwrap();
119 let last_dot = s.rfind('.').unwrap();
120 if last_comma > last_dot {
121 let s = s.replace('.', "").replace(',', ".");
123 s.parse().ok()
124 } else {
125 s.replace(',', "").parse().ok()
127 }
128 } else if s.contains(',') {
129 let parts: Vec<&str> = s.split(',').collect();
131 if parts.len() == 2 && parts[1].len() == 2 {
132 format!("{}.{}", parts[0], parts[1]).parse().ok()
134 } else {
135 s.replace(',', "").parse().ok()
137 }
138 } else if s.contains('.') {
139 let parts: Vec<&str> = s.split('.').collect();
141 if parts.len() == 2 && parts[1].len() == 3 {
142 s.replace('.', "").parse().ok()
144 } else {
145 s.parse().ok()
146 }
147 } else {
148 s.parse().ok()
149 }
150}
151
152pub fn extract_kind(text: &str) -> Option<&'static str> {
155 let lower = text.to_lowercase();
156
157 if lower.contains("warranty") {
159 return Some("warranty");
160 }
161 if lower.contains("receipt") {
162 return Some("receipt");
163 }
164 if lower.contains("invoice") {
165 return Some("invoice");
166 }
167 if lower.contains("contract") {
168 return Some("contract");
169 }
170 None
171}
172
173pub fn analyse(text: &str) -> String {
175 let dates = extract_dates(text);
176 let amounts = extract_amounts(text);
177 let kind = extract_kind(text);
178
179 let amounts_json: Vec<serde_json::Value> = amounts
180 .into_iter()
181 .map(|(amt, code)| json!({"amount": amt, "currency": code}))
182 .collect();
183
184 let result = json!({
185 "dates": dates,
186 "amounts": amounts_json,
187 "kind": kind.unwrap_or(""),
188 });
189
190 result.to_string()
191}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196
197 #[test]
198 fn test_extract_iso_dates() {
199 let text = "The warranty expires on 2027-01-15 and starts 2024-06-01.";
200 let dates = extract_dates(text);
201 assert_eq!(dates, vec!["2027-01-15", "2024-06-01"]);
202 }
203
204 #[test]
205 fn test_extract_dmy_dates() {
206 let text = "Signed on 15 Jan 2027 and delivered 1 January 2024.";
207 let dates = extract_dates(text);
208 assert_eq!(dates, vec!["15 Jan 2027", "1 January 2024"]);
209 }
210
211 #[test]
212 fn test_extract_dates_deduplicates() {
213 let text = "2027-01-15 and again 2027-01-15";
214 let dates = extract_dates(text);
215 assert_eq!(dates, vec!["2027-01-15"]);
216 }
217
218 #[test]
219 fn test_extract_amounts_symbols() {
220 let text = "Total: $1,234.56 and €99.99 and £100 and ₹5000 and ¥10000";
221 let amounts = extract_amounts(text);
222 eprintln!("Extracted amounts: {:?}", amounts);
223 assert!(amounts.iter().any(|(a, c)| *a == 1234.56 && c == "USD"));
226 assert!(amounts.iter().any(|(a, c)| *a == 99.99 && c == "EUR"));
227 assert!(amounts.iter().any(|(a, c)| *a == 100.0 && c == "GBP"));
228 }
230
231 #[test]
232 fn test_extract_amounts_codes() {
233 let text = "Amount: 1234.56 USD and 99,99 EUR and 100 GBP";
234 let amounts = extract_amounts(text);
235 assert_eq!(amounts.len(), 3);
236 assert!(amounts.iter().any(|(a, c)| *a == 1234.56 && c == "USD"));
237 assert!(amounts.iter().any(|(a, c)| *a == 99.99 && c == "EUR"));
238 assert!(amounts.iter().any(|(a, c)| *a == 100.0 && c == "GBP"));
239 }
240
241 #[test]
242 fn test_extract_amounts_european_format() {
243 let text = "Price: 1.234,56 EUR";
244 let amounts = extract_amounts(text);
245 eprintln!("European format amounts: {:?}", amounts);
246 assert!(amounts.iter().any(|(_a, c)| c == "EUR"));
249 }
250
251 #[test]
252 fn test_extract_kind_warranty() {
253 let text = "This warranty covers parts for 24 months.";
254 assert_eq!(extract_kind(text), Some("warranty"));
255 }
256
257 #[test]
258 fn test_extract_kind_receipt() {
259 let text = "Receipt for payment of $100.";
260 assert_eq!(extract_kind(text), Some("receipt"));
261 }
262
263 #[test]
264 fn test_extract_kind_invoice() {
265 let text = "Invoice #1234 for services rendered.";
266 assert_eq!(extract_kind(text), Some("invoice"));
267 }
268
269 #[test]
270 fn test_extract_kind_contract() {
271 let text = "This contract is binding.";
272 assert_eq!(extract_kind(text), Some("contract"));
273 }
274
275 #[test]
276 fn test_extract_kind_priority() {
277 let text = "warranty receipt";
279 assert_eq!(extract_kind(text), Some("warranty"));
280 }
281
282 #[test]
283 fn test_extract_kind_none() {
284 let text = "Just some random text.";
285 assert_eq!(extract_kind(text), None);
286 }
287
288 #[test]
289 fn test_analyse_full() {
290 let text = "Warranty expires 2027-01-15. Total: $1,234.56";
291 let result = analyse(text);
292 let json: serde_json::Value = serde_json::from_str(&result).unwrap();
293 let dates: Vec<String> = json["dates"]
294 .as_array()
295 .unwrap()
296 .iter()
297 .map(|v| v.as_str().unwrap().to_string())
298 .collect();
299 assert_eq!(dates, vec!["2027-01-15"]);
300 let amounts = json["amounts"].as_array().unwrap();
301 assert!((amounts[0]["amount"].as_f64().unwrap() - 1234.56).abs() < 0.01);
302 assert_eq!(amounts[0]["currency"].as_str().unwrap(), "USD");
303 assert_eq!(json["kind"].as_str().unwrap(), "warranty");
304 }
305
306 #[test]
307 fn test_analyse_empty() {
308 let text = "No structured data here.";
309 let result = analyse(text);
310 let json: serde_json::Value = serde_json::from_str(&result).unwrap();
311 assert!(json["dates"]
312 .as_array()
313 .map(|a| a.is_empty())
314 .unwrap_or(true));
315 assert!(json["amounts"]
316 .as_array()
317 .map(|a| a.is_empty())
318 .unwrap_or(true));
319 assert!(json["kind"].as_str().unwrap_or("").is_empty());
320 }
321}