1use ixc_message_api::handler::Allocator;
4use ixc_message_api::packet::MessagePacket;
5use crate::buffer::WriterFactory;
6use crate::codec::{decode_value, Codec, ValueDecodeVisitor};
7use crate::decoder::{DecodeError, Decoder};
8use crate::encoder::{EncodeError, Encoder};
9use crate::list::AllocatorVecBuilder;
10use crate::mem::MemoryManager;
11use crate::types::*;
12
13pub trait SchemaValue<'a>
17where
18 Self: 'a,
19{
20 type Type: Type;
22
23
24 type DecodeState: Default;
26
27 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError>;
29
30 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError>
32 where
33 Self: Sized;
34
35 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError>;
37}
38
39impl<'a> SchemaValue<'a> for u8 {
40 type Type = u8;
41 type DecodeState = u8;
42
43 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
44 *state = decoder.decode_u8()?;
45 Ok(())
46 }
47
48 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError>
49 where
50 Self: Sized
51 {
52 Ok(state)
53 }
54
55 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
56 encoder.encode_u8(*self)
57 }
58}
59impl<'a> SchemaValue<'a> for u16 {
60 type Type = u16;
61 type DecodeState = u16;
62
63 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
64 *state = decoder.decode_u16()?;
65 Ok(())
66 }
67
68 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
69 Ok(state)
70 }
71
72 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
73 encoder.encode_u16(*self)
74 }
75}
76
77impl<'a> SchemaValue<'a> for u32 {
78 type Type = u32;
79 type DecodeState = u32;
80
81 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
82 *state = decoder.decode_u32()?;
83 Ok(())
84 }
85
86 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
87 Ok(state)
88 }
89
90 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
91 encoder.encode_u32(*self)
92 }
93}
94
95impl<'a> SchemaValue<'a> for u64 {
96 type Type = u64;
97 type DecodeState = u64;
98
99 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
100 *state = decoder.decode_u64()?;
101 Ok(())
102 }
103
104 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
105 Ok(state)
106 }
107
108 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
109 encoder.encode_u64(*self)
110 }
111}
112
113impl<'a> SchemaValue<'a> for u128 {
114 type Type = UIntNT<16>;
115 type DecodeState = u128;
116
117 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
118 *state = decoder.decode_u128()?;
119 Ok(())
120 }
121
122 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
123 Ok(state)
124 }
125
126 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
127 encoder.encode_u128(*self)
128 }
129}
130
131impl<'a> SchemaValue<'a> for i8 {
132 type Type = i8;
133 type DecodeState = i8;
134
135 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
136 *state = decoder.decode_i8()?;
137 Ok(())
138 }
139
140 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
141 Ok(state)
142 }
143
144 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
145 encoder.encode_i8(*self)
146 }
147}
148
149impl<'a> SchemaValue<'a> for i16 {
150 type Type = i16;
151 type DecodeState = i16;
152
153 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
154 *state = decoder.decode_i16()?;
155 Ok(())
156 }
157
158 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
159 Ok(state)
160 }
161
162 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
163 encoder.encode_i16(*self)
164 }
165}
166
167impl<'a> SchemaValue<'a> for i32 {
168 type Type = i32;
169 type DecodeState = i32;
170
171 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
172 *state = decoder.decode_i32()?;
173 Ok(())
174 }
175
176 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
177 Ok(state)
178 }
179
180 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
181 encoder.encode_i32(*self)
182 }
183}
184
185impl<'a> SchemaValue<'a> for i64 {
186 type Type = i64;
187 type DecodeState = i64;
188
189 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
190 *state = decoder.decode_i64()?;
191 Ok(())
192 }
193
194 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
195 Ok(state)
196 }
197
198 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
199 encoder.encode_i64(*self)
200 }
201}
202
203impl<'a> SchemaValue<'a> for i128 {
204 type Type = IntNT<16>;
205 type DecodeState = i128;
206
207 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
208 *state = decoder.decode_i128()?;
209 Ok(())
210 }
211
212 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
213 Ok(state)
214 }
215
216 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
217 encoder.encode_i128(*self)
218 }
219}
220
221impl<'a> SchemaValue<'a> for bool {
222 type Type = bool;
223 type DecodeState = bool;
224
225 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
226 *state = decoder.decode_bool()?;
227 Ok(())
228 }
229
230 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
231 Ok(state)
232 }
233
234 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
235 encoder.encode_bool(*self)
236 }
237}
238
239impl<'a> SchemaValue<'a> for &'a str {
240 type Type = StrT;
241 type DecodeState = &'a str;
242
243 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
244 *state = decoder.decode_borrowed_str()?;
245 Ok(())
246 }
247
248 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
249 Ok(state)
250 }
251
252 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
253 encoder.encode_str(self)
254 }
255}
256
257#[cfg(feature = "std")]
258impl<'a> SchemaValue<'a> for alloc::string::String {
259 type Type = StrT;
260 type DecodeState = alloc::string::String;
261
262 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
263 *state = decoder.decode_owned_str()?;
264 Ok(())
265 }
266
267 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
268 Ok(state)
269 }
270
271 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
272 encoder.encode_str(self)
273 }
274}
275
276impl<'a> SchemaValue<'a> for simple_time::Time {
277 type Type = TimeT;
278 type DecodeState = simple_time::Time;
279
280 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
281 *state = decoder.decode_time()?;
282 Ok(())
283 }
284
285 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
286 Ok(state)
287 }
288
289 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
290 encoder.encode_time(*self)
291 }
292}
293
294impl<'a> SchemaValue<'a> for simple_time::Duration {
295 type Type = DurationT;
296 type DecodeState = simple_time::Duration;
297
298 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
299 *state = decoder.decode_duration()?;
300 Ok(())
301 }
302
303 fn finish_decode_state(state: Self::DecodeState, _: &'a MemoryManager) -> Result<Self, DecodeError> {
304 Ok(state)
305 }
306
307 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
308 encoder.encode_duration(*self)
309 }
310}
311
312
313impl<'a, V: SchemaValue<'a>> SchemaValue<'a> for Option<V> {
314 type Type = Option<V::Type>;
315 type DecodeState = Option<V::DecodeState>;
316
317 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
318 struct Visitor<'b, U:SchemaValue<'b>>(U::DecodeState);
319 impl <'b, U:SchemaValue<'b>> ValueDecodeVisitor<'b> for Visitor<'b, U> {
321 fn decode(&mut self, decoder: &mut dyn Decoder<'b>) -> Result<(), DecodeError> {
322 U::visit_decode_state(&mut self.0, decoder)
323 }
324 }
325 let mut visitor = Visitor::<V>(V::DecodeState::default());
326 if decoder.decode_option(&mut visitor)? {
327 *state = Some(visitor.0);
328 }
329 Ok(())
330 }
331
332 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError>
333 where
334 Self: Sized
335 {
336 state.map(|state| V::finish_decode_state(state, mem)).transpose()
337 }
338
339 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
340 match self {
341 Some(value) => {
342 encoder.encode_option(Some(value))
343 }
344 None => {
345 encoder.encode_option(None)
346 }
347 }
348 }
349}
350
351impl<'a> SchemaValue<'a> for &'a [u8] {
352 type Type = BytesT;
353 type DecodeState = &'a [u8];
354
355 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
356 *state = decoder.decode_borrowed_bytes()?;
357 Ok(())
358 }
359
360 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError> {
361 Ok(state)
362 }
363
364 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
365 encoder.encode_bytes(self)
366 }
367}
368
369impl<'a> SchemaValue<'a> for alloc::vec::Vec<u8> {
370 type Type = BytesT;
371 type DecodeState = alloc::vec::Vec<u8>;
372
373 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
374 *state = decoder.decode_owned_bytes()?;
375 Ok(())
376 }
377
378 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError> {
379 Ok(state)
380 }
381
382 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
383 encoder.encode_bytes(self)
384 }
385}
386
387pub trait ListElementValue<'a>: SchemaValue<'a>
389where
390 Self::Type: ListElementType,
391{}
392
393impl<'a, V: ListElementValue<'a>> SchemaValue<'a> for &'a [V]
394where
395 V::Type: ListElementType,
396{
397 type Type = ListT<V::Type>;
398 type DecodeState = AllocatorVecBuilder<'a, V>;
399
400 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
401 decoder.decode_list(state)
402 }
403
404 fn finish_decode_state(state: Self::DecodeState, mem_handle: &'a MemoryManager) -> Result<Self, DecodeError> {
405 match state.xs {
406 None => Ok(&[]),
407 Some(xs) => Ok(mem_handle.unpack_slice(xs))
408 }
409 }
410
411 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
412 encoder.encode_list(self)
413 }
414}
415
416impl<'a, V: ListElementValue<'a>> SchemaValue<'a> for allocator_api2::vec::Vec<V, &'a dyn allocator_api2::alloc::Allocator>
417where
418 V::Type: ListElementType,
419{
420 type Type = ListT<V::Type>;
421 type DecodeState = AllocatorVecBuilder<'a, V>;
422
423 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
424 decoder.decode_list(state)
425 }
426
427 fn finish_decode_state(state: Self::DecodeState, mem_handle: &'a MemoryManager) -> Result<Self, DecodeError> {
428 match state.xs {
429 None => Ok(allocator_api2::vec::Vec::new_in(mem_handle)),
430 Some(xs) => Ok(xs)
431 }
432 }
433
434 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
435 todo!()
437 }
438}
439
440#[cfg(feature = "std")]
441impl<'a, V: ListElementValue<'a>> SchemaValue<'a> for alloc::vec::Vec<V>
442where
443 V::Type: ListElementType,
444{
445 type Type = ListT<V::Type>;
446 type DecodeState = alloc::vec::Vec<V>;
447
448 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
449 decoder.decode_list(state)
450 }
451
452 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError>
453 where
454 Self: Sized
455 {
456 Ok(state)
457 }
458
459 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
460 encoder.encode_list(self)
461 }
462}
463
464impl<'a> SchemaValue<'a> for ixc_message_api::AccountID {
465 type Type = AccountIdT;
466 type DecodeState = ixc_message_api::AccountID;
467
468 fn visit_decode_state(state: &mut Self::DecodeState, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError> {
469 *state = decoder.decode_account_id()?;
470 Ok(())
471 }
472
473 fn finish_decode_state(state: Self::DecodeState, mem: &'a MemoryManager) -> Result<Self, DecodeError> {
474 Ok(state)
475 }
476
477 fn encode(&self, encoder: &mut dyn Encoder) -> Result<(), EncodeError> {
478 encoder.encode_account_id(*self)
479 }
480}
481
482#[cfg(feature = "arrayvec")]
483impl<'a, T: Type, V: SchemaValue<'a, T>, const N: usize> SchemaValue<'a, ListT<T>> for arrayvec::ArrayVec<T, N> {}
484#[cfg(feature = "arrayvec")]
485impl<'a, const N: usize> SchemaValue<'a, StrT> for arrayvec::ArrayString<T, N> {}
486
487
488pub trait OptionalValue<'a> {
492 type Value;
494
495 fn decode_value(cdc: &dyn Codec, data: &'a [u8], memory_manager: &'a MemoryManager) -> Result<Self::Value, DecodeError>;
497
498 fn encode_value<'b>(cdc: &dyn Codec, value: &Self::Value, writer_factory: &'b dyn WriterFactory) -> Result<Option<&'b [u8]>, EncodeError>;
500}
501
502impl<'a> OptionalValue<'a> for () {
503 type Value = ();
504
505 fn decode_value(cdc: &dyn Codec, data: &'a [u8], memory_manager: &'a MemoryManager) -> Result<Self::Value, DecodeError> {
506 Ok(())
507 }
508
509 fn encode_value<'b>(cdc: &dyn Codec, value: &Self::Value, writer_factory: &'b dyn WriterFactory) -> Result<Option<&'b [u8]>, EncodeError> {
510 Ok(None)
511 }
512}
513
514impl<'a, V: SchemaValue<'a>> OptionalValue<'a> for V
515{
516 type Value = V;
517
518 fn decode_value(cdc: &dyn Codec, data: &'a [u8], memory_manager: &'a MemoryManager) -> Result<Self::Value, DecodeError> {
519 unsafe { decode_value(cdc, data, memory_manager) }
520 }
521
522 fn encode_value<'b>(cdc: &dyn Codec, value: &Self::Value, writer_factory: &'b dyn WriterFactory) -> Result<Option<&'b [u8]>, EncodeError> {
523 Ok(Some(cdc.encode_value(value, writer_factory)?))
524 }
525}
526
527impl <'a> ListElementValue<'a> for u16 {}
528impl <'a> ListElementValue<'a> for u32 {}
529impl <'a> ListElementValue<'a> for u64 {}
530impl <'a> ListElementValue<'a> for u128 {}
531impl <'a> ListElementValue<'a> for i8 {}
532impl <'a> ListElementValue<'a> for i16 {}
533impl <'a> ListElementValue<'a> for i32 {}
534impl <'a> ListElementValue<'a> for i64 {}
535impl <'a> ListElementValue<'a> for i128 {}
536impl <'a> ListElementValue<'a> for bool {}
537impl <'a> ListElementValue<'a> for &'a str {}
538#[cfg(feature = "std")]
539impl <'a> ListElementValue<'a> for alloc::string::String {}
540impl <'a> ListElementValue<'a> for &'a [u8] {}
541#[cfg(feature = "std")]
542impl <'a> ListElementValue<'a> for alloc::vec::Vec<u8> {}
543impl <'a> ListElementValue<'a> for simple_time::Time {}
544impl <'a> ListElementValue<'a> for simple_time::Duration {}