use super::super::{sanitize, Verdict};
use anyhow::{anyhow, Result};
fn compute_check(body: &str) -> u32 {
let mut intermediate: u32 = 10;
for c in body.chars() {
let d = c.to_digit(10).unwrap();
let mut sum = (intermediate + d) % 10;
if sum == 0 {
sum = 10;
}
intermediate = (sum * 2) % 11;
}
(11 - intermediate) % 10
}
pub fn verify_rs_vat(input: &str) -> Verdict {
let clean = match super::strip_vat_prefix(input, "RS") {
Ok(body) => body,
Err(v) => return v,
};
if clean.len() != 9 {
return Verdict::Invalid {
reason: format!("RS PIB: expected 9 digits, got {}", clean.len()),
};
}
if !clean.chars().all(|c| c.is_ascii_digit()) {
return Verdict::Invalid { reason: "non-digit input".into() };
}
let body = &clean[..8];
let check: u32 = clean.chars().nth(8).unwrap().to_digit(10).unwrap();
let expected = compute_check(body);
if expected == check {
Verdict::Valid {
formatted: format!("RS{}", clean),
detected: "Serbian VAT (PIB)".into(),
comment: String::new(),
}
} else {
Verdict::Invalid {
reason: format!("RS PIB check mismatch: expected {}, got {}", expected, check),
}
}
}
pub fn create_rs_vat(input: &str, _raw: bool) -> Result<String> {
let clean = sanitize(input, false);
if clean.len() != 8 {
return Err(anyhow!("RS PIB: expected 8 digits (body without check digit), got {}", clean.len()));
}
if !clean.chars().all(|c| c.is_ascii_digit()) {
return Err(anyhow!("non-digit input"));
}
let check = compute_check(&clean);
Ok(format!("RS{}{}", clean, check))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rs_vat_valid_101134702() {
match verify_rs_vat("101134702") {
Verdict::Valid { formatted, detected, .. } => {
assert_eq!(formatted, "RS101134702");
assert_eq!(detected, "Serbian VAT (PIB)");
}
v => panic!("{:?}", v),
}
}
#[test]
fn rs_vat_rejects_wrong_length() {
match verify_rs_vat("12345678") {
Verdict::Invalid { reason } => assert!(reason.contains("expected 9 digits")),
v => panic!("{:?}", v),
}
}
#[test]
fn rs_vat_round_trip() {
let body = "10113470";
let full = create_rs_vat(body, false).unwrap();
assert_eq!(full, "RS101134702");
match verify_rs_vat(&full) {
Verdict::Valid { .. } => {}
v => panic!("{:?}", v),
}
}
#[test]
fn rs_vat_rejects_bad_check() {
match verify_rs_vat("101134703") {
Verdict::Invalid { .. } => {}
v => panic!("{:?}", v),
}
}
}