apache_dubbo/triple/codec/mod.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18pub mod buffer;
19pub mod prost;
20pub mod serde_codec;
21
22use std::io;
23
24pub use self::buffer::{DecodeBuf, EncodeBuf};
25use crate::status::Status;
26
27pub trait Codec {
28 /// The encodable message.
29 type Encode: Send + 'static;
30 /// The decodable message.
31 type Decode: Send + 'static;
32
33 /// The encoder that can encode a message.
34 type Encoder: Encoder<Item = Self::Encode, Error = Status> + Send + 'static;
35 /// The encoder that can decode a message.
36 type Decoder: Decoder<Item = Self::Decode, Error = Status> + Send + 'static;
37
38 /// Fetch the encoder.
39 fn encoder(&mut self) -> Self::Encoder;
40 /// Fetch the decoder.
41 fn decoder(&mut self) -> Self::Decoder;
42}
43
44/// Encodes gRPC message types
45pub trait Encoder {
46 /// The type that is encoded.
47 type Item;
48
49 /// The type of encoding errors.
50 ///
51 /// The type of unrecoverable frame encoding errors.
52 type Error: From<io::Error>;
53
54 /// Encodes a message into the provided buffer.
55 fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error>;
56}
57
58/// Decodes gRPC message types
59pub trait Decoder {
60 /// The type that is decoded.
61 type Item;
62
63 /// The type of unrecoverable frame decoding errors.
64 type Error: From<io::Error>;
65
66 /// Decode a message from the buffer.
67 ///
68 /// The buffer will contain exactly the bytes of a full message. There
69 /// is no need to get the length from the bytes, gRPC framing is handled
70 /// for you.
71 fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error>;
72}