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("unknown constant {0}")]
17    UnknownConstant(String),
18    #[error("not an olean file")]
19    NotOlean,
20    #[error("unsupported olean format {0}")]
21    Format(u8),
22    #[error(
23        "built by Lean {0}, but only 4.{lo} to 4.{hi} are supported",
24        lo = SUPPORTED.start(),
25        hi = SUPPORTED.end()
26    )]
27    UnsupportedLean(String),
28    /// The library was called in a way it does not support.
29    #[error("{0}")]
30    Misuse(&'static str),
31    /// The file is malformed, or the environment it describes is inconsistent.
32    #[error("{0}")]
33    Corrupt(String),
34    #[error("{}: {source}", path.display())]
35    File { path: PathBuf, source: Box<Self> },
36    #[error(transparent)]
37    Json(#[from] serde_json::Error),
38    #[error("blean: {0}")]
39    Postcard(#[from] postcard::Error),
40    #[error(transparent)]
41    Threads(#[from] rayon::ThreadPoolBuildError),
42}
43
44pub type Result<T, E = Error> = std::result::Result<T, E>;
45
46pub(crate) fn corrupt(msg: impl Display) -> Error {
47    Error::Corrupt(msg.to_string())
48}
49
50pub(crate) fn at(path: &Path) -> impl FnOnce(Error) -> Error + '_ {
51    move |e| Error::File {
52        path: path.to_owned(),
53        source: Box::new(e),
54    }
55}