Skip to main content

olean_export/
error.rs

1use crate::olean::SUPPORTED;
2use std::{
3    fmt::Display,
4    io,
5    path::{Path, PathBuf},
6};
7
8/// Everything that can go wrong loading oleans or writing an export.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    #[error(transparent)]
13    Io(#[from] io::Error),
14    #[error("module {0} not found on LEAN_PATH")]
15    ModuleNotFound(String),
16    #[error("not an olean file")]
17    NotOlean,
18    #[error("unsupported olean format {0}")]
19    Format(u8),
20    #[error(
21        "built by Lean {0}, but only 4.{lo} to 4.{hi} are supported",
22        lo = SUPPORTED.start(),
23        hi = SUPPORTED.end()
24    )]
25    UnsupportedLean(String),
26    /// The file is malformed, or the environment it describes is inconsistent.
27    #[error("{0}")]
28    Corrupt(String),
29    #[error("{}: {source}", path.display())]
30    File { path: PathBuf, source: Box<Self> },
31    #[error(transparent)]
32    Json(#[from] serde_json::Error),
33    #[error("blean: {0}")]
34    Postcard(#[from] postcard::Error),
35    #[error(transparent)]
36    Threads(#[from] rayon::ThreadPoolBuildError),
37}
38
39pub type Result<T, E = Error> = std::result::Result<T, E>;
40
41pub(crate) fn corrupt(msg: impl Display) -> Error {
42    Error::Corrupt(msg.to_string())
43}
44
45pub(crate) fn at(path: &Path) -> impl FnOnce(Error) -> Error + '_ {
46    move |e| Error::File {
47        path: path.to_owned(),
48        source: Box::new(e),
49    }
50}