1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! A packet structures for [JAMMA Video Standart] protocols.
//! 
//! This crate provides a wrapper around `[u8]` array with getter and setter methods for easily changing/writing/reading data.
//! 
//! # Example
//! ```
//! use jvs_packets::{jvs::{RequestPacket}, ReadPacket, Packet};
//! 
//! # fn main() -> std::io::Result<()> {
//!     // This is only for example. You can use any structure, that implements std::io::Read. 
//!     let mut reader = std::io::Cursor::new([0xE0, 0xFF, 0x03, 0x01, 0x02, 0x05]);
//!     let mut req_packet: RequestPacket = RequestPacket::new();
//!     reader.read_packet(&mut req_packet);
//!     
//!     assert_eq!(req_packet.size(), 0x03);
//!     Ok(())
//! # }
//! ```
//! 
//! [JAMMA Video Standart]: https://en.wikipedia.org/wiki/Japan_Amusement_Machine_and_Marketing_Association#Video

mod packet;
pub use packet::{
    Packet, ReadByteExt, ReadPacket, Report, ReportField, WriteByteExt, WritePacket, MARK_BYTE, SYNC_BYTE,
};

#[cfg(feature = "jvs")]
pub mod jvs;

#[cfg(feature = "jvs_modified")]
pub mod jvs_modified;

#[cfg(any(feature = "jvs", feature = "jvs_modified"))]
#[macro_export]
macro_rules! impl_required_packet_blocks {
    ($t:tt) => {
        impl<const N: usize> $t<N> {
            pub const fn new() -> Self {
                Self { inner: [0; N] }
            }

            pub fn from_reader(reader: &mut impl crate::ReadPacket) -> std::io::Result<Self> {
                let mut packet = $t::new();
                reader.read_packet(&mut packet)?;

                Ok(packet)
            }

            /// Initialize a struct from a slice.
            ///
            /// # Panics
            /// If the slice length is less than 4 and more than N.
            /// The slice can't be less than 4 because the packet is always has at least 4 bytes.
            pub fn from_slice(slice: &[u8]) -> Self {
                let mut packet = Self::new();
                packet.inner[..slice.len()].copy_from_slice(slice);
                packet
            }
        }

        impl<const N: usize> AsRef<[u8]> for $t<N> {
            fn as_ref(&self) -> &[u8] {
                &self.inner
            }
        }

        impl<const N: usize> AsMut<[u8]> for $t<N> {
            fn as_mut(&mut self) -> &mut [u8] {
                &mut self.inner
            }
        }

        impl<const N: usize> Default for $t<N> {
            fn default() -> Self {
                Self::new()
            }
        }
    };
}