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
use crate::{current_timestamp, ReplyRenderer};


#[derive(Debug, Eq, PartialEq, Clone)]
pub struct VoiceReply {
    pub source: String,
    pub target: String,
    pub time: i64,
    pub media_id: String,
}

#[allow(unused)]
impl VoiceReply {
    #[inline]
    pub fn new<S: Into<String>>(source: S, target: S, media_id: S) -> VoiceReply {
        VoiceReply {
            source: source.into(),
            target: target.into(),
            time: current_timestamp(),
            media_id: media_id.into(),
        }
    }
}

#[allow(unused)]
impl ReplyRenderer for VoiceReply {
    #[inline]
    fn render(&self) -> String {
        format!("<xml>\n\
            <ToUserName><![CDATA[{target}]]></ToUserName>\n\
            <FromUserName><![CDATA[{source}]]></FromUserName>\n\
            <CreateTime>{time}</CreateTime>\n\
            <MsgType><![CDATA[voice]]></MsgType>\n\
            <Voice>\n\
            <MediaId><![CDATA[{media_id}]]></MediaId>\n\
            </Voice>\n\
            </xml>",
            target=self.target,
            source=self.source,
            time=self.time,
            media_id=self.media_id
        )
    }
}

#[cfg(test)]
mod tests {
    use super::ReplyRenderer;
    use super::VoiceReply;

    #[test]
    fn test_render_voice_reply() {
        let reply = VoiceReply::new("test1", "test2", "test");
        let rendered = reply.render();
        assert!(rendered.contains("test1"));
        assert!(rendered.contains("test2"));
        assert!(rendered.contains("test"));
    }
}