nix-super/nix-rust/src/error.rs

119 lines
3.7 KiB
Rust
Raw Normal View History

2019-09-17 23:26:49 +03:00
use std::fmt;
2019-09-10 22:55:32 +03:00
#[derive(Debug)]
pub enum Error {
InvalidPath(crate::store::StorePath),
BadStorePath(std::path::PathBuf),
NotInStore(std::path::PathBuf),
BadNarInfo,
2019-09-17 01:18:17 +03:00
BadBase32,
2019-12-06 00:10:16 +02:00
StorePathNameEmpty,
2019-09-17 23:26:49 +03:00
StorePathNameTooLong,
BadStorePathName,
2019-09-18 01:31:51 +03:00
NarSizeFieldTooBig,
BadNarString,
BadNarPadding,
BadNarVersionMagic,
MissingNarOpenTag,
MissingNarCloseTag,
MissingNarField,
BadNarField(String),
BadExecutableField,
IOError(std::io::Error),
#[cfg(unused)]
2019-12-04 22:01:16 +02:00
HttpError(hyper::error::Error),
2019-09-10 22:55:32 +03:00
Misc(String),
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
Foreign(CppException),
2019-12-13 19:11:37 +02:00
BadTarFileMemberName(String),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IOError(err)
}
}
#[cfg(unused)]
2019-12-04 22:01:16 +02:00
impl From<hyper::error::Error> for Error {
fn from(err: hyper::error::Error) -> Self {
Error::HttpError(err)
}
}
2019-09-17 23:26:49 +03:00
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidPath(_) => write!(f, "invalid path"),
Error::BadNarInfo => write!(f, ".narinfo file is corrupt"),
Error::BadStorePath(path) => write!(f, "path '{}' is not a store path", path.display()),
Error::NotInStore(path) => {
write!(f, "path '{}' is not in the Nix store", path.display())
}
2019-09-17 23:26:49 +03:00
Error::BadBase32 => write!(f, "invalid base32 string"),
2019-12-06 00:10:16 +02:00
Error::StorePathNameEmpty => write!(f, "store path name is empty"),
2019-09-17 23:26:49 +03:00
Error::StorePathNameTooLong => {
write!(f, "store path name is longer than 211 characters")
}
Error::BadStorePathName => write!(f, "store path name contains forbidden character"),
2019-09-18 01:31:51 +03:00
Error::NarSizeFieldTooBig => write!(f, "size field in NAR is too big"),
Error::BadNarString => write!(f, "NAR string is not valid UTF-8"),
Error::BadNarPadding => write!(f, "NAR padding is not zero"),
Error::BadNarVersionMagic => write!(f, "unsupported NAR version"),
Error::MissingNarOpenTag => write!(f, "NAR open tag is missing"),
Error::MissingNarCloseTag => write!(f, "NAR close tag is missing"),
Error::MissingNarField => write!(f, "expected NAR field is missing"),
Error::BadNarField(s) => write!(f, "unrecognized NAR field '{}'", s),
Error::BadExecutableField => write!(f, "bad 'executable' field in NAR"),
2019-09-17 23:26:49 +03:00
Error::IOError(err) => write!(f, "I/O error: {}", err),
#[cfg(unused)]
2019-09-17 23:26:49 +03:00
Error::HttpError(err) => write!(f, "HTTP error: {}", err),
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
2019-09-17 23:26:49 +03:00
Error::Foreign(_) => write!(f, "<C++ exception>"), // FIXME
Error::Misc(s) => write!(f, "{}", s),
2019-12-13 19:11:37 +02:00
Error::BadTarFileMemberName(s) => {
write!(f, "tar archive contains illegal file name '{}'", s)
}
2019-09-17 23:26:49 +03:00
}
}
}
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
impl From<Error> for CppException {
fn from(err: Error) -> Self {
match err {
Error::Foreign(ex) => ex,
2019-12-13 20:05:09 +02:00
_ => CppException::new(&err.to_string()),
}
}
}
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
#[repr(C)]
#[derive(Debug)]
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
2019-12-13 20:05:09 +02:00
impl CppException {
fn new(s: &str) -> Self {
Self(unsafe { make_error(s) })
}
}
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
2019-12-13 20:05:09 +02:00
impl Drop for CppException {
fn drop(&mut self) {
unsafe {
destroy_error(self.0);
}
}
}
2019-12-15 11:47:59 +02:00
#[cfg(not(test))]
extern "C" {
2019-09-11 13:44:31 +03:00
#[allow(improper_ctypes)] // YOLO
2019-12-13 20:05:09 +02:00
fn make_error(s: &str) -> *const libc::c_void;
fn destroy_error(exc: *const libc::c_void);
2019-09-10 22:55:32 +03:00
}