You are reviewing uncommitted changes in the Rust repo at…
- User
You are reviewing uncommitted changes in the Rust repo at /Users/mseele/dev/sve/sve-backend against the documented coding standards and the Fowler smell baseline.
Diff command
git diff HEADFiles changed
src/logic/banking.rs— 134 insertions, 3 deletions.Current HEAD context
Latest commit:
e4b8b36 chore: release version 2.12.4.Full diff
src/logic/banking.rs | 137 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 3 deletions(-) Changes: src/logic/banking.rs @@ -1,5 +1,5 @@ -use chrono::Utc; +use chrono::{Datelike, Duration, NaiveDate, Utc, Weekday}; use iban::IbanLike; use num_traits::ToPrimitive; use quick_xml::Writer; @@ -27,6 +27,59 @@ pub(crate) fn validate_iban_str(raw: &str) -> Result<String, ValidationError> { +fn easter_sunday(year: i32) -> NaiveDate { + let a = year % 19; + let b = year / 100; + let c = year % 100; + let d = b / 4; + let e = b % 4; + let f = (b + 8) / 25; + let g = (b - f + 1) / 3; + let h = (19 * a + b - d - g + 15) % 30; + let i = c / 4; + let k = c % 4; + let l = (32 + 2 * e + 2 * i - h - k) % 7; + let m = (a + 11 * h + 22 * l) / 451; + let month = ((h + l - 7 * m + 114) / 31) as u32; + let day = (((h + l - 7 * m + 114) % 31) + 1) as u32; + NaiveDate::from_ymd_opt(year, month, day).expect("valid easter date") +} + +fn is_sepa_banking_day(date: NaiveDate) -> bool { + if matches!(date.weekday(), Weekday::Sat | Weekday::Sun) { + return false; + } + let (m, d) = (date.month(), date.day()); + if matches!((m, d), (1, 1) | (5, 1) | (12, 25) | (12, 26)) { + return false; + } + let easter = easter_sunday(date.year()); + if date == easter - Duration::days(2) || date == easter + Duration::days(1) { + return false; + } + true +} + +fn add_sepa_banking_days(from: NaiveDate, count: u32) -> NaiveDate { + let mut date = from; + let mut remaining = count; + while remaining > 0 { + date += Duration::days(1); + if is_sepa_banking_day(date) { + remaining -= 1; + } + } + date +} + +fn sepa_lead_days(seq_tp: &str) -> u32 { + match seq_tp { + "FRST" | "OOFF" => 5, + "RCUR" => 2, + _ => 2, + } +} + pub(crate) async fn lookup_bic(iban: &str) -> Result<String> { let parsed = iban .parse::<iban::Iban>() @@ -149,16 +202,18 @@ pub(crate) fn generate_sepa_xml( - write_element(&mut writer, "SeqTp", "RCUR")?; + let seq_tp = "RCUR"; + write_element(&mut writer, "SeqTp", seq_tp)?; writer.write_event(XmlEvent::Start(BytesStart::new("CtgyPurp")))?; write_element(&mut writer, "Cd", "OTHR")?; writer.write_event(XmlEvent::End(BytesEnd::new("CtgyPurp")))?; writer.write_event(XmlEvent::End(BytesEnd::new("PmtTpInf")))?; + let coll_dt = add_sepa_banking_days(Utc::now().date_naive(), sepa_lead_days(seq_tp)); write_element( &mut writer, "ReqdColltnDt", - &Utc::now().format("%Y-%m-%d").to_string(), + &coll_dt.format("%Y-%m-%d").to_string(), )?; writer.write_event(XmlEvent::Start(BytesStart::new("Cdtr")))?; @@ -308,6 +363,63 @@ mod tests { + #[test] + fn test_easter_sunday_known_values() { + assert_eq!( + easter_sunday(2024), + NaiveDate::from_ymd_opt(2024, 3, 31).unwrap() + ); + assert_eq!( + easter_sunday(2025), + NaiveDate::from_ymd_opt(2025, 4, 20).unwrap() + ); + assert_eq!( + easter_sunday(2026), + NaiveDate::from_ymd_opt(2026, 4, 5).unwrap() + ); + assert_eq!( + easter_sunday(2027), + NaiveDate::from_ymd_opt(2027, 3, 28).unwrap() + ); + } + + #[test] + fn test_is_sepa_banking_day() { + let ok = |y, m, d| is_sepa_banking_day(NaiveDate::from_ymd_opt(y, m, d).unwrap()); + assert!(ok(2026, 7, 31), "Fri is a banking day"); + assert!(!ok(2026, 7, 25), "Sat is not"); + assert!(!ok(2026, 7, 26), "Sun is not"); + assert!(!ok(2026, 1, 1), "New Year"); + assert!(!ok(2026, 5, 1), "Labour Day"); + assert!(!ok(2026, 12, 25), "Christmas Day"); + assert!(!ok(2026, 12, 26), "Boxing Day"); + assert!(!ok(2026, 4, 3), "Good Friday 2026"); + assert!(!ok(2026, 4, 6), "Easter Monday 2026"); + assert!(ok(2026, 4, 7), "Tue after Easter Monday is a banking day"); + } + + #[test] + fn test_add_sepa_banking_days_skips_weekend() { + let fri = NaiveDate::from_ymd_opt(2026, 7, 31).unwrap(); + let plus2 = add_sepa_banking_days(fri, 2); + assert_eq!(plus2, NaiveDate::from_ymd_opt(2026, 8, 4).unwrap()); + + let thursday_before_easter = NaiveDate::from_ymd_opt(2026, 4, 2).unwrap(); + let plus1 = add_sepa_banking_days(thursday_before_easter, 1); + assert_eq!(plus1, NaiveDate::from_ymd_opt(2026, 4, 7).unwrap()); + + let plus5 = add_sepa_banking_days(thursday_before_easter, 5); + assert_eq!(plus5, NaiveDate::from_ymd_opt(2026, 4, 13).unwrap()); + } + + #[test] + fn test_sepa_lead_days_matches_epc_core_rulebook() { + assert_eq!(sepa_lead_days("FRST"), 5); + assert_eq!(sepa_lead_days("OOFF"), 5); + assert_eq!(sepa_lead_days("RCUR"), 2); + assert_eq!(sepa_lead_days("UNKNOWN"), 2); + } + #[test] fn test_generate_sepa_xml_structure() { let event = Event::new( @@ -402,6 +514,25 @@ mod tests { + let re = regex::Regex::new(r"<ReqdColltnDt>(\d{4}-\d{2}-\d{2})</ReqdColltnDt>").unwrap(); + let coll_dt_str = re + .captures(&xml) + .expect("ReqdColltnDt element present") + .get(1) + .unwrap() + .as_str(); + let coll_dt = NaiveDate::parse_from_str(coll_dt_str, "%Y-%m-%d") + .expect("ReqdColltnDt is a valid ISO date"); + let today = Utc::now().date_naive(); + assert!( + is_sepa_banking_day(coll_dt), + "ReqdColltnDt must be a SEPA banking day, got {coll_dt}" + ); + assert!( + coll_dt >= add_sepa_banking_days(today, sepa_lead_days("RCUR")), + "ReqdColltnDt must be today + SEPA CORE RCUR lead time, got {coll_dt}" + ); + // Validate against the canonical pain.008.001.02 XSD // (bundled at src/assets/pain.008.001.02.xsd) using the uppsala crate. let schema_xml = include_str!("../assets/pain.008.001.02.xsd"); Assistant
Review:
src/logic/banking.rsDocumented-standard violations (hard)
pretty_assertionsnot used in new tests. AGENTS.md requires: "Inline#[cfg(test)]modules; usepretty_assertions." The new tests (test_easter_sunday_known_values,test_is_sepa_banking_day,test_add_sepa_banking_days_skips_weekend,test_sepa_lead_days_matches_epc_core_rulebook, and the new assertion intest_generate_sepa_xml_structure) all use plainassert_eq!/assert!.pretty_assertionsis already a dev dependency.
Baseline smells (judgement calls)
-
Primitive Obsession.
sepa_lead_days(seq_tp: &str)matches on raw strings for a SEPA domain concept (sequence type). A small enum would be more honest: