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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::collections::TryReserveError;
use anyhow::anyhow;
use std::fmt::{Debug, Display, Formatter};
use docchi_compaction::kval_enum::KVal;
use std::error::Error;
use docchi_compaction::error::ComError;
pub type DiffResult<T> = std::result::Result<T, DiffError>;
pub(crate) trait OptToErr{
fn ast_i64(&self) -> Result<i64, &'static str>;
fn ast_bool(&self) -> Result<bool, &'static str>;
fn ast_f64(&self) -> Result<f64, &'static str>;
fn ast_string(&self) -> Result<String, &'static str>;
}
impl OptToErr for KVal{
fn ast_i64(&self) -> Result<i64, &'static str> {
self.as_i64().ok_or("The value is not i64")
}
fn ast_bool(&self) -> Result<bool, &'static str> {
self.as_bool().ok_or("The value is not bool")
}
fn ast_f64(&self) -> Result<f64, &'static str> {
self.as_f64().ok_or("The value is not f64")
}
fn ast_string(&self) -> Result<String, &'static str> {
self.as_string().ok_or("The value is not string")
}
}
pub struct DiffError {
e : anyhow::Error
}
impl DiffError{
pub(crate) fn new(e : impl Error + Send + Sync + 'static) -> Self{ Self{ e : e.into() } }
}
impl Display for DiffError{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.e, f)
}
}
impl Debug for DiffError{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.e, f)
}
}
impl Error for DiffError{
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.e.source()
}
}
impl From<TryReserveError> for DiffError{
fn from(e: TryReserveError) -> Self { DiffError::new(e) }
}
impl From<ComError> for DiffError{
fn from(e: ComError) -> Self { DiffError::new(e) }
}
impl From<&str> for DiffError{
fn from(e: &str) -> Self { Self{ e : anyhow!("{}", e) } }
}
impl From<String> for DiffError{
fn from(e: String) -> Self { Self{ e : anyhow!("{}", e) } }
}