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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

use crate::error::ApiError;

use stream::packet::{
	self, Flags, Packet,
	PacketHeader, BodyBytes, BodyBytesMut, PacketError
};
pub use stream::packet::{PlainBytes, PacketBytes};
#[cfg(feature = "encrypted")]
pub use stream::packet::EncryptedBytes;

use std::fmt::Debug;
use std::hash::Hash;

use bytes::{Bytes, BytesMut, BytesRead, BytesWrite};

/// the number zero is not allowed to be used
pub trait Action: Debug + Copy + Eq + Hash {
	/// returns an Action that will never be returned
	fn empty() -> Self;
	/// should try to convert an u16 to the action
	/// 
	/// 0 will never be passed as num
	fn from_u16(num: u16) -> Option<Self>;
	fn as_u16(&self) -> u16;

	fn max_body_size(_header: &Header<Self>) -> Option<u32> {
		None
	}
}


#[derive(Debug)]
pub struct Header<A> {
	body_len: u32,
	flags: Flags,
	success: bool,
	id: u32,
	action: A
}

impl<A> Header<A>
where A: Action {

	pub fn empty() -> Self {
		Self {
			body_len: 0,
			flags: Flags::empty(),
			success: true,
			id: 0,
			action: A::empty()
		}
	}

	pub fn to_bytes(&self, mut bytes: BytesMut) {
		bytes.write_u32(self.body_len);
		bytes.write_u8(self.flags.as_u8());
		bytes.write_u8(self.success as u8);
		bytes.write_u32(self.id);
		bytes.write_u16(self.action.as_u16());
	}

	pub fn set_action(&mut self, action: A) {
		self.action = action;
	}

}

impl<A> PacketHeader for Header<A>
where A: Action {
	fn len() -> usize {
		4 + 1 + 1 + 4 + 2
	}

	fn from_bytes(mut bytes: Bytes) -> packet::Result<Self> {
		let me = Self {
			body_len: bytes.read_u32(),
			flags: Flags::from_u8(bytes.read_u8())?,
			success: bytes.read_u8() == 1,
			id: bytes.read_u32(),
			action: {
				let action_num = bytes.read_u16();
				// if the action is empty create an empty action
				// instead of rellying on the Action implementation
				if action_num == 0 {
					A::empty()
				} else {
					A::from_u16(action_num)
						.ok_or_else(|| PacketError::Header("Action unknown".into()))?
				}
			}
		};

		if let Some(max) = A::max_body_size(&me) {
			if me.body_len > max {
				return Err(PacketError::Body("body to big".into()))
			}
		}

		Ok(me)
	}

	fn body_len(&self) -> usize {
		self.body_len as usize
	}

	fn flags(&self) -> &Flags {
		&self.flags
	}

	fn set_flags(&mut self, flags: Flags) {
		self.flags = flags;
	}

	fn id(&self) -> u32 {
		self.id
	}

	fn set_id(&mut self, id: u32) {
		self.id = id;
	}
}

#[derive(Debug)]
pub struct Message<A, B> {
	header: Header<A>,
	bytes: B
}

impl<A, B> Message<A, B>
where
	A: Action,
	B: PacketBytes
{
	pub fn new() -> Self {
		Self::empty()
	}
}

impl<A, B> Message<A, B>
where B: PacketBytes {

	// #[cfg(feature = "json")]
	// pub fn serialize<S: ?Sized>(value: &S) -> Result<Self, E>
	// where S: serde::Serialize {
	// 	let mut me = Self::new();
	// 	me.bytes.body_mut().serialize(value)?;
	// 	Ok(me)
	// }

	// #[cfg(feature = "json")]
	// pub fn deserialize<D>(&self) -> crate::Result<D>
	// where D: serde::de::DeserializeOwned {
	// 	self.bytes.body().deserialize()
	// 		.map_err(Into::into)
	// }

	pub fn set_success(&mut self, success: bool) {
		self.header.success = success;
	}

	pub fn is_success(&self) -> bool {
		self.header.success
	}

	pub fn body(&self) -> BodyBytes<'_> {
		self.bytes.body()
	}

	pub fn body_mut(&mut self) -> BodyBytesMut<'_> {
		self.bytes.body_mut()
	}
}

impl<A, B> Message<A, B> {
	pub fn action(&self) -> &A {
		&self.header.action
	}
}

impl<A, B> Packet<B> for Message<A, B>
where
	A: Action,
	B: PacketBytes
{
	type Header = Header<A>;

	fn header(&self) -> &Self::Header {
		&self.header
	}

	fn header_mut(&mut self) -> &mut Self::Header {
		&mut self.header
	}

	fn empty() -> Self {
		Self {
			header: Self::Header::empty(),
			bytes: B::new(Self::Header::len())
		}
	}

	fn from_bytes_and_header(
		bytes: B,
		header: Self::Header
	) -> packet::Result<Self> {
		Ok(Self { header, bytes })
	}

	fn into_bytes(mut self) -> B {
		let body_len = self.bytes.body().len();
		self.header.body_len = body_len as u32;
		self.header.to_bytes(self.bytes.header_mut());
		self.bytes
	}
}

/// Means SerializeDeserialize Message
pub trait SerdeMessage<A, B, E: ApiError>: Sized {

	/// You should never return a Message which has a status of !success
	/// 
	/// ## Note
	/// The action and the success flag/states will automatically be set
	fn into_message(self) -> Result<Message<A, B>, E>
	where B: PacketBytes;

	/// you will never receive a Message with a status of !success
	/// 
	/// ## Note
	/// The action will always match
	fn from_message(msg: Message<A, B>) -> Result<Self, E>
	where B: PacketBytes;

}

/// Helps to implement SerdeMessage with the default serde traits.
/// 
/// ```
/// # use fire_stream_api as stream_api;
/// use stream_api::derive_serde_message;
/// 
/// use serde::{Serialize, Deserialize};
/// 
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// pub struct Request {
/// 	name: String
/// }
/// 
/// derive_serde_message!(Request);
/// ```
#[macro_export]
macro_rules! derive_serde_message {
	($type:ty) => (
		impl<A, B, E> $crate::message::SerdeMessage<A, B, E> for $type
		where
			A: $crate::message::Action,
			E: $crate::error::ApiError
		{

			fn into_message(
				self
			) -> std::result::Result<$crate::message::Message<A, B>, E>
			where B: $crate::message::PacketBytes {
				let mut msg = $crate::message::Message::new();
				msg.body_mut()
					.serialize(&self)
					.map_err(|e| E::request(
						format!("malformed request: {}", e)
					))?;
				Ok(msg)
			}

			fn from_message(
				msg: $crate::message::Message<A, B>
			) -> std::result::Result<Self, E>
			where B: $crate::message::PacketBytes {
				msg.body().deserialize()
					.map_err(|e| E::response(
						format!("malformed response: {}", e)
					))
			}

		}
	)
}

derive_serde_message!(());

#[cfg(test)]
mod tests {
	use crate::derive_serde_message;

	use serde::{Serialize, Deserialize};

	#[derive(Debug, Clone, Serialize, Deserialize)]
	struct Request {
		name: String
	}

	derive_serde_message!(Request);


}