about summary refs log tree commit diff
path: root/src/writer/outvec.rs
blob: eb049340dee151cdbc1e33e1ba5991c60254b9ac (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
use crate::common::Color;

#[derive(Debug, PartialEq)]
pub struct OutVec {
	data: Vec<u8>
}

impl OutVec {
	pub fn new() -> Self {
		Self {
			data: vec![]
		}
	}

	pub fn push_u8(&mut self, n: u8) -> &mut Self {
		self.data.push(n);
		self
	}

	pub fn push_u16(&mut self, n: u16) -> &mut Self {
		self.data.extend_from_slice(&n.to_le_bytes());
		self
	}

	pub fn push_u32(&mut self, n: u32) -> &mut Self {
		self.data.extend_from_slice(&n.to_le_bytes());
		self
	}

	pub fn push_u64(&mut self, n: u64) -> &mut Self {
		self.data.extend_from_slice(&n.to_le_bytes());
		self
	}

	pub fn push_slice(&mut self, slice: &[u8]) -> &mut Self {
		self.data.extend_from_slice(slice);
		self
	}

	pub fn push_color(&mut self, color: &Color) -> &mut Self {
		self.data.extend_from_slice(&[color.r, color.g, color.b]);
		self
	}

	pub fn push_colors(&mut self, colors: &[Color]) -> &mut Self {
		for color in colors {
			self.push_color(color);
		}
		self
	}

	pub fn vec(self) -> Vec<u8> {
		self.data
	}
}

impl From<Vec<u8>> for OutVec {
	fn from(v: Vec<u8>) -> Self {
		let mut outvec = Self::new();
		outvec.push_slice(&v);

		outvec
	}
}