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
use crate::*;
use xml::{
    writer::{EventWriter, XmlEvent},
    EmitterConfig,
};

#[derive(Debug)]
pub struct Say<'a> {
    voice: Voice,
    count: usize,
    language: &'a str,
    body: &'a str,
}

#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum Voice {
    man,
    woman,
    alice,
}

impl<'a> Say<'a> {
    pub fn new(body: &'a str) -> Self {
        Say {
            body,
            voice: Voice::man,
            count: 1,
            language: "en",
        }
    }

    pub fn lang(mut self, language: &'a str) -> Say<'a> {
        self.language = language;
        self
    }

    pub fn voice(mut self, voice: Voice) -> Say<'a> {
        self.voice = voice;
        self
    }

    pub fn say_count(mut self, count: usize) -> Say<'a> {
        self.count = count;
        self
    }
}

impl<'a> Twiml for Say<'a> {
    fn write<W: Write>(&self, w: &mut EventWriter<W>) -> TwimlResult<()> {
        // Create a buffer and serialize our nodes into it
        w.write(
            XmlEvent::start_element("Say")
                .attr("voice", self.voice.to_str())
                .attr("language", self.language)
                .attr("loop", &self.count.to_string()),
        )?;
        w.write(self.body)?;
        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 Voice {
    pub fn to_str(&self) -> &str {
        match *self {
            Voice::man => "man",
            Voice::woman => "woman",
            Voice::alice => "alice",
        }
    }
}

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