samael-mdext 0.1.0

samael with unreleased fixes for SAML metadata extensions and XML escaping
use quick_xml::Writer;
use serde::Deserialize;
use std::io::{self, Write};

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Endpoint {
    #[serde(rename = "@Binding")]
    pub binding: String,
    #[serde(rename = "@Location")]
    pub location: String,
    #[serde(rename = "@ResponseLocation")]
    pub response_location: Option<String>,
}

impl Endpoint {
    pub fn write_xml<W: Write>(
        &self,
        writer: &mut Writer<W>,
        element_name: &str,
    ) -> io::Result<()> {
        writer
            .create_element(element_name)
            .with_attribute(("Binding", self.binding.as_str()))
            .with_attribute(("Location", self.location.as_str()))
            .with_attributes(
                self.response_location
                    .as_deref()
                    .map(|location| ("ResponseLocation", location)),
            )
            .write_empty()?;
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct IndexedEndpoint {
    #[serde(rename = "@Binding")]
    pub binding: String,
    #[serde(rename = "@Location")]
    pub location: String,
    #[serde(rename = "@ResponseLocation")]
    pub response_location: Option<String>,
    #[serde(rename = "@index")]
    pub index: usize,
    #[serde(rename = "@isDefault")]
    pub is_default: Option<bool>,
}

impl IndexedEndpoint {
    pub fn write_xml<W: Write>(
        &self,
        writer: &mut Writer<W>,
        element_name: &str,
    ) -> io::Result<()> {
        let index = self.index.to_string();
        let is_default = self.is_default.map(|is_default| is_default.to_string());

        writer
            .create_element(element_name)
            .with_attribute(("Binding", self.binding.as_str()))
            .with_attribute(("Location", self.location.as_str()))
            .with_attribute(("index", index.as_str()))
            .with_attributes(
                self.response_location
                    .as_deref()
                    .map(|location| ("ResponseLocation", location)),
            )
            .with_attributes(
                is_default
                    .as_deref()
                    .map(|is_default| ("isDefault", is_default)),
            )
            .write_empty()?;
        Ok(())
    }
}