Skip to main content

rust_ethernet_ip_protocol/
lib.rs

1//! EtherNet/IP encapsulation and CIP wire codecs.
2
3use bytes::{Buf, BytesMut};
4
5/// Common Industrial Protocol request and response codecs.
6pub mod cip;
7/// EtherNet/IP encapsulation packet codecs.
8pub mod encap;
9/// Logix value type codes and payload codecs.
10pub mod values;
11
12/// Error returned when protocol data is invalid or unsupported.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct ProtocolError {
15    message: String,
16}
17
18impl ProtocolError {
19    /// Creates a protocol error with a human-readable explanation.
20    #[must_use]
21    pub fn new(message: impl Into<String>) -> Self {
22        Self {
23            message: message.into(),
24        }
25    }
26}
27
28impl std::fmt::Display for ProtocolError {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "{}", self.message)
31    }
32}
33
34impl std::error::Error for ProtocolError {}
35
36/// Result type used by protocol codecs.
37pub type Result<T> = std::result::Result<T, ProtocolError>;
38
39/// Encodes a value into an existing byte buffer.
40pub trait Encode {
41    /// Appends this value's wire representation to `buf`.
42    fn encode(&self, buf: &mut BytesMut);
43}
44
45/// Decodes a value from a byte buffer.
46pub trait Decode: Sized {
47    /// Consumes and decodes one value from `buf`.
48    fn decode(buf: &mut impl Buf) -> Result<Self>;
49}
50
51#[cfg(test)]
52mod tests;