summaryrefslogblamecommitdiffstats
path: root/src/stanza/stream.rs
blob: 9a21373f5281da8e3ce38f511959f20882059d3b (plain) (tree)
























                                                            

 
                     
               




                                 
               
              

                 
               
                    
                 

                                

         
 

                                                                                                                





                              







                                 
         

     
use serde::Serialize;

use crate::JID;

pub static XMLNS: &str = "http://etherx.jabber.org/streams";
pub static XMLNS_CLIENT: &str = "jabber:client";

// MUST be qualified by stream namespace
#[derive(Serialize)]
pub struct Stream<'s> {
    #[serde(rename = "@from")]
    from: Option<&'s JID>,
    #[serde(rename = "@to")]
    to: Option<&'s JID>,
    #[serde(rename = "@id")]
    id: Option<&'s str>,
    #[serde(rename = "@version")]
    version: Option<&'s str>,
    // TODO: lang enum
    #[serde(rename = "@lang")]
    lang: Option<&'s str>,
    #[serde(rename = "@xmlns")]
    xmlns: &'s str,
    #[serde(rename = "@xmlns:stream")]
    xmlns_stream: &'s str,
}

impl<'s> Stream<'s> {
    pub fn new(
        from: Option<&'s JID>,
        to: Option<&'s JID>,
        id: Option<&'s str>,
        version: Option<&'s str>,
        lang: Option<&'s str>,
    ) -> Self {
        Self {
            from,
            to,
            id,
            version,
            lang,
            xmlns: XMLNS_CLIENT,
            xmlns_stream: XMLNS,
        }
    }

    /// For initial stream headers, the initiating entity SHOULD include the 'xml:lang' attribute.
    /// For privacy, it is better to not set `from` when sending a client stanza over an unencrypted connection.
    pub fn new_client(
        from: Option<&'s JID>,
        to: &'s JID,
        id: Option<&'s str>,
        lang: &'s str,
    ) -> Self {
        Self {
            from,
            to: Some(to),
            id,
            version: Some("1.0"),
            lang: Some(lang),
            xmlns: XMLNS_CLIENT,
            xmlns_stream: XMLNS,
        }
    }
}