farbfeld/
lib.rs

1//! Farbfeld is a simple image encoding format from suckless.
2//! # Related Links
3//! * http://git.suckless.org/farbfeld/tree/FORMAT.
4#![deny(unsafe_code)]
5#![deny(trivial_casts, trivial_numeric_casts)]
6#![deny(missing_docs, missing_debug_implementations, missing_copy_implementations)]
7#![deny(unused_extern_crates, unused_import_braces, unused_qualifications)]
8
9extern crate byteorder;
10
11use std::error;
12use std::fmt;
13use std::io;
14
15mod decoder;
16mod encoder;
17#[cfg(test)]
18mod tests;
19
20pub use decoder::Decoder;
21pub use encoder::Encoder;
22
23const HEADER_LEN: u64 = 8+4+4;
24
25/// Result of an image decoding/encoding process
26pub type Result<T> = ::std::result::Result<T, Error>;
27
28/// An enumeration of decoding/encoding Errors
29#[derive(Debug)]
30pub enum Error {
31     /// The Image is not formatted properly
32    FormatError(String),
33
34    /// Not enough data was provided to the Decoder
35    /// to decode the image
36    NotEnoughData,
37
38    /// An I/O Error occurred while decoding the image
39    IoError(io::Error),
40
41    /// The end of the image has been reached
42    ImageEnd
43}
44
45
46impl fmt::Display for Error {
47    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48        match self {
49            &Error::FormatError(ref e) => write!(fmt, "Format error: {}", e),
50            &Error::NotEnoughData => write!(fmt, "Not enough data was provided to the \
51                                                         Decoder to decode the image"),
52            &Error::IoError(ref e) => e.fmt(fmt),
53            &Error::ImageEnd => write!(fmt, "The end of the image has been reached")
54        }
55    }
56}
57
58impl error::Error for Error {
59    fn description (&self) -> &str {
60        match *self {
61            Error::FormatError(..) => &"Format error",
62            Error::NotEnoughData => &"Not enough data",
63            Error::IoError(..) => &"IO error",
64            Error::ImageEnd => &"Image end"
65        }
66    }
67
68    fn cause (&self) -> Option<&error::Error> {
69        match *self {
70            Error::IoError(ref e) => Some(e),
71            _ => None
72        }
73    }
74}
75
76impl From<io::Error> for Error {
77    fn from(err: io::Error) -> Error {
78        Error::IoError(err)
79    }
80}