use std::net::IpAddr;
use xso::{AsXml, FromXml};
use crate::jingle_ice_udp::Type;
use crate::ns;
#[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
#[xml(namespace = ns::JINGLE_RAW_UDP, name = "transport")]
pub struct Transport {
#[xml(child(n = ..))]
pub candidates: Vec<Candidate>,
}
impl Transport {
pub fn new() -> Transport {
Transport::default()
}
pub fn add_candidate(mut self, candidate: Candidate) -> Self {
self.candidates.push(candidate);
self
}
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::JINGLE_RAW_UDP, name = "candidate")]
pub struct Candidate {
#[xml(attribute)]
pub component: u8,
#[xml(attribute)]
pub generation: u8,
#[xml(attribute)]
pub id: String,
#[xml(attribute)]
pub ip: IpAddr,
#[xml(attribute)]
pub port: u16,
#[xml(attribute(default, name = "type"))]
pub type_: Option<Type>,
}
#[cfg(test)]
mod tests {
use super::*;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Transport, 12);
assert_size!(Candidate, 36);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Transport, 24);
assert_size!(Candidate, 48);
}
#[test]
fn example_1() {
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:raw-udp:1'>
<candidate component='1'
generation='0'
id='a9j3mnbtu1'
ip='10.1.1.104'
port='13540'/>
</transport>"
.parse()
.unwrap();
let mut transport = Transport::try_from(elem).unwrap();
assert_eq!(transport.candidates.len(), 1);
let candidate = transport.candidates.pop().unwrap();
assert_eq!(candidate.component, 1);
assert_eq!(candidate.generation, 0);
assert_eq!(candidate.id, "a9j3mnbtu1");
assert_eq!(candidate.ip, "10.1.1.104".parse::<IpAddr>().unwrap());
assert_eq!(candidate.port, 13540u16);
assert!(candidate.type_.is_none());
}
}