|  | // Copyright 2023 Google LLC | 
|  | // Use of this source code is governed by a BSD-style license that can be | 
|  | // found in the LICENSE file. | 
|  |  | 
|  | use thiserror::Error; | 
|  |  | 
|  | /// A decodable type can be created from a byte buffer. | 
|  | /// The type returned is separate (copied) from the buffer once decoded. | 
|  | pub trait Decodable: ::core::marker::Sized { | 
|  | type Error; | 
|  |  | 
|  | /// Decodes into a new object with the number of bytes that were decoded, or returns an error. | 
|  | fn decode(buf: &[u8]) -> ::core::result::Result<(Self, usize), Self::Error>; | 
|  | } | 
|  |  | 
|  | /// An encodable type can write itself into a byte buffer. | 
|  | pub trait Encodable: ::core::marker::Sized { | 
|  | type Error; | 
|  |  | 
|  | /// Returns the number of bytes necessary to encode |self|. | 
|  | fn encoded_len(&self) -> ::core::primitive::usize; | 
|  |  | 
|  | /// Writes the encoded version of |self| at the start of |buf|. | 
|  | /// |buf| must be at least |self.encoded_len()| length. | 
|  | fn encode(&self, buf: &mut [u8]) -> ::core::result::Result<(), Self::Error>; | 
|  | } | 
|  |  | 
|  | #[derive(Error, Debug, PartialEq)] | 
|  | pub enum Error { | 
|  | #[error("Parameter is not valid: {0}")] | 
|  | InvalidParameter(String), | 
|  |  | 
|  | #[error("Out-of-range enum value")] | 
|  | OutOfRange, | 
|  |  | 
|  | #[error("Encoding buffer is too small")] | 
|  | BufferTooSmall, | 
|  |  | 
|  | #[error("Buffer being decoded is invalid length")] | 
|  | UnexpectedDataLength, | 
|  | } |