aboutsummaryrefslogtreecommitdiff
path: root/src/message.rs
blob: 0617c4de13e159b97577384124d2fd8b76d10e68 (plain)
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
extern crate rustc_serialize;

use self::rustc_serialize::{json, Decodable, Decoder};

#[derive(Debug)]
pub enum Push {
    Mirror {
        notification_id: i32,
        title: String,
        body: String,
        application_name: String,
        package_name: String,
        icon: String
    },
    Dismissal {
        notification_id: i32
    }
}

#[derive(Debug)]
pub enum Message {
    Nop,
    Push(Push)
}

impl Message {
    pub fn parse(message: &str) -> Option<Message> {
        let json = json::Json::from_str(message).unwrap();
        let mut decoder = json::Decoder::new(json);
        let result : Result<Message, _> = Decodable::decode(&mut decoder);
        result.ok()
    }
}

impl Decodable for Push {
    fn decode<D: Decoder>(d: &mut D) -> Result<Push, D::Error> {
        d.read_struct("root", 0, |d| {
            let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode));
            match type_.as_ref() {
                "mirror" => {
                    let id = try!(
                        d.read_struct_field("notification_id", 0, Decodable::decode));
                    let title = try!(
                        d.read_struct_field("title", 0, Decodable::decode));
                    let body = try!(
                        d.read_struct_field("body", 0, Decodable::decode));
                    let app_name = try!(
                        d.read_struct_field("application_name", 0, Decodable::decode));
                    let package_name = try!(
                        d.read_struct_field("package_name", 0, Decodable::decode));
                    let icon = try!(
                        d.read_struct_field("icon", 0, Decodable::decode)); 

                    Ok(Push::Mirror {
                        title: title,
                        body: body,
                        application_name: app_name,
                        package_name: package_name,
                        notification_id: id,
                        icon: icon
                    })
                }
                "dismissal" => {
                    let id = try!(
                        d.read_struct_field("notification_id", 0, Decodable::decode));

                    Ok(Push::Dismissal {
                        notification_id: id
                    })
                }
                _ => {
                    let err_msg = format!(
                        "Invalid value for push type: {} (expected 'mirror' or 'dismissal')",
                        type_);
                    Err(d.error(err_msg.as_ref()))
                }
            }
        })
    }
}

impl Decodable for Message {
    fn decode<D: Decoder>(d: &mut D) -> Result<Message, D::Error> {
        d.read_struct("root", 0, |d| {
            let type_ : String = try!(d.read_struct_field("type", 0, Decodable::decode));

            match type_.as_ref() {
                "nop" => Ok(Message::Nop),
                "push" => {
                    d.read_struct_field("push", 0, |d| {
                        Ok(Message::Push(try!(Decodable::decode(d))))
                    })
                },
                _ => {
                    let err_msg = format!(
                        "Invalid value for message type: {}",
                        type_);
                    Err(d.error(err_msg.as_ref()))
                }
            }
        })
    }
}