about summary refs log tree commit diff
path: root/src/writer/outvec.rs
diff options
context:
space:
mode:
authorGenny <gen@nyble.dev>2020-10-06 20:05:35 -0500
committerGenny <gen@nyble.dev>2020-10-06 20:05:35 -0500
commit4dfa8fa22bc70c96adc902ded4b8795cab061627 (patch)
treef91c81926cbf339016f331c59b4390a63a129869 /src/writer/outvec.rs
downloadgifed-4dfa8fa22bc70c96adc902ded4b8795cab061627.tar.gz
gifed-4dfa8fa22bc70c96adc902ded4b8795cab061627.zip
Initial commit
Messy code, but it bloody works! Well, the tests don't fail, which is
a great start!
Diffstat (limited to 'src/writer/outvec.rs')
-rw-r--r--src/writer/outvec.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/writer/outvec.rs b/src/writer/outvec.rs
new file mode 100644
index 0000000..eb04934
--- /dev/null
+++ b/src/writer/outvec.rs
@@ -0,0 +1,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
+	}
+}
\ No newline at end of file