use std::error;
use std::fmt;
use std::str;
use std::string::FromUtf8Error;
use raw::FstType;
pub enum Error {
Version {
expected: u64,
got: u64,
},
Format,
DuplicateKey {
got: Vec<u8>,
},
OutOfOrder {
previous: Vec<u8>,
got: Vec<u8>,
},
WrongType {
expected: FstType,
got: FstType,
},
FromUtf8(FromUtf8Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match *self {
FromUtf8(ref err) => err.fmt(f),
Version { expected, got } => {
write!(f, "\
Error opening FST: expected API version {}, got API version {}.
It looks like the FST you're trying to open is either not an FST file or it
was generated with a different version of the 'fst' crate. You'll either need
to change the version of the 'fst' crate you're using, or re-generate the
FST.", expected, got)
}
Format => write!(f, "\
Error opening FST: An unknown error occurred. This usually means you're trying
to read data that isn't actually an encoded FST."),
DuplicateKey { ref got } => write!(f, "\
Error inserting duplicate key: {}.", format_bytes(&*got)),
OutOfOrder { ref previous, ref got } => write!(f, "\
Error inserting out-of-order key: {}. (Previous key was {}.) Keys must be
inserted in lexicographic order.",
format_bytes(&*got), format_bytes(&*previous)),
WrongType { expected, got } => write!(f, "\
Error opening FST: expected type {}, got type {}.", expected, got),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl error::Error for Error {
fn description(&self) -> &str {
use self::Error::*;
match *self {
FromUtf8(ref err) => err.description(),
Version { .. } => "incompatible version found when opening FST",
Format => "unknown invalid format found when opening FST",
DuplicateKey { .. } => "duplicate key insertion",
OutOfOrder { .. } => "out-of-order key insertion",
WrongType { .. } => "incompatible type found when opening FST",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::FromUtf8(ref err) => Some(err),
_ => None,
}
}
}
impl From<FromUtf8Error> for Error {
#[inline]
fn from(err: FromUtf8Error) -> Self {
Error::FromUtf8(err)
}
}
fn format_bytes(bytes: &[u8]) -> String {
match str::from_utf8(bytes) {
Ok(s) => s.to_owned(),
Err(_) => format!("{:?}", bytes),
}
}