forked from lix-project/lix
bbe97dff8b
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
88 lines
2.9 KiB
Rust
88 lines
2.9 KiB
Rust
use std::fmt;
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error {
|
|
InvalidPath(crate::store::StorePath),
|
|
BadStorePath(std::path::PathBuf),
|
|
BadNarInfo,
|
|
BadBase32,
|
|
StorePathNameEmpty,
|
|
StorePathNameTooLong,
|
|
BadStorePathName,
|
|
NarSizeFieldTooBig,
|
|
BadNarString,
|
|
BadNarPadding,
|
|
BadNarVersionMagic,
|
|
MissingNarOpenTag,
|
|
MissingNarCloseTag,
|
|
MissingNarField,
|
|
BadNarField(String),
|
|
BadExecutableField,
|
|
IOError(std::io::Error),
|
|
#[cfg(unused)]
|
|
HttpError(hyper::error::Error),
|
|
Misc(String),
|
|
Foreign(CppException),
|
|
}
|
|
|
|
impl From<std::io::Error> for Error {
|
|
fn from(err: std::io::Error) -> Self {
|
|
Error::IOError(err)
|
|
}
|
|
}
|
|
|
|
#[cfg(unused)]
|
|
impl From<hyper::error::Error> for Error {
|
|
fn from(err: hyper::error::Error) -> Self {
|
|
Error::HttpError(err)
|
|
}
|
|
}
|
|
|
|
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::BadBase32 => write!(f, "invalid base32 string"),
|
|
Error::StorePathNameEmpty => write!(f, "store path name is empty"),
|
|
Error::StorePathNameTooLong => {
|
|
write!(f, "store path name is longer than 211 characters")
|
|
}
|
|
Error::BadStorePathName => write!(f, "store path name contains forbidden character"),
|
|
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"),
|
|
Error::IOError(err) => write!(f, "I/O error: {}", err),
|
|
#[cfg(unused)]
|
|
Error::HttpError(err) => write!(f, "HTTP error: {}", err),
|
|
Error::Foreign(_) => write!(f, "<C++ exception>"), // FIXME
|
|
Error::Misc(s) => write!(f, "{}", s),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Error> for CppException {
|
|
fn from(err: Error) -> Self {
|
|
match err {
|
|
Error::Foreign(ex) => ex,
|
|
_ => unsafe { make_error(&err.to_string()) },
|
|
}
|
|
}
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug)]
|
|
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
|
|
|
|
extern "C" {
|
|
#[allow(improper_ctypes)] // YOLO
|
|
fn make_error(s: &str) -> CppException;
|
|
}
|