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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::*;
use xml::{
    writer::{EventWriter, XmlEvent},
    EmitterConfig,
};

#[derive(Debug)]
pub struct Dial<'a> {
    method: Method,
    action: Option<&'a str>,
    timeout: usize,
    number: &'a str,
    recording_callback: Option<&'a str>,
    record: Record,
}

impl<'a> Default for Dial<'a> {
    fn default() -> Self {
        Dial {
            number: "",
            method: Method::Post,
            recording_callback: None,
            record: Record::DoNotRecord,
            action: None,
            timeout: 30,
        }
    }
}

impl<'a> Dial<'a> {
    pub fn new(number: &'a str) -> Self {
        Dial {
            number,
            ..Dial::default()
        }
    }

    pub fn method(mut self, method: Method) -> Self {
        self.method = method;
        self
    }

    pub fn action(mut self, url: &'a str) -> Self {
        self.action = Some(url);
        self
    }

    pub fn record(mut self, status: Record) -> Self {
        self.record = status;
        self
    }

    pub fn recording_callback(mut self, url: &'a str) -> Self {
        self.recording_callback = Some(url);
        self
    }

    pub fn timeout(mut self, timeout: usize) -> Self {
        self.timeout = timeout;
        self
    }
}

#[derive(Debug)]
pub enum Record {
    DoNotRecord,
    RecordFromAnswer,
    RecordFromRinging,
}

impl Record {
    fn to_str(&self) -> &str {
        match *self {
            Record::DoNotRecord => "do-not-record",
            Record::RecordFromAnswer => "record-from-answer",
            Record::RecordFromRinging => "record-from-ringing",
        }
    }
}

impl<'a> Twiml for Dial<'a> {
    fn write<W: Write>(&self, w: &mut EventWriter<W>) -> TwimlResult<()> {
        let timeout = self.timeout.to_string();
        let el = XmlEvent::start_element("Dial")
            .attr("method", self.method.to_str())
            .attr("timeout", &timeout)
            .attr("record", self.record.to_str());

        // not sure how else to get around this particular move error
        match (self.action, self.recording_callback) {
            (None, None) => w.write(el)?,
            (Some(action), None) => w.write(el.attr("action", action))?,
            (None, Some(callback)) => w.write(el.attr("recordingStatusCallback", callback))?,
            (Some(action), Some(callback)) => w.write(
                el.attr("action", action)
                    .attr("recordingStatusCallback", callback),
            )?,
        }

        w.write(self.number)?;
        w.write(XmlEvent::end_element())?;
        Ok(())
    }

    fn build(&self) -> TwimlResult<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)?)
    }
}

impl<'a, T> From<T> for Dial<'a>
where
    T: Into<&'a str>,
{
    fn from(s: T) -> Self {
        Dial::new(s.into())
    }
}