use std::str::FromStr;
use jiff::civil::Date;
use rust_decimal::Decimal;
pub(super) struct Field<'a> {
pub(super) tag: &'a str,
pub(super) value: String,
}
pub(super) fn fields(text: &str) -> Vec<Field<'_>> {
let mut out: Vec<Field<'_>> = Vec::new();
for line in text.lines().map(|l| l.trim_end_matches('\r')) {
let opened = line
.strip_prefix(':')
.and_then(|after| after.split_once(':'));
match (opened, out.last_mut()) {
(Some((tag, first)), _) => out.push(Field {
tag,
value: first.to_owned(),
}),
_ if line.is_empty() || line == "-" => {}
(None, Some(field)) => {
field.value.push('\n');
field.value.push_str(line);
}
(None, None) => {}
}
}
out
}
pub(super) fn qualified(value: &str) -> Option<(&str, &str)> {
let inner = value.strip_prefix(':')?;
inner.split_once("//")
}
pub(super) fn amount(text: &str) -> Option<Decimal> {
let text = text.trim();
let (negative, digits) = match text.strip_prefix('N') {
Some(rest) => (true, rest),
None => (false, text),
};
let (whole, fraction) = digits.split_once(',').unwrap_or((digits, ""));
let literal = match (whole, fraction) {
("", "") => return None,
(whole, "") => whole.to_owned(),
("", fraction) => format!("0.{fraction}"),
(whole, fraction) => format!("{whole}.{fraction}"),
};
let value = Decimal::from_str(&literal).ok()?;
Some(if negative { -value } else { value })
}
fn starts_with_currency(text: &str) -> bool {
let bytes = text.as_bytes();
bytes.len() > 3 && bytes[..3].iter().all(u8::is_ascii_uppercase)
}
pub(super) fn currency_amount(text: &str) -> Option<(String, Decimal)> {
let text = text.trim();
let (negative, rest) = match text.strip_prefix('N') {
Some(rest) if starts_with_currency(rest) => (true, rest),
_ => (false, text),
};
if !starts_with_currency(rest) {
return None;
}
let (currency, number) = rest.split_at(3);
let value = amount(number)?;
Some((currency.to_owned(), if negative { -value } else { value }))
}
pub(super) fn date(text: &str) -> Option<Date> {
let day = text.get(..8)?;
Date::strptime("%Y%m%d", day).ok()
}
pub(super) fn message_text(data: &[u8]) -> String {
let decoded = encoding_rs::WINDOWS_1252.decode(data).0;
decoded.replace("@@", "\n")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum Price {
Percent(Decimal),
PerUnit(String, Decimal),
}
impl Price {
pub(super) fn parse(data: &str) -> Option<Self> {
let (kind, rest) = data.split_once('/')?;
match kind {
"PRCT" => amount(rest).map(Self::Percent),
"ACTU" => currency_amount(rest).map(|(c, a)| Self::PerUnit(c, a)),
_ => None,
}
}
pub(super) fn value(&self) -> Decimal {
match self {
Self::Percent(value) | Self::PerUnit(_, value) => *value,
}
}
pub(super) fn currency(&self) -> Option<&str> {
match self {
Self::Percent(_) => None,
Self::PerUnit(currency, _) => Some(currency),
}
}
pub(super) fn code(&self) -> &'static str {
match self {
Self::Percent(_) => "PRCT",
Self::PerUnit(..) => "ACTU",
}
}
}
pub(super) fn quantity(data: &str) -> Option<(Decimal, bool)> {
let (unit, number) = data.split_once('/')?;
Some((amount(number)?, unit == "FAMT"))
}
pub(super) fn depot_number(account: &str) -> String {
let number = account.rsplit('/').next().unwrap_or(account);
number.trim().to_owned()
}
#[derive(Default)]
pub(super) struct SecurityId {
pub(super) isin: Option<String>,
pub(super) wkn: Option<String>,
pub(super) name_lines: Vec<String>,
}
fn first_word(text: &str) -> (&str, &str) {
match text.split_once(' ') {
Some((word, rest)) => (word, rest.trim()),
None => (text, ""),
}
}
impl SecurityId {
pub(super) fn parse(value: &str) -> Self {
let mut id = Self::default();
for (number, line) in value.lines().enumerate() {
let mut text = line.trim();
if number == 0
&& let Some(after) = text.strip_prefix("ISIN ")
{
let (isin, rest) = first_word(after.trim_start());
id.isin = (!isin.is_empty()).then(|| isin.to_owned()).or(id.isin);
text = rest;
}
if let Some((_country, coded)) = text.strip_prefix('/').and_then(|t| t.split_once('/'))
{
let (code, rest) = first_word(coded.trim());
if id.wkn.is_none() && !code.is_empty() {
id.wkn = Some(code.to_owned());
}
text = rest;
}
if !text.is_empty() {
id.name_lines.push(text.to_owned());
}
}
id
}
pub(super) fn name(&self) -> String {
self.name_lines.join(" ")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn dec(text: &str) -> Decimal {
Decimal::from_str(text).unwrap()
}
#[test]
fn fields_join_continuation_lines() {
let text = "junk\r\n:16R:FIN\r\n:35B:ISIN XS0000000003\r\n/DE/A1B2C3\r\nTEST ANLEIHE\r\n\r\n:16S:FIN\r\n-";
let read: Vec<_> = fields(text).into_iter().map(|f| (f.tag, f.value)).collect();
assert_eq!(
read,
[
("16R", "FIN".to_owned()),
(
"35B",
"ISIN XS0000000003\n/DE/A1B2C3\nTEST ANLEIHE".to_owned()
),
("16S", "FIN".to_owned()),
]
);
}
#[test]
fn qualified_contents() {
assert_eq!(qualified(":HOLD//EUR1,"), Some(("HOLD", "EUR1,")));
assert_eq!(qualified("HOLD//EUR1,"), None);
assert_eq!(qualified(":HOLD/EUR1,"), None);
}
#[test]
fn numbers() {
assert_eq!(amount("100,"), Some(dec("100")));
assert_eq!(amount("100,").unwrap().to_string(), "100");
assert_eq!(amount(" 0,0125 "), Some(dec("0.0125")));
assert_eq!(amount(",5"), Some(dec("0.5")));
assert_eq!(amount("N7,25"), Some(dec("-7.25")));
for bad in ["", "N", ",", "1,2,3", "abc"] {
assert_eq!(amount(bad), None, "{bad:?}");
}
}
#[test]
fn currency_amounts() {
assert_eq!(
currency_amount("EUR12,5"),
Some(("EUR".into(), dec("12.5")))
);
assert_eq!(currency_amount("NUSD3,"), Some(("USD".into(), dec("-3"))));
assert_eq!(currency_amount("NOK100,"), Some(("NOK".into(), dec("100"))));
assert_eq!(currency_amount("EUR"), None);
assert_eq!(currency_amount("eur1,"), None);
assert_eq!(currency_amount("€€€"), None);
}
#[test]
fn dates() {
assert_eq!(date("20260131"), Some(Date::constant(2026, 1, 31)));
assert_eq!(date("20260131093000"), Some(Date::constant(2026, 1, 31)));
assert_eq!(date("2026013"), None);
assert_eq!(date("20261331"), None);
}
#[test]
fn prices_quantities_and_depots() {
assert_eq!(Price::parse("PRCT/98,5"), Some(Price::Percent(dec("98.5"))));
let per_unit = Price::parse("ACTU/EUR12,5").unwrap();
assert_eq!(
(per_unit.value(), per_unit.currency()),
(dec("12.5"), Some("EUR"))
);
assert_eq!(per_unit.code(), "ACTU");
assert_eq!(Price::parse("YIEL/3,1"), None);
assert_eq!(Price::parse("ACTU/12,5"), None);
assert_eq!(quantity("UNIT/40,"), Some((dec("40"), false)));
assert_eq!(quantity("FAMT/2000,"), Some((dec("2000"), true)));
assert_eq!(quantity("UNIT"), None);
assert_eq!(depot_number("12345678/0000004711 "), "0000004711");
assert_eq!(depot_number("4711"), "4711");
assert_eq!(
message_text(b":16R:FIN@@:16S:FIN\xe4"),
":16R:FIN\n:16S:FINä"
);
}
#[test]
fn security_identification() {
let id = SecurityId::parse("ISIN XS0000000003 /DE/A1B2C3 TEST\nANLEIHE 2030");
assert_eq!(id.isin.as_deref(), Some("XS0000000003"));
assert_eq!(id.wkn.as_deref(), Some("A1B2C3"));
assert_eq!(id.name(), "TEST ANLEIHE 2030");
let national_only = SecurityId::parse("/DE/A1B2C3\n/DE/ZZZZZZ\nTESTFONDS");
assert_eq!(national_only.isin, None);
assert_eq!(national_only.wkn.as_deref(), Some("A1B2C3"));
assert_eq!(national_only.name(), "TESTFONDS");
let empty_code = SecurityId::parse("/XX/\nOHNE KENNUNG");
assert_eq!(empty_code.wkn, None);
assert_eq!(empty_code.name_lines, ["OHNE KENNUNG"]);
}
}