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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// License: see LICENSE file at root directory of `master` branch

//! # Notifications

use std::{
    collections::BTreeMap,
    convert::TryFrom,
    io::{Error, ErrorKind},
};

#[cfg(test)]
use std::collections::HashSet;

use binn_ir::value::Value;

use crate::db::Id;

const KEY_ID: i32 = 0;
const KEY_DATE_CREATED: i32 = 1;
const KEY_USER_ID: i32 = 2;
const KEY_TITLE: i32 = 3;
const KEY_CONTENT: i32 = 4;
const KEY_DATA: i32 = 5;
const KEY_READ: i32 = 6;

#[test]
fn test_keys() {
    let keys = &[KEY_ID, KEY_DATE_CREATED, KEY_USER_ID, KEY_TITLE, KEY_CONTENT, KEY_DATA, KEY_READ];
    assert_eq!(keys.len(), keys.iter().collect::<HashSet<_>>().len());
}

/// # Notification
#[derive(Debug)]
pub struct Notification {
    id: Option<Id>,
    date_created: Option<u64>,
    user_id: Option<Id>,
    title: Option<String>,
    content: Option<String>,
    data: Option<Vec<u8>>,
    read: bool,
}

impl Notification {

    /// # Makes new instance
    pub const fn new(
        id: Option<Id>, date_created: Option<u64>, user_id: Option<Id>, title: Option<String>, content: Option<String>, data: Option<Vec<u8>>,
        read: bool,
    ) -> Self {
        Self {
            id,
            date_created,
            user_id,
            title,
            content,
            data,
            read,
        }
    }

    /// # ID
    pub fn id(&self) -> Option<Id> {
        self.id
    }

    /// # Date created
    pub fn date_created(&self) -> Option<u64> {
        self.date_created
    }

    /// # User ID
    pub fn user_id(&self) -> Option<Id> {
        self.user_id
    }

    /// Title
    pub fn title(&self) -> Option<&str> {
        self.title.as_ref().map(|t| t.as_str())
    }

    /// # Content
    pub fn content(&self) -> Option<&str> {
        self.content.as_ref().map(|c| c.as_str())
    }

    /// # Data
    pub fn data(&self) -> Option<&[u8]> {
        self.data.as_ref().map(|d| d.as_slice())
    }

    /// # Read flag
    pub fn is_read(&self) -> bool {
        self.read
    }

}

impl From<Notification> for Value {

    fn from(notification: Notification) -> Self {
        let mut map = BTreeMap::new();

        if let Some(id) = notification.id {
            map.insert(KEY_ID, id.into());
        }
        if let Some(date_created) = notification.date_created {
            map.insert(KEY_DATE_CREATED, date_created.into());
        }
        if let Some(user_id) = notification.user_id {
            map.insert(KEY_USER_ID, user_id.into());
        }
        if let Some(title) = notification.title {
            map.insert(KEY_TITLE, title.into());
        }
        if let Some(content) = notification.content {
            map.insert(KEY_CONTENT, content.into());
        }
        if let Some(data) = notification.data {
            map.insert(KEY_DATA, data.into());
        }
        map.insert(KEY_READ, notification.read.into());

        map.into()
    }

}

impl TryFrom<Value> for Notification {

    type Error = Error;

    fn try_from(value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Map(mut map) => Ok(Self {
                id: match map.remove(&KEY_ID) {
                    Some(Value::I64(id)) => Some(id),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid ID: {:?}", other))),
                    None => None,
                },
                date_created: match map.remove(&KEY_DATE_CREATED) {
                    Some(Value::U64(date_created)) => Some(date_created),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid date created: {:?}", other))),
                    None => None,
                },
                user_id: match map.remove(&KEY_USER_ID) {
                    Some(Value::I64(user_id)) => Some(user_id),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid user ID: {:?}", other))),
                    None => None,
                },
                title: match map.remove(&KEY_TITLE) {
                    Some(Value::Text(title)) => Some(title),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid title: {:?}", other))),
                    None => None,
                },
                content: match map.remove(&KEY_CONTENT) {
                    Some(Value::Text(content)) => Some(content),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid content: {:?}", other))),
                    None => None,
                },
                data: match map.remove(&KEY_DATA) {
                    Some(Value::Blob(data)) => Some(data),
                    Some(other) => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid data: {:?}", other))),
                    None => None,
                },
                read: match map.remove(&KEY_READ).ok_or_else(|| Error::new(ErrorKind::Other, "Missing read flag"))? {
                    Value::True => true,
                    Value::False => false,
                    other => return Err(Error::new(ErrorKind::InvalidData, format!("Invalid read flag: {:?}", other))),
                },
            }),
            other => Err(Error::new(ErrorKind::Other, format!("Value is not a map: {:?}", other))),
        }
    }

}