aboutsummaryrefslogblamecommitdiffstats
path: root/src/writer.rs
blob: 21d5fe0a09b0ca44fa8760cadbbfcaaa0d62c893 (plain) (tree)
1
2
3
4
5
6
7
8
9



                              

            
                                                   
                 
                                                                     

  
                                       
                      
             
                     
                                                               

 

                                                                          

               

                                                                                

               

                                                            





























                                                                                                 
     





























                                                                                       
use std::collections::HashSet;

use futures::Sink;
use tokio::io::AsyncWrite;

use crate::{
    element::{Element, Name, NamespaceDeclaration},
    error::Error,
    xml::{self, composers::Composer, parsers_complete::Parser, ETag},
};

// pub struct Writer<W, C = Composer> {
pub struct Writer<W> {
    inner: W,
    depth: Vec<Name>,
    namespace_declarations: Vec<HashSet<NamespaceDeclaration>>,
}

impl<W: AsyncWrite + Unpin> Writer<W> {
    pub async fn write(&mut self, element: Element) -> Result<(), Error> {
        todo!()
    }

    pub async fn write_start(&mut self, element: Element) -> Result<(), Error> {
        todo!()
    }

    pub async fn write_end(&mut self) -> Result<(), Error> {
        let e_tag;
        if let Some(name) = &self.depth.pop() {
            let namespace_declarations_stack: Vec<_> =
                self.namespace_declarations.iter().flatten().collect();
            let namespace_declaration = namespace_declarations_stack
                .iter()
                .rfind(|namespace_declaration| namespace_declaration.namespace == name.namespace)
                // will always be in this vector
                .unwrap();
            let prefix = &namespace_declaration.prefix;
            if let Some(prefix) = &prefix {
                e_tag = xml::ETag {
                    name: xml::QName::PrefixedName(xml::PrefixedName {
                        prefix: xml::Prefix::parse_full(prefix)?,
                        local_part: xml::LocalPart::parse_full(&name.local_name)?,
                    }),
                };
            } else {
                e_tag = xml::ETag {
                    name: xml::QName::UnprefixedName(xml::UnprefixedName::parse_full(
                        &name.local_name,
                    )?),
                };
            }
            e_tag.write(&mut self.inner).await?;
            self.namespace_declarations.pop();
            Ok(())
        } else {
            return Err(Error::NotInElement("".to_string()));
        }
    }
}

impl<W: AsyncWrite, E: Into<Element>> Sink<E> for Writer<W> {
    type Error = Error;

    fn poll_ready(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn start_send(self: std::pin::Pin<&mut Self>, item: E) -> Result<(), Self::Error> {
        todo!()
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn poll_close(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        todo!()
    }
}