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
//! Farbfeld is a simple image encoding format from suckless.
//! # Related Links
//! * http://git.suckless.org/farbfeld/tree/FORMAT.
#![deny(unsafe_code)]
#![deny(trivial_casts, trivial_numeric_casts)]
#![deny(missing_docs, missing_debug_implementations, missing_copy_implementations)]
#![deny(unused_extern_crates, unused_import_braces, unused_qualifications)]

extern crate byteorder;

use std::error;
use std::fmt;
use std::io;

mod decoder;
mod encoder;
#[cfg(test)]
mod tests;

pub use decoder::Decoder;
pub use encoder::Encoder;

const HEADER_LEN: u64 = 8+4+4;

/// Result of an image decoding/encoding process
pub type Result<T> = ::std::result::Result<T, Error>;

/// An enumeration of decoding/encoding Errors
#[derive(Debug)]
pub enum Error {
     /// The Image is not formatted properly
    FormatError(String),

    /// Not enough data was provided to the Decoder
    /// to decode the image
    NotEnoughData,

    /// An I/O Error occurred while decoding the image
    IoError(io::Error),

    /// The end of the image has been reached
    ImageEnd
}


impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &Error::FormatError(ref e) => write!(fmt, "Format error: {}", e),
            &Error::NotEnoughData => write!(fmt, "Not enough data was provided to the \
                                                         Decoder to decode the image"),
            &Error::IoError(ref e) => e.fmt(fmt),
            &Error::ImageEnd => write!(fmt, "The end of the image has been reached")
        }
    }
}

impl error::Error for Error {
    fn description (&self) -> &str {
        match *self {
            Error::FormatError(..) => &"Format error",
            Error::NotEnoughData => &"Not enough data",
            Error::IoError(..) => &"IO error",
            Error::ImageEnd => &"Image end"
        }
    }

    fn cause (&self) -> Option<&error::Error> {
        match *self {
            Error::IoError(ref e) => Some(e),
            _ => None
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IoError(err)
    }
}