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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::ValidationStatus;
use holochain_serialized_bytes::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Judged<T> {
pub data: T,
status: Option<ValidationStatus>,
}
impl<T> Judged<T> {
pub fn new(data: T, status: ValidationStatus) -> Self {
Self {
data,
status: Some(status),
}
}
pub fn raw(data: T, status: Option<ValidationStatus>) -> Self {
Self { data, status }
}
pub fn valid(data: T) -> Self {
Self {
data,
status: Some(ValidationStatus::Valid),
}
}
pub fn none(data: T) -> Self {
Self { data, status: None }
}
pub fn into_data(self) -> T {
self.data
}
pub fn map<B, F>(self, f: F) -> Judged<B>
where
F: FnOnce(T) -> B,
{
Judged::<B> {
data: f(self.data),
status: self.status,
}
}
}
pub trait HasValidationStatus {
type Data;
fn validation_status(&self) -> Option<ValidationStatus>;
fn data(&self) -> &Self::Data;
}
impl<T> HasValidationStatus for Judged<T> {
type Data = T;
fn validation_status(&self) -> Option<ValidationStatus> {
self.status
}
fn data(&self) -> &Self::Data {
&self.data
}
}
impl<T> From<(T, Option<ValidationStatus>)> for Judged<T> {
fn from((data, status): (T, Option<ValidationStatus>)) -> Self {
Self { data, status }
}
}
impl<T> From<Judged<T>> for (T, Option<ValidationStatus>) {
fn from(judged: Judged<T>) -> (T, Option<ValidationStatus>) {
(judged.data, judged.status)
}
}