1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#[cfg(feature = "rest")]
use ergo_lib::ergo_rest::{NodeError, PeerDiscoveryError};

use std::error;
use thiserror::Error;

// pub type Result<T> = result::Result<T, Box<dyn error::Error + 'static>>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("error: {0}")]
    Misc(Box<dyn error::Error + 'static>),
    #[error("invalid argument: {0}")]
    InvalidArgument(&'static str),
}

pub type ErrorPtr = *mut Error;

impl Error {
    pub fn misc<E>(details: E) -> Self
    where
        E: error::Error + 'static,
    {
        Error::Misc(Box::new(details))
    }

    pub fn c_api_from(result: Result<(), Error>) -> ErrorPtr {
        match result {
            Ok(()) => std::ptr::null_mut(),
            Err(err) => Box::into_raw(Box::new(err)),
        }
    }
}

#[cfg(feature = "rest")]
impl From<NodeError> for Error {
    fn from(error: NodeError) -> Self {
        Error::Misc(format!("{:?}", error).into())
    }
}

#[cfg(feature = "rest")]
impl From<PeerDiscoveryError> for Error {
    fn from(error: PeerDiscoveryError) -> Self {
        Error::Misc(format!("{:?}", error).into())
    }
}