1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::borrow::Cow;
use std::io::Write;
use xml::writer::{EventWriter, XmlEvent, Result};

use xml::attribute::Attribute;
use xml::namespace::Namespace;

pub fn write_text_element<W: Write>( w: &mut EventWriter<W>, name: &str, text: &str, attributes: &Vec<Attribute>) -> Result<()>
{
    write_start_element(w, name, attributes)?;

    w.write(XmlEvent::Characters {
        0: text
    })?;

    write_end_element(w, name)?;
    
    Ok(())
}

pub fn write_vec_element<W: Write, T: std::fmt::Display>( w: &mut EventWriter<W>, name: &str, vec: &Vec<T>, attributes: &Vec<Attribute>) -> Result<()>
{
    let text = vec.iter().map(|idx| idx.to_string()).collect::<Vec<_>>().join(" ");
    write_text_element(w, name, &text, attributes)?;

    Ok(())
}

pub fn write_start_element<W: Write>( w: &mut EventWriter<W>, name: &str, attributes: &Vec<Attribute>) -> Result<()>
{
    w.write(XmlEvent::StartElement {
        name: name.into(),
        attributes: Cow::Borrowed(attributes),
        namespace: Cow::Owned(Namespace::empty())
    })?;
    Ok(())
}

pub fn write_end_element<W: Write>( w: &mut EventWriter<W>, name: &str) -> Result<()>
{
    w.write(XmlEvent::EndElement {
        name: Some(name.into()),
    })?;
    Ok(())
}