2021-07-03 07:03:52 +00:00
|
|
|
pub use super::schemas::internal_protocol::_Any;
|
|
|
|
pub use super::schemas::internal_protocol::_Ptr;
|
|
|
|
pub use super::schemas::internal_protocol::Error;
|
|
|
|
|
|
|
|
use preserves::value::NestedValue;
|
2021-07-24 21:22:01 +00:00
|
|
|
use preserves::value::Value;
|
2021-07-03 07:03:52 +00:00
|
|
|
use preserves_schema::support::ParseError;
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
write!(f, "Error: {}; detail: {:?}", self.message, self.detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
pub fn error<Detail>(message: &str, detail: Detail) -> Error where _Any: From<Detail> {
|
2021-07-03 07:03:52 +00:00
|
|
|
Error {
|
|
|
|
message: message.to_owned(),
|
2021-07-22 14:53:56 +00:00
|
|
|
detail: _Any::from(detail),
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 21:22:01 +00:00
|
|
|
pub fn encode_error(result: Result<(), Error>) -> _Any {
|
|
|
|
match result {
|
|
|
|
Ok(()) => {
|
|
|
|
let mut r = Value::record(Value::symbol("Ok").wrap(), 1);
|
|
|
|
r.fields_vec_mut().push(Value::record(Value::symbol("tuple").wrap(), 0).finish().wrap());
|
|
|
|
r.finish().wrap()
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
let mut r = Value::record(Value::symbol("Err").wrap(), 1);
|
|
|
|
r.fields_vec_mut().push((&e).into());
|
|
|
|
r.finish().wrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 07:03:52 +00:00
|
|
|
impl From<&str> for Error {
|
|
|
|
fn from(v: &str) -> Self {
|
2021-07-08 22:04:11 +00:00
|
|
|
error(v, _Any::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(v: std::io::Error) -> Self {
|
2021-07-08 22:04:11 +00:00
|
|
|
error(&format!("{}", v), _Any::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseError> for Error {
|
|
|
|
fn from(v: ParseError) -> Self {
|
2021-07-08 22:04:11 +00:00
|
|
|
error(&format!("{}", v), _Any::new(false))
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
impl From<preserves::error::Error> for Error {
|
|
|
|
fn from(v: preserves::error::Error) -> Self {
|
|
|
|
error(&format!("{}", v), _Any::new(false))
|
|
|
|
}
|
|
|
|
}
|