use std::io::{BufRead, Cursor, Read};
use std::marker::PhantomData;
use super::Envelope;
use super::generated::{
NS_CIS_INFORMATION, NS_D2_PAYLOAD, NS_EXCHANGE_INFORMATION, NS_INFORMATION_MANAGEMENT,
NS_MESSAGE_CONTAINER, PUBLICATION_CHILDREN, PUBLICATION_TYPES, TABLES,
};
use super::record::Record;
use crate::error::{Error, ErrorKind, Warning, WarningKind, quote};
use crate::xml::{Limits, NS_FOREIGN, NS_NONE, NS_SOAP11, NS_SOAP12, Recorder, Xml};
const SOAP_WRAPPER_DEPTH: u32 = 4;
enum Level {
Soap(u32),
Container,
Route {
routes: Vec<&'static [u32]>,
depth: usize,
},
}
pub struct Stream<'a, T> {
xml: Xml<'a>,
stack: Vec<Level>,
inferred: Option<u32>,
done: bool,
pub(super) envelope: Envelope,
pub(super) soap: bool,
pub(super) payloads: usize,
exchange_information: Option<Box<super::exchange_information::ExchangeInformation>>,
record: PhantomData<fn() -> T>,
}
impl<T> std::fmt::Debug for Stream<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Stream")
.field("envelope", &self.envelope)
.field("done", &self.done)
.finish_non_exhaustive()
}
}
fn is_payload(xml: &Xml<'_>, key: u32, ns: u32) -> bool {
key >> 16 == ns && xml.local() == "payload"
}
fn infer_publication(input: &mut dyn BufRead, limits: &Limits) -> Option<u32> {
let mut xml = Xml::new(Box::new(input), &TABLES, limits);
let key = xml.next_child().ok()??;
if !is_payload(&xml, key, NS_D2_PAYLOAD) || xml.xsi_type().is_some() {
return None;
}
while let Some(child) = xml.next_child().ok()? {
if let Some((_, publication)) = PUBLICATION_CHILDREN.iter().find(|(c, _)| *c == child) {
return Some(*publication);
}
let mut owners = PUBLICATION_CHILDREN
.iter()
.filter(|(c, _)| c & 0xFFFF == child & 0xFFFF)
.map(|(_, p)| *p);
if let Some(first) = owners.next() {
return owners.all(|p| p == first).then_some(first);
}
xml.skip().ok()?;
}
None
}
impl<'a, T: Record> Stream<'a, T> {
pub(super) fn new(input: impl BufRead + 'a, limits: &Limits) -> Result<Self, Error> {
let mut input: Box<dyn BufRead + 'a> = Box::new(input);
let mut recorder = Recorder {
inner: &mut *input,
seen: Vec::new(),
};
let inferred = infer_publication(&mut recorder, limits);
let seen = recorder.seen;
let xml = Xml::new(Box::new(Cursor::new(seen).chain(input)), &TABLES, limits);
let mut stream = Stream {
xml,
stack: Vec::new(),
inferred,
done: false,
envelope: Envelope::Payload,
soap: false,
payloads: 0,
exchange_information: None,
record: PhantomData,
};
match stream.xml.next_child()? {
Some(key) => stream.open_root(key)?,
None => {
return Err(stream
.xml
.error(ErrorKind::Syntax, "a root element", "an end tag"));
}
}
Ok(stream)
}
pub fn take_warnings(&mut self) -> Vec<Warning> {
self.xml.take_warnings()
}
pub fn exchange_information(
&self,
) -> Option<&super::exchange_information::ExchangeInformation> {
self.exchange_information.as_deref()
}
pub(super) fn take_exchange_information(
&mut self,
) -> Option<Box<super::exchange_information::ExchangeInformation>> {
self.exchange_information.take()
}
pub fn suppressed_warnings(&self) -> usize {
self.xml.suppressed()
}
fn open_root(&mut self, key: u32) -> Result<(), Error> {
let ns = key >> 16;
let unqualified = is_payload(&self.xml, key, NS_NONE)
&& self
.xml
.xsi_type()
.is_some_and(|t| PUBLICATION_TYPES.contains(&t));
if is_payload(&self.xml, key, NS_D2_PAYLOAD) || unqualified {
if unqualified {
self.xml.warn(
WarningKind::WrongNamespace,
"\"payload\" in no namespace, expected in d2Payload".to_owned(),
);
}
self.envelope = Envelope::Payload;
let inferred = self.inferred;
return self.open_publication(inferred, true);
}
if ns == NS_MESSAGE_CONTAINER && self.xml.local() == "messageContainer" {
self.envelope = Envelope::MessageContainer;
self.stack.push(Level::Container);
return Ok(());
}
if matches!(ns, NS_SOAP11 | NS_SOAP12) && self.xml.local() == "Envelope" {
self.soap = true;
self.stack.push(Level::Soap(0));
return Ok(());
}
let named = PUBLICATION_TYPES
.iter()
.copied()
.filter(|t| TABLES.names[(t & 0xFFFF) as usize] == self.xml.local());
let kind = named.clone().find(|t| *t == key).or_else(|| {
let mut named = named;
let first = named.next()?;
named.next().is_none().then_some(first)
});
if let Some(kind) = kind.filter(|_| ns != NS_FOREIGN) {
self.envelope = Envelope::Publication;
return self.enter_publication(kind, true);
}
let hint = if self.xml.local() == "d2LogicalModel" {
" (DATEX II version 1 or 2, which this reader does not support)"
} else {
""
};
Err(self.xml.error(
ErrorKind::NotDatex2,
"a DATEX II v3 payload, messageContainer or publication",
format!("element {}{hint}", self.xml.describe_current(key)),
))
}
fn open_publication(&mut self, inferred: Option<u32>, at_root: bool) -> Result<(), Error> {
let kind = match self.xml.xsi_type() {
Some(kind) if PUBLICATION_TYPES.contains(&kind) => kind,
Some(_) => {
return Err(self.xml.error(
ErrorKind::UnknownPublication,
"a DATEX II v3 publication type in xsi:type",
quote(self.xml.xsi_raw()),
));
}
None => match inferred {
Some(kind) => {
self.xml.warn(
WarningKind::MissingXsiType,
format!(
"no xsi:type on the payload, read as {}",
TABLES.names[(kind & 0xFFFF) as usize]
),
);
kind
}
None => {
return Err(self.xml.error(
ErrorKind::UnknownPublication,
"an xsi:type attribute naming the publication type",
"none, and the content does not identify one",
));
}
},
};
self.enter_publication(kind, at_root)
}
fn enter_publication(&mut self, kind: u32, at_root: bool) -> Result<(), Error> {
self.payloads += 1;
let routes: Vec<&'static [u32]> = T::ROUTES
.iter()
.filter(|(publication, _)| *publication == kind)
.map(|(_, route)| *route)
.collect();
if routes.is_empty() {
let name = TABLES.names[(kind & 0xFFFF) as usize];
if at_root {
return Err(self.xml.error(
ErrorKind::UnexpectedPublication,
format!("a publication that holds {} records", T::NAME),
name,
));
}
self.xml.warn(
WarningKind::UnexpectedPublication,
format!("{name} holds no {} records", T::NAME),
);
return self.xml.skip();
}
self.stack.push(Level::Route { routes, depth: 0 });
self.inferred = Some(kind);
Ok(())
}
fn advance(&mut self) -> Result<Option<T>, Error> {
loop {
let Some(level) = self.stack.last() else {
return Ok(None);
};
if let Level::Route { routes, depth: 0 } = level {
if routes.iter().any(|r| r.is_empty()) {
self.stack.pop();
match T::read_record(&mut self.xml, self.inferred)? {
Some(record) => return Ok(Some(record)),
None => continue,
}
}
}
let Some(key) = self.xml.next_child()? else {
self.xml.close();
self.stack.pop();
continue;
};
let ns = key >> 16;
match self.stack.last().expect("checked above") {
Level::Soap(depth) => {
let depth = *depth;
if is_payload(&self.xml, key, NS_D2_PAYLOAD) {
self.open_publication(None, false)?;
} else if ns == NS_MESSAGE_CONTAINER && self.xml.local() == "messageContainer" {
self.envelope = Envelope::MessageContainer;
self.stack.push(Level::Container);
} else if depth < SOAP_WRAPPER_DEPTH {
self.stack.push(Level::Soap(depth + 1));
} else {
self.xml.skip_silently()?;
}
}
Level::Container => {
if is_payload(&self.xml, key, NS_MESSAGE_CONTAINER) {
self.open_publication(None, false)?;
} else if ns == NS_MESSAGE_CONTAINER
&& self.xml.local() == "exchangeInformation"
{
let info =
super::exchange_information::ExchangeInformation::read(&mut self.xml)?;
self.exchange_information = Some(Box::new(info));
} else if matches!(
ns,
NS_MESSAGE_CONTAINER
| NS_EXCHANGE_INFORMATION
| NS_INFORMATION_MANAGEMENT
| NS_CIS_INFORMATION
) {
self.xml.skip_silently()?;
} else {
self.xml.skip_unknown(key)?;
}
}
Level::Route { routes, depth } => {
let depth = *depth;
let mut next: Vec<&'static [u32]> = routes
.iter()
.copied()
.filter(|r| r.get(depth) == Some(&key))
.collect();
if next.is_empty() {
let expected: Vec<u32> = routes
.iter()
.filter_map(|r| r.get(depth).copied())
.collect();
if let Some(found) = self.xml.fallback(key, &expected) {
next = routes
.iter()
.copied()
.filter(|r| r.get(depth) == Some(&found))
.collect();
}
}
if next.iter().any(|r| r.len() == depth + 1) {
if let Some(record) = T::read_record(&mut self.xml, None)? {
return Ok(Some(record));
}
} else if next.is_empty() {
self.xml.skip_silently()?;
} else {
self.stack.push(Level::Route {
routes: next,
depth: depth + 1,
});
}
}
}
}
}
}
impl<T: Record> Iterator for Stream<'_, T> {
type Item = Result<T, Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
match self.advance() {
Ok(Some(record)) => Some(Ok(record)),
Ok(None) => {
self.done = true;
None
}
Err(error) => {
self.done = true;
Some(Err(error))
}
}
}
}
impl<T: Record> std::iter::FusedIterator for Stream<'_, T> {}