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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use twiml::*;
use xml::{
    writer::{EventWriter, XmlEvent}, EmitterConfig,
};

#[derive(Debug)]
pub struct Gather<'a> {
    method: Method,
    action: Option<&'a str>,
    key: char,
    timeout: usize,
    body: GatherBody<'a>,
}

#[derive(Debug)]
enum GatherBody<'a> {
    Nil,
    Say(Say<'a>),
    Play(Play<'a>),
    Redirect(Redirect<'a>),
}

impl<'a> Default for Gather<'a> {
    fn default() -> Self {
        Gather {
            body: GatherBody::Nil,
            method: Method::Post,
            key: '#',
            action: None,
            timeout: 5,
        }
    }
}

impl<'a> Gather<'a> {
    pub fn say<S: Into<Say<'a>>>(mut self, say: S) -> Self {
        self.body = GatherBody::Say(say.into());
        self
    }
    pub fn play<P: Into<Play<'a>>>(mut self, play: P) -> Self {
        self.body = GatherBody::Play(play.into());
        self
    }
    pub fn redirect<R: Into<Redirect<'a>>>(mut self, redirect: R) -> Self {
        self.body = GatherBody::Redirect(redirect.into());
        self
    }
    pub fn method(mut self, method: Method) -> Self {
        self.method = method;
        self
    }
    pub fn finish_on_key(mut self, key: char) -> Self {
        self.key = key;
        self
    }
    pub fn timeout(mut self, timeout: usize) -> Self {
        self.timeout = timeout;
        self
    }
}

impl<'a> Twiml for Gather<'a> {
    fn write<W: Write>(&self, w: &mut EventWriter<W>) -> TwilioResult<()> {
        let timeout = self.timeout.to_string();
        let key = self.key.to_string();
        let el = XmlEvent::start_element("Gather")
            .attr("method", self.method.to_str())
            .attr("timeout", &timeout)
            .attr("finishOnKey", &key);
        if let Some(action) = self.action {
            w.write(el.attr("action", action))?;
        } else {
            w.write(el)?;
        }
        match self.body {
            GatherBody::Nil => {}
            GatherBody::Play(ref val) => val.write(w)?,
            GatherBody::Say(ref val) => val.write(w)?,
            GatherBody::Redirect(ref val) => val.write(w)?,
        }

        w.write(XmlEvent::end_element())?;
        Ok(())
    }
    fn build(&self) -> TwilioResult<String> {
        // Create a buffer and serialize our nodes into it
        let mut writer = Vec::with_capacity(30);
        {
            let mut w = EmitterConfig::new()
                .write_document_declaration(false)
                .create_writer(&mut writer);

            self.write(&mut w)?;
        }
        Ok(String::from_utf8(writer)?)
    }
}