Skip to content

Commit 128f482

Browse files
committed
chore: Remove num-traits dep
1 parent 7701be2 commit 128f482

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ repository = "https://github.com/oblique/async-tftp-rs"
1616
bytes = "1.5.0"
1717
log = "0.4.20"
1818
nom = "7.1.3"
19-
num-derive = "0.3.3"
20-
num-traits = "0.2.16"
2119
thiserror = "1.0.48"
2220

2321
async-executor = "1.5.1"

src/packet.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
///! Packet definitions.
22
use bytes::{BufMut, Bytes, BytesMut};
3-
use num_derive::FromPrimitive;
43
use std::convert::From;
54
use std::io;
65
use std::str;
@@ -10,7 +9,7 @@ use crate::parse::*;
109

1110
pub(crate) const PACKET_DATA_HEADER_LEN: usize = 4;
1211

13-
#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive)]
12+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1413
#[repr(u16)]
1514
pub(crate) enum PacketType {
1615
Rrq = 1,
@@ -66,6 +65,26 @@ pub(crate) struct Opts {
6665
pub transfer_size: Option<u64>,
6766
}
6867

68+
impl PacketType {
69+
pub(crate) fn from_u16(n: u16) -> Option<PacketType> {
70+
match n {
71+
1 => Some(PacketType::Rrq),
72+
2 => Some(PacketType::Wrq),
73+
3 => Some(PacketType::Data),
74+
4 => Some(PacketType::Ack),
75+
5 => Some(PacketType::Error),
76+
6 => Some(PacketType::OAck),
77+
_ => None,
78+
}
79+
}
80+
}
81+
82+
impl From<PacketType> for u16 {
83+
fn from(value: PacketType) -> Self {
84+
value as u16
85+
}
86+
}
87+
6988
impl<'a> Packet<'a> {
7089
pub(crate) fn decode(data: &[u8]) -> Result<Packet> {
7190
parse_packet(data)
@@ -74,45 +93,45 @@ impl<'a> Packet<'a> {
7493
pub(crate) fn encode(&self, buf: &mut BytesMut) {
7594
match self {
7695
Packet::Rrq(req) => {
77-
buf.put_u16(PacketType::Rrq as u16);
96+
buf.put_u16(PacketType::Rrq.into());
7897
buf.put_slice(req.filename.as_bytes());
7998
buf.put_u8(0);
8099
buf.put_slice(req.mode.to_str().as_bytes());
81100
buf.put_u8(0);
82101
req.opts.encode(buf);
83102
}
84103
Packet::Wrq(req) => {
85-
buf.put_u16(PacketType::Wrq as u16);
104+
buf.put_u16(PacketType::Wrq.into());
86105
buf.put_slice(req.filename.as_bytes());
87106
buf.put_u8(0);
88107
buf.put_slice(req.mode.to_str().as_bytes());
89108
buf.put_u8(0);
90109
req.opts.encode(buf);
91110
}
92111
Packet::Data(block, data) => {
93-
buf.put_u16(PacketType::Data as u16);
112+
buf.put_u16(PacketType::Data.into());
94113
buf.put_u16(*block);
95114
buf.put_slice(data);
96115
}
97116
Packet::Ack(block) => {
98-
buf.put_u16(PacketType::Ack as u16);
117+
buf.put_u16(PacketType::Ack.into());
99118
buf.put_u16(*block);
100119
}
101120
Packet::Error(error) => {
102-
buf.put_u16(PacketType::Error as u16);
121+
buf.put_u16(PacketType::Error.into());
103122
buf.put_u16(error.code());
104123
buf.put_slice(error.msg().as_bytes());
105124
buf.put_u8(0);
106125
}
107126
Packet::OAck(opts) => {
108-
buf.put_u16(PacketType::OAck as u16);
127+
buf.put_u16(PacketType::OAck.into());
109128
opts.encode(buf);
110129
}
111130
}
112131
}
113132

114133
pub(crate) fn encode_data_head(block_id: u16, buf: &mut BytesMut) {
115-
buf.put_u16(PacketType::Data as u16);
134+
buf.put_u16(PacketType::Data.into());
116135
buf.put_u16(block_id);
117136
}
118137

src/parse.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use nom::multi::many0;
55
use nom::number::complete::be_u16;
66
use nom::sequence::tuple;
77
use nom::IResult;
8-
use num_traits::FromPrimitive;
98
use std::str::{self, FromStr};
109

1110
use crate::error::Result;

0 commit comments

Comments
 (0)