1use chrono;
2use reqwest;
3use std::error::Error;
4use std::fmt;
5
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
8pub enum SpaceEmailStyle {
9 Yellow,
10 Red,
11 Lime,
12 Cyan,
13 Blue,
14 White,
15 Pink,
16 Admin,
17}
18
19impl SpaceEmailStyle {
20 pub(crate) fn from_css(class: &str) -> SpaceEmailStyle {
21 match class {
22 "msg-red" => SpaceEmailStyle::Red,
23 "msg-lime" => SpaceEmailStyle::Lime,
24 "msg-cyan" => SpaceEmailStyle::Cyan,
25 "msg-blue" => SpaceEmailStyle::Blue,
26 "msg-white" => SpaceEmailStyle::White,
27 "msg-pink" => SpaceEmailStyle::Pink,
28 "admin" => SpaceEmailStyle::Admin,
29 _ => SpaceEmailStyle::Yellow
30 }
31 }
32
33 pub(crate) fn into_id(&self) -> Option<u32> {
34 match *self {
35 SpaceEmailStyle::Yellow => Some(0),
36 SpaceEmailStyle::Red => Some(1),
37 SpaceEmailStyle::Lime => Some(2),
38 SpaceEmailStyle::Cyan => Some(3),
39 SpaceEmailStyle::Blue => Some(4),
40 SpaceEmailStyle::White => Some(5),
41 SpaceEmailStyle::Pink => Some(6),
42 SpaceEmailStyle::Admin => None,
43 }
44 }
45}
46
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48#[derive(Clone, Debug)]
49pub struct EmailId { pub(crate) id: u32, pub(crate) style: SpaceEmailStyle }
50
51impl From<u32> for EmailId {
52 fn from(id: u32) -> EmailId {
53 EmailId { id, style: SpaceEmailStyle::Yellow }
54 }
55}
56
57impl Into<u32> for EmailId {
58 fn into(self) -> u32 { self.id }
59}
60
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
62#[derive(Clone, Eq, PartialEq, Hash, Debug)]
63pub struct SpaceEmailContents {
64 pub subject: String,
65 pub sender: String,
66 pub body: String,
67 pub style: SpaceEmailStyle,
68}
69
70#[derive(Eq, PartialEq, Hash, Debug)]
71pub struct SpaceEmail {
72 pub(crate) id: u32,
73 pub(crate) share_id: String,
74 pub(crate) timestamp: chrono::NaiveDateTime,
75 pub(crate) contents: SpaceEmailContents,
76}
77
78impl SpaceEmail {
79 pub fn id(&self) -> EmailId { EmailId { id: self.id, style: self.contents.style } }
80
81 pub fn timestamp(&self) -> chrono::NaiveDateTime { self.timestamp }
82
83 pub fn contents(&self) -> &SpaceEmailContents { &self.contents }
84
85 pub fn share_id(&self) -> &str { &self.share_id }
86
87 pub fn share_url(&self) -> String { format!("https://space.galaxybuster.net/shv.php?id={}", self.share_id) }
88}
89
90#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
91#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
92pub enum SpaceEmailRange {
93 Today,
94 Week,
95 Month,
96 All
97}
98
99impl SpaceEmailRange {
100 pub(crate) fn into_id(&self) -> u32 {
101 match *self {
102 SpaceEmailRange::Today => 0,
103 SpaceEmailRange::Week => 1,
104 SpaceEmailRange::Month => 2,
105 SpaceEmailRange::All => 3,
106 }
107 }
108}
109
110#[derive(Debug)]
111pub enum SpaceEmailError {
112 Network(reqwest::Error),
113 MalformedResponse(String),
114 InvalidParameter,
115 RequiresLogin,
116}
117
118impl From<reqwest::Error> for SpaceEmailError {
119 fn from(error: reqwest::Error) -> SpaceEmailError {
120 SpaceEmailError::Network(error)
121 }
122}
123
124impl fmt::Display for SpaceEmailError {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 match *self {
127 SpaceEmailError::Network(ref e) => e.fmt(f),
128 SpaceEmailError::MalformedResponse(ref s) => write!(f, "Recieved malformed response: {}", s),
129 SpaceEmailError::InvalidParameter => write!(f, "Invalid parameter."),
130 SpaceEmailError::RequiresLogin => write!(f, "Operation requires the client to be logged in to an account.")
131 }
132 }
133}
134
135impl Error for SpaceEmailError {
136 fn source(&self) -> Option<&(dyn Error + 'static)> {
137 match *self {
138 SpaceEmailError::Network(ref e) => Some(e),
139 _ => None,
140 }
141 }
142}