sui_rpc/client/response_ext.rs
1use sui_sdk_types::Digest;
2
3/// Extension trait used to facilitate retrieval of Sui specific data from responses
4pub trait ResponseExt: sealed::Sealed {
5 fn chain_id(&self) -> Option<Digest>;
6 fn chain(&self) -> Option<&str>;
7 fn epoch(&self) -> Option<u64>;
8 fn checkpoint_height(&self) -> Option<u64>;
9 fn timestamp_ms(&self) -> Option<u64>;
10 fn timestamp(&self) -> Option<&str>;
11 fn lowest_available_checkpoint(&self) -> Option<u64>;
12 fn lowest_available_checkpoint_objects(&self) -> Option<u64>;
13}
14
15impl ResponseExt for http::header::HeaderMap {
16 fn chain_id(&self) -> Option<Digest> {
17 self.get(crate::headers::X_SUI_CHAIN_ID)
18 .map(http::header::HeaderValue::as_bytes)
19 .and_then(|s| Digest::from_base58(s).ok())
20 }
21
22 fn chain(&self) -> Option<&str> {
23 self.get(crate::headers::X_SUI_CHAIN)
24 .and_then(|h| h.to_str().ok())
25 }
26
27 fn epoch(&self) -> Option<u64> {
28 self.get(crate::headers::X_SUI_EPOCH)
29 .and_then(|h| h.to_str().ok())
30 .and_then(|s| s.parse().ok())
31 }
32
33 fn checkpoint_height(&self) -> Option<u64> {
34 self.get(crate::headers::X_SUI_CHECKPOINT_HEIGHT)
35 .and_then(|h| h.to_str().ok())
36 .and_then(|s| s.parse().ok())
37 }
38
39 fn timestamp_ms(&self) -> Option<u64> {
40 self.get(crate::headers::X_SUI_TIMESTAMP_MS)
41 .and_then(|h| h.to_str().ok())
42 .and_then(|s| s.parse().ok())
43 }
44
45 fn timestamp(&self) -> Option<&str> {
46 self.get(crate::headers::X_SUI_TIMESTAMP)
47 .and_then(|h| h.to_str().ok())
48 }
49
50 fn lowest_available_checkpoint(&self) -> Option<u64> {
51 self.get(crate::headers::X_SUI_LOWEST_AVAILABLE_CHECKPOINT)
52 .and_then(|h| h.to_str().ok())
53 .and_then(|s| s.parse().ok())
54 }
55
56 fn lowest_available_checkpoint_objects(&self) -> Option<u64> {
57 self.get(crate::headers::X_SUI_LOWEST_AVAILABLE_CHECKPOINT_OBJECTS)
58 .and_then(|h| h.to_str().ok())
59 .and_then(|s| s.parse().ok())
60 }
61}
62
63impl ResponseExt for tonic::metadata::MetadataMap {
64 fn chain_id(&self) -> Option<Digest> {
65 self.as_ref().chain_id()
66 }
67
68 fn chain(&self) -> Option<&str> {
69 self.as_ref().chain()
70 }
71
72 fn epoch(&self) -> Option<u64> {
73 self.as_ref().epoch()
74 }
75
76 fn checkpoint_height(&self) -> Option<u64> {
77 self.as_ref().checkpoint_height()
78 }
79
80 fn timestamp_ms(&self) -> Option<u64> {
81 self.as_ref().timestamp_ms()
82 }
83
84 fn timestamp(&self) -> Option<&str> {
85 self.as_ref().timestamp()
86 }
87
88 fn lowest_available_checkpoint(&self) -> Option<u64> {
89 self.as_ref().lowest_available_checkpoint()
90 }
91
92 fn lowest_available_checkpoint_objects(&self) -> Option<u64> {
93 self.as_ref().lowest_available_checkpoint_objects()
94 }
95}
96
97impl<T> ResponseExt for tonic::Response<T> {
98 fn chain_id(&self) -> Option<Digest> {
99 self.metadata().chain_id()
100 }
101
102 fn chain(&self) -> Option<&str> {
103 self.metadata().chain()
104 }
105
106 fn epoch(&self) -> Option<u64> {
107 self.metadata().epoch()
108 }
109
110 fn checkpoint_height(&self) -> Option<u64> {
111 self.metadata().checkpoint_height()
112 }
113
114 fn timestamp_ms(&self) -> Option<u64> {
115 self.metadata().timestamp_ms()
116 }
117
118 fn timestamp(&self) -> Option<&str> {
119 self.metadata().timestamp()
120 }
121
122 fn lowest_available_checkpoint(&self) -> Option<u64> {
123 self.metadata().lowest_available_checkpoint()
124 }
125
126 fn lowest_available_checkpoint_objects(&self) -> Option<u64> {
127 self.metadata().lowest_available_checkpoint_objects()
128 }
129}
130
131impl ResponseExt for tonic::Status {
132 fn chain_id(&self) -> Option<Digest> {
133 self.metadata().chain_id()
134 }
135
136 fn chain(&self) -> Option<&str> {
137 self.metadata().chain()
138 }
139
140 fn epoch(&self) -> Option<u64> {
141 self.metadata().epoch()
142 }
143
144 fn checkpoint_height(&self) -> Option<u64> {
145 self.metadata().checkpoint_height()
146 }
147
148 fn timestamp_ms(&self) -> Option<u64> {
149 self.metadata().timestamp_ms()
150 }
151
152 fn timestamp(&self) -> Option<&str> {
153 self.metadata().timestamp()
154 }
155
156 fn lowest_available_checkpoint(&self) -> Option<u64> {
157 self.metadata().lowest_available_checkpoint()
158 }
159
160 fn lowest_available_checkpoint_objects(&self) -> Option<u64> {
161 self.metadata().lowest_available_checkpoint_objects()
162 }
163}
164
165mod sealed {
166 pub trait Sealed {}
167
168 impl Sealed for tonic::Status {}
169 impl<T> Sealed for tonic::Response<T> {}
170 impl Sealed for http::header::HeaderMap {}
171 impl Sealed for tonic::metadata::MetadataMap {}
172}