1use crate::api::{EndpointFinishApi, ErrorApi};
2use crate::numbat_codec::{EncodeError, TopEncode, TopEncodeOutput};
3use crate::Vec;
4
5struct ApiOutputAdapter<FA>
6where
7 FA: EndpointFinishApi + Clone + 'static,
8{
9 api: FA,
10}
11
12impl<FA> ApiOutputAdapter<FA>
13where
14 FA: EndpointFinishApi + Clone + 'static,
15{
16 #[inline]
17 fn new(api: FA) -> Self {
18 ApiOutputAdapter { api }
19 }
20}
21
22impl<FA> TopEncodeOutput for ApiOutputAdapter<FA>
23where
24 FA: EndpointFinishApi + Clone + 'static,
25{
26 fn set_slice_u8(self, bytes: &[u8]) {
27 self.api.finish_slice_u8(bytes);
28 }
29
30 fn set_u64(self, value: u64) {
31 self.api.finish_u64(value);
32 }
33
34 fn set_i64(self, value: i64) {
35 self.api.finish_i64(value);
36 }
37
38 #[inline]
39 fn set_unit(self) {
40 }
42
43 #[inline]
44 fn set_big_int_handle_or_bytes<F: FnOnce() -> Vec<u8>>(self, handle: i32, _else_bytes: F) {
45 self.api.finish_big_int_raw(handle);
46 }
47
48 #[inline]
49 fn set_big_uint_handle_or_bytes<F: FnOnce() -> Vec<u8>>(self, handle: i32, _else_bytes: F) {
50 self.api.finish_big_uint_raw(handle);
51 }
52}
53
54pub trait EndpointResult: Sized {
56 type DecodeAs;
59
60 fn finish<FA>(&self, api: FA)
61 where
62 FA: EndpointFinishApi + Clone + 'static;
63}
64
65impl<T> EndpointResult for T
67where
68 T: TopEncode,
69{
70 type DecodeAs = Self;
71
72 fn finish<FA>(&self, api: FA)
73 where
74 FA: EndpointFinishApi + Clone + 'static,
75 {
76 self.top_encode_or_exit(ApiOutputAdapter::new(api.clone()), api, finish_exit);
77 }
78}
79
80#[inline(always)]
81fn finish_exit<FA>(api: FA, en_err: EncodeError) -> !
82where
83 FA: EndpointFinishApi + ErrorApi + 'static,
84{
85 api.signal_error(en_err.message_bytes())
86}