jvs_packets/
lib.rs

1//! A packet structures for [JAMMA Video Standart] protocols.
2//!
3//! This crate provides a wrapper around `[`[u8]`]` array with getter and setter methods for easily writing/reading data.
4//!
5//! # Example
6//! ```
7//! use jvs_packets::{jvs::{RequestPacket}, ReadPacket, Packet};
8//!
9//! # fn main() -> std::io::Result<()> {
10//!     // This is only for example. You can use any structure, that implements std::io::Read.
11//!     let mut reader = std::io::Cursor::new([0xE0, 0xFF, 0x03, 0x01, 0x02, 0x05]);
12//!     let mut req_packet: RequestPacket = RequestPacket::new();
13//!     reader.read_packet(&mut req_packet)?;
14//!     
15//!     assert_eq!(req_packet.size(), 0x03);
16//!     Ok(())
17//! # }
18//! ```
19//!
20//! [JAMMA Video Standart]: https://en.wikipedia.org/wiki/Japan_Amusement_Machine_and_Marketing_Association#Video
21
22mod packet;
23pub use packet::{
24    Packet, ReadByteExt, ReadPacket, Report, ReportField, WriteByteExt, WritePacket, MARK_BYTE,
25    SYNC_BYTE,
26};
27
28#[cfg(feature = "jvs")]
29pub mod jvs;
30
31#[cfg(feature = "jvs_modified")]
32pub mod jvs_modified;
33
34#[cfg(any(feature = "jvs", feature = "jvs_modified"))]
35macro_rules! impl_required_packet_blocks {
36    ($t:tt) => {
37        impl<const N: usize> $t<N> {
38            pub const fn new() -> Self {
39                Self { inner: [0; N] }
40            }
41
42            pub fn from_reader(reader: &mut impl crate::ReadPacket) -> std::io::Result<Self> {
43                let mut packet = $t::new();
44                reader.read_packet(&mut packet)?;
45
46                Ok(packet)
47            }
48
49            /// Initialize a struct from a slice.
50            ///
51            /// # Panics
52            /// If the slice length is less than 4 and more than N.
53            /// The slice can't be less than 4 because the packet is always has at least 4 bytes.
54            pub fn from_slice(slice: &[u8]) -> Self {
55                let mut packet = Self::new();
56                packet.inner[..slice.len()].copy_from_slice(slice);
57                packet
58            }
59        }
60
61        impl<const N: usize> AsRef<[u8]> for $t<N> {
62            fn as_ref(&self) -> &[u8] {
63                &self.inner
64            }
65        }
66
67        impl<const N: usize> AsMut<[u8]> for $t<N> {
68            fn as_mut(&mut self) -> &mut [u8] {
69                &mut self.inner
70            }
71        }
72
73        impl<const N: usize> Default for $t<N> {
74            fn default() -> Self {
75                Self::new()
76            }
77        }
78    };
79}
80
81pub(crate) use impl_required_packet_blocks;