gettext_ng/
error.rs

1use std::borrow::Cow;
2use std::error;
3use std::fmt;
4use std::io;
5
6/// Represents an error encountered while parsing an MO file.
7#[derive(Debug)]
8pub enum Error {
9    /// An incorrect magic number has been encountered
10    BadMagic,
11    /// An invalid byte sequence for the given encoding has been encountered
12    DecodingError,
13    /// An unexpected EOF occured
14    Eof,
15    /// An I/O error occured
16    Io(io::Error),
17    /// Incorrect syntax encountered while parsing the meta information
18    MalformedMetadata,
19    /// Meta information string was not the first string in the catalog
20    MisplacedMetadata,
21    /// Invalid Plural-Forms metadata
22    PluralParsing,
23    /// An unknown encoding was specified in the metadata
24    UnknownEncoding,
25}
26use self::Error::*;
27
28impl error::Error for Error {
29    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
30        match *self {
31            Io(ref err) => Some(err),
32            _ => None,
33        }
34    }
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39        match *self {
40            BadMagic => write!(fmt, "bad magic number"),
41            DecodingError => write!(fmt, "invalid byte sequence in a string"),
42            Eof => write!(fmt, "unxpected end of file"),
43            Io(ref err) => err.fmt(fmt),
44            MalformedMetadata => write!(fmt, "metadata syntax error"),
45            MisplacedMetadata => write!(fmt, "misplaced metadata"),
46            UnknownEncoding => write!(fmt, "unknown encoding specified"),
47            PluralParsing => write!(fmt, "invalid plural expression"),
48        }
49    }
50}
51
52impl From<io::Error> for Error {
53    fn from(inner: io::Error) -> Error {
54        Io(inner)
55    }
56}
57
58impl From<Cow<'static, str>> for Error {
59    fn from(_: Cow<'static, str>) -> Error {
60        DecodingError
61    }
62}