hermes_encoding_components/impls/
delegate.rs1use core::marker::PhantomData;
2
3use cgp::prelude::{DelegateComponent, HasErrorType};
4
5use crate::traits::convert::Converter;
6use crate::traits::decode::Decoder;
7use crate::traits::encode::Encoder;
8use crate::traits::schema::SchemaGetter;
9use crate::traits::types::encoded::HasEncodedType;
10use crate::traits::types::schema::HasSchemaType;
11
12pub struct DelegateEncoding<Delegate>(pub PhantomData<Delegate>);
13
14impl<Encoding, Strategy, Value, Components, Delegate> Encoder<Encoding, Strategy, Value>
15 for DelegateEncoding<Components>
16where
17 Encoding: HasEncodedType + HasErrorType,
18 Components: DelegateComponent<(Strategy, Value), Delegate = Delegate>,
19 Delegate: Encoder<Encoding, Strategy, Value>,
20{
21 fn encode(encoding: &Encoding, value: &Value) -> Result<Encoding::Encoded, Encoding::Error> {
22 Delegate::encode(encoding, value)
23 }
24}
25
26impl<Encoding, Strategy, Value, Components, Delegate> Decoder<Encoding, Strategy, Value>
27 for DelegateEncoding<Components>
28where
29 Encoding: HasEncodedType + HasErrorType,
30 Components: DelegateComponent<(Strategy, Value), Delegate = Delegate>,
31 Delegate: Decoder<Encoding, Strategy, Value>,
32{
33 fn decode(encoding: &Encoding, encoded: &Encoding::Encoded) -> Result<Value, Encoding::Error> {
34 Delegate::decode(encoding, encoded)
35 }
36}
37
38impl<Encoding, From, To, Components, Delegate> Converter<Encoding, From, To>
39 for DelegateEncoding<Components>
40where
41 Encoding: HasErrorType,
42 Components: DelegateComponent<(From, To), Delegate = Delegate>,
43 Delegate: Converter<Encoding, From, To>,
44{
45 fn convert(encoding: &Encoding, from: &From) -> Result<To, Encoding::Error> {
46 Delegate::convert(encoding, from)
47 }
48}
49
50impl<Encoding, Value, Components, Delegate> SchemaGetter<Encoding, Value>
51 for DelegateEncoding<Components>
52where
53 Encoding: HasSchemaType,
54 Components: DelegateComponent<Value, Delegate = Delegate>,
55 Delegate: SchemaGetter<Encoding, Value>,
56{
57 fn schema(encoding: &Encoding, phantom: PhantomData<Value>) -> &Encoding::Schema {
58 Delegate::schema(encoding, phantom)
59 }
60}