aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-03 13:41:42 +0000
committerAkshay <[email protected]>2021-03-03 13:41:42 +0000
commitf46b981d06128642fee7e81f83b4ff09bd5f1be8 (patch)
tree3414cd19a1eb179490221dc5cb622d76a34febba /src/lib.rs
parent7c0b4374c1c6f049a6fe68d07bed87e7c22431d4 (diff)
implement simple error handling
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs49
1 files changed, 33 insertions, 16 deletions
diff --git a/src/lib.rs b/src/lib.rs
index cd7923c..7429101 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,8 +1,13 @@
1#![allow(unreachable_patterns)] 1#![allow(unreachable_patterns)]
2#![allow(non_snake_case)] 2#![allow(non_snake_case)]
3 3
4pub mod decode; 4use std::io;
5pub mod encode; 5
6mod decode;
7mod encode;
8pub mod error;
9
10use crate::error::{OBIError, OBIResult};
6 11
7#[non_exhaustive] 12#[non_exhaustive]
8#[derive(Copy, Clone, Debug, PartialEq)] 13#[derive(Copy, Clone, Debug, PartialEq)]
@@ -59,6 +64,7 @@ impl ImageInfoHeader {
59} 64}
60 65
61#[non_exhaustive] 66#[non_exhaustive]
67#[derive(Copy, Clone, Debug, PartialEq)]
62pub enum CompressionType { 68pub enum CompressionType {
63 RLE, 69 RLE,
64 Kosinki, 70 Kosinki,
@@ -96,23 +102,34 @@ impl Image {
96 data, 102 data,
97 } 103 }
98 } 104 }
99}
100 105
101#[cfg(test)] 106 pub fn width(&self) -> u32 {
102mod tests { 107 self.image_info_header.width
103 use super::*; 108 }
104 use std::mem::size_of; 109
110 pub fn height(&self) -> u32 {
111 self.image_info_header.height
112 }
113
114 fn to_index(&self, x: u32, y: u32) -> usize {
115 (y * self.width() + x) as usize
116 }
117
118 pub fn set_pixel(&mut self, x: u32, y: u32, val: bool) -> OBIResult<()> {
119 if x >= self.width() || y > self.height() {
120 Err(OBIError::Image)
121 } else {
122 let index = self.to_index(x, y);
123 self.data[index] = val;
124 Ok(())
125 }
126 }
105 127
106 #[test] 128 pub fn encode(&self) -> OBIResult<Vec<u8>> {
107 fn size_of_image_info_header() { 129 encode::encode_image(self)
108 let file_header_size = size_of::<ImageInfoHeader>();
109 assert_eq!(16, file_header_size);
110 } 130 }
111 131
112 #[test] 132 pub fn decode(data: &mut io::Cursor<Vec<u8>>) -> OBIResult<Image> {
113 fn encode_decode() { 133 decode::decode_image(data)
114 let img = Image::new(100, 80);
115 let encoded = encode::encode_image(img).unwrap();
116 assert_eq!(encoded.len(), 1026);
117 } 134 }
118} 135}