Skip to main content

kimchi_stubs/
cache_error.rs

1//! Error plumbing for the cached-index FFI wrappers.
2
3use std::fmt;
4
5/// Owned error raised through [`ocaml::Error::Error`] on the cached-index
6/// FFI paths.
7///
8/// ocaml-rs raises that variant by calling `caml_failwith` on the boxed
9/// error's `Debug` rendering, so `Debug` here must produce the plain
10/// message — boxing the `String` directly would Debug-quote it and OCaml
11/// would see `Failure "\"caml_...: ...\""`. Unlike `ocaml::Error::Message`
12/// (which takes a `&'static str` and previously forced a `Box::leak` per
13/// call), this owns its message and is freed when the error drops.
14pub struct CacheFfiError(String);
15
16impl CacheFfiError {
17    /// Wraps `msg` into the `ocaml::Error` the FFI wrappers return.
18    pub fn wrap(msg: String) -> ocaml::Error {
19        ocaml::Error::Error(Box::new(Self(msg)))
20    }
21}
22
23impl fmt::Debug for CacheFfiError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.write_str(&self.0)
26    }
27}
28
29impl fmt::Display for CacheFfiError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.write_str(&self.0)
32    }
33}
34
35impl std::error::Error for CacheFfiError {}