doip_codec/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)] // Use no_std when the "std" feature is disabled
2#![warn(clippy::pedantic)]
3#![warn(missing_docs)]
4#![warn(missing_debug_implementations)]
5//! # Diagnostics over Internet Protocol Codec Crate
6//!
7//! The purpose of this crate is to provide an easy way to encode and decode
8//! `DoIP` Messages defined in the `doip-definitions` crate.
9//!
10//!
11
12mod decoder;
13mod doip_message;
14mod encoder;
15mod error;
16
17pub use crate::error::*;
18use heapless::Vec;
19
20/// A simple Decoder and Encoder implementation for Diagnostics over Internet
21/// Protocol.
22///
23/// Can be used independently via `encode` and `decode` methods, however is best
24/// utilised during.
25#[derive(Debug)]
26pub struct DoipCodec<const N: usize> {}
27
28/// Decoder trait to decode inbound messages from a source and produce human-readable and programmable
29/// output. Similar but adapted from the `tokio_utils` Decoder to be used within a `no_std` environment.
30pub trait Decoder<const N: usize> {
31    /// The type of decoded frames
32    type Item;
33    /// The type of unrecoverable frame decoding errors.
34    ///
35    /// If an individual message is ill-formed but can be ignored without interfering with the
36    /// processing of future messages, it may be more useful to report the failure as an Item.
37    type Error: From<DecodeError>;
38
39    /// Attempts to decode a frame from the provided buffer of bytes.
40    fn from_bytes(&mut self, src: &mut Vec<u8, N>) -> Result<Option<Self::Item>, Self::Error>;
41}
42
43/// Encoder trait to encode runtime or compile time messages for diagnsotic applications into streamable
44/// bytes. Similar but adapted from the `tokio_utils` Encoder to be used within a `no_std` environment.
45pub trait Encoder<Item, const N: usize> {
46    /// The type of encoding errors.
47    type Error: From<EncodeError>;
48
49    /// Encodes a frame into the buffer provided.
50    fn to_bytes(&mut self, item: Item, dst: &mut Vec<u8, N>) -> Result<(), Self::Error>;
51}
52
53trait ToBytes {
54    fn to_bytes(self) -> &'static [u8];
55}
56
57trait FromBytes {
58    fn from_bytes(bytes: &[u8]) -> Option<Self>
59    where
60        Self: Sized;
61}
62
63// Python bindings (only available when std is enabled)
64#[cfg(feature = "std")]
65#[cfg(any(not(test), rust_analyzer))]
66mod bindings;