uftp 0.2.0

UFTP 3.1, the USEF Flex Trading Protocol (LF Energy Shapeshifter), in Rust: all 26 messages, CS1 signing and a receive pipeline for DSOs and aggregators.
Documentation
//! The messages every role may send `[XSD UFTP-common.xsd]`.

use jiff::Timestamp;

use crate::error::Rejection;
use crate::msg::meta::{Metadata, Reply};
use crate::msg::{DecodeOptions, Message, Wire, wire_meta};
use crate::types::{AcceptedRejected, MessageUuid};
use crate::xml::attrs::{Attrs, children};
use crate::xml::reader::Element;
use crate::xml::writer::Writer;

/// A message that tests the connection `[XSD TestMessageType]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestMessage {
    /// The common metadata.
    pub meta: Metadata,
}

/// The answer to a TestMessage `[XSD TestMessageResponseType]`. Unlike the other
/// responses it has no `Result` (spec note SS-03).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestMessageResponse {
    meta: Metadata,
}

impl TestMessageResponse {
    /// The response metadata.
    #[must_use]
    pub fn meta(&self) -> &Metadata {
        &self.meta
    }
}

impl TestMessage {
    /// Answers this test message, copying the conversation id.
    #[must_use]
    pub fn reply(&self, message_id: MessageUuid, timestamp: Timestamp) -> TestMessageResponse {
        TestMessageResponse {
            meta: self.meta.reply(message_id, timestamp),
        }
    }
}

fn read_meta_only(el: &Element, name: &'static str) -> Result<Metadata, Rejection> {
    children(el, name, &[])?;
    let mut a = Attrs::new(el, name);
    let meta = Metadata::read(&mut a)?;
    a.finish()?;
    Ok(meta)
}

fn write_meta_only(w: &mut Writer, name: &'static str, meta: &Metadata) {
    w.start(name);
    meta.write(w);
    w.end();
}

impl Wire for TestMessage {
    const NAME: &'static str = "TestMessage";
    wire_meta!(meta);
    fn answer(&self, reply: Reply) -> Option<Message> {
        (reply.result == AcceptedRejected::Accepted)
            .then(|| self.reply(reply.message_id, reply.timestamp).into())
    }
    fn read(el: &Element, _: DecodeOptions) -> Result<Self, Rejection> {
        Ok(Self {
            meta: read_meta_only(el, Self::NAME)?,
        })
    }
    fn write(&self, w: &mut Writer) -> Result<(), Rejection> {
        write_meta_only(w, Self::NAME, &self.meta);
        Ok(())
    }
}

impl Wire for TestMessageResponse {
    const NAME: &'static str = "TestMessageResponse";
    const RESPONSE: bool = true;
    wire_meta!(meta);
    fn read(el: &Element, _: DecodeOptions) -> Result<Self, Rejection> {
        Ok(Self {
            meta: read_meta_only(el, Self::NAME)?,
        })
    }
    fn write(&self, w: &mut Writer) -> Result<(), Rejection> {
        write_meta_only(w, Self::NAME, &self.meta);
        Ok(())
    }
}