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
mod gather;
mod message;
mod play;
mod record;
mod redirect;
mod say;
mod sms;

pub use self::gather::{Gather, Prompt};
pub use self::message::Message;
pub use self::play::{Digits, Play, Playable};
pub use self::record::{Record, Transcribe};
pub use self::redirect::Redirect;
pub use self::say::{Say, Voice};
pub use self::sms::Sms;

pub trait Action {
    fn as_twiml(&self) -> String;
}

pub struct Twiml {
    body: String,
}

impl Twiml {
    pub fn new() -> Twiml {
        Twiml {
            body: "".to_string(),
        }
    }

    pub fn add(&mut self, a: &dyn Action) -> &mut Twiml {
        let twiml = a.as_twiml();
        self.body.push_str((&twiml as &dyn AsRef<str>).as_ref());
        self
    }

    pub fn as_twiml(&self) -> String {
        let b: &str = self.body.as_ref();
        format!(
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Response>{}</Response>",
            b
        )
    }
}

fn format_xml_string(tag: &str, attributes: &[(&str, &str)], inner: &str) -> String {
    let attribute_string = match attributes.len() {
        0 => "".to_string(),
        _ => attributes
            .iter()
            .map(|t| format!("{}=\"{}\"", t.0, t.1))
            .fold("".to_string(), |mut acc, v| {
                acc.push_str(" ");
                acc.push_str(&v);
                acc
            }),
    };
    let attribute_str: &str = attribute_string.as_ref();
    format!("<{}{}>{}</{}>", tag, attribute_str, inner, tag)
}

pub enum Method {
    Get,
    Post,
}