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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use crate::protocol::{AckChunkData, SetChunkData, StartTransferData, TransferId};
use flood_rs::{ReadOctetStream, WriteOctetStream};
use std::io;
use std::io::ErrorKind;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SetChunkFrontData {
    pub transfer_id: TransferId,
    pub data: SetChunkData,
}

impl SetChunkFrontData {
    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    #[allow(clippy::cast_possible_truncation)]
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.transfer_id.to_stream(stream)?;
        self.data.to_stream(stream)?;
        Ok(())
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            transfer_id: TransferId::from_stream(stream)?,
            data: SetChunkData::from_stream(stream)?,
        })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum SenderToReceiverFrontCommands {
    SetChunk(SetChunkFrontData),
    StartTransfer(StartTransferData),
}

#[repr(u8)]
enum SenderToReceiverFrontCommand {
    SetChunk = 0x01,
    StartTransfer = 0x02,
}

impl TryFrom<u8> for SenderToReceiverFrontCommand {
    type Error = io::Error;

    fn try_from(value: u8) -> io::Result<Self> {
        match value {
            0x01 => Ok(Self::SetChunk),
            0x02 => Ok(Self::StartTransfer),
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unknown command {value}"),
            )),
        }
    }
}

impl SenderToReceiverFrontCommands {
    #[must_use]
    pub const fn to_octet(&self) -> u8 {
        match self {
            Self::SetChunk(_) => SenderToReceiverFrontCommand::SetChunk as u8,
            Self::StartTransfer(_) => SenderToReceiverFrontCommand::StartTransfer as u8,
        }
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.

    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.to_octet())?;
        match self {
            Self::SetChunk(set_chunk_header) => set_chunk_header.to_stream(stream),
            Self::StartTransfer(transfer_data) => transfer_data.to_stream(stream),
        }
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let command_value = stream.read_u8()?;
        let command = SenderToReceiverFrontCommand::try_from(command_value)?;
        let x = match command {
            SenderToReceiverFrontCommand::SetChunk => {
                Self::SetChunk(SetChunkFrontData::from_stream(stream)?)
            }
            SenderToReceiverFrontCommand::StartTransfer => {
                Self::StartTransfer(StartTransferData::from_stream(stream)?)
            }
        };
        Ok(x)
    }
}

#[repr(u8)]
enum ReceiverToSenderFrontCommand {
    AckChunk = 0x02,
    AckStart = 0x03,
}

impl TryFrom<u8> for ReceiverToSenderFrontCommand {
    type Error = io::Error;

    fn try_from(value: u8) -> io::Result<Self> {
        match value {
            0x02 => Ok(Self::AckChunk),
            0x03 => Ok(Self::AckStart),
            _ => Err(io::Error::new(
                ErrorKind::InvalidData,
                format!("Unknown command {value}"),
            )),
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AckChunkFrontData {
    pub transfer_id: TransferId,
    pub data: AckChunkData,
}

impl AckChunkFrontData {
    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.transfer_id.to_stream(stream)?;
        self.data.to_stream(stream)
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            transfer_id: TransferId::from_stream(stream)?,
            data: AckChunkData::from_stream(stream)?,
        })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ReceiverToSenderFrontCommands {
    AckChunk(AckChunkFrontData),
    AckStart(u16),
}

impl ReceiverToSenderFrontCommands {
    #[must_use]
    pub const fn to_octet(&self) -> u8 {
        match self {
            Self::AckChunk(_) => ReceiverToSenderFrontCommand::AckChunk as u8,
            Self::AckStart(_) => ReceiverToSenderFrontCommand::AckStart as u8,
        }
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.to_octet())?;
        match self {
            Self::AckChunk(set_chunk_header) => set_chunk_header.to_stream(stream),
            Self::AckStart(transfer_id) => stream.write_u16(*transfer_id),
        }
    }

    /// # Errors
    ///
    /// This function will return an `io::Error` if there is an issue with writing to the stream.
    /// This could happen if the stream is closed or if there are underlying I/O errors during the write operation.
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let command_value = stream.read_u8()?;
        let command = ReceiverToSenderFrontCommand::try_from(command_value)?;
        let x = match command {
            ReceiverToSenderFrontCommand::AckChunk => Self::AckChunk(AckChunkFrontData {
                transfer_id: TransferId::from_stream(stream)?,
                data: AckChunkData::from_stream(stream)?,
            }),
            ReceiverToSenderFrontCommand::AckStart => Self::AckStart(stream.read_u16()?),
        };
        Ok(x)
    }
}