Skip to content

Commit

Permalink
chore: Remove num-traits dep
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Sep 17, 2023
1 parent 7701be2 commit 128f482
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ repository = "https://github.com/oblique/async-tftp-rs"
bytes = "1.5.0"
log = "0.4.20"
nom = "7.1.3"
num-derive = "0.3.3"
num-traits = "0.2.16"
thiserror = "1.0.48"

async-executor = "1.5.1"
Expand Down
37 changes: 28 additions & 9 deletions src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
///! Packet definitions.

Check failure on line 1 in src/packet.rs

View workflow job for this annotation

GitHub Actions / Lints

this is an outer doc comment and does not apply to the parent module or crate
use bytes::{BufMut, Bytes, BytesMut};
use num_derive::FromPrimitive;
use std::convert::From;
use std::io;
use std::str;
Expand All @@ -10,7 +9,7 @@ use crate::parse::*;

pub(crate) const PACKET_DATA_HEADER_LEN: usize = 4;

#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub(crate) enum PacketType {
Rrq = 1,
Expand Down Expand Up @@ -66,6 +65,26 @@ pub(crate) struct Opts {
pub transfer_size: Option<u64>,
}

impl PacketType {
pub(crate) fn from_u16(n: u16) -> Option<PacketType> {
match n {
1 => Some(PacketType::Rrq),
2 => Some(PacketType::Wrq),
3 => Some(PacketType::Data),
4 => Some(PacketType::Ack),
5 => Some(PacketType::Error),
6 => Some(PacketType::OAck),
_ => None,
}
}
}

impl From<PacketType> for u16 {
fn from(value: PacketType) -> Self {
value as u16
}
}

impl<'a> Packet<'a> {
pub(crate) fn decode(data: &[u8]) -> Result<Packet> {
parse_packet(data)
Expand All @@ -74,45 +93,45 @@ impl<'a> Packet<'a> {
pub(crate) fn encode(&self, buf: &mut BytesMut) {
match self {
Packet::Rrq(req) => {
buf.put_u16(PacketType::Rrq as u16);
buf.put_u16(PacketType::Rrq.into());
buf.put_slice(req.filename.as_bytes());
buf.put_u8(0);
buf.put_slice(req.mode.to_str().as_bytes());
buf.put_u8(0);
req.opts.encode(buf);
}
Packet::Wrq(req) => {
buf.put_u16(PacketType::Wrq as u16);
buf.put_u16(PacketType::Wrq.into());
buf.put_slice(req.filename.as_bytes());
buf.put_u8(0);
buf.put_slice(req.mode.to_str().as_bytes());
buf.put_u8(0);
req.opts.encode(buf);
}
Packet::Data(block, data) => {
buf.put_u16(PacketType::Data as u16);
buf.put_u16(PacketType::Data.into());
buf.put_u16(*block);
buf.put_slice(data);
}
Packet::Ack(block) => {
buf.put_u16(PacketType::Ack as u16);
buf.put_u16(PacketType::Ack.into());
buf.put_u16(*block);
}
Packet::Error(error) => {
buf.put_u16(PacketType::Error as u16);
buf.put_u16(PacketType::Error.into());
buf.put_u16(error.code());
buf.put_slice(error.msg().as_bytes());
buf.put_u8(0);
}
Packet::OAck(opts) => {
buf.put_u16(PacketType::OAck as u16);
buf.put_u16(PacketType::OAck.into());
opts.encode(buf);
}
}
}

pub(crate) fn encode_data_head(block_id: u16, buf: &mut BytesMut) {
buf.put_u16(PacketType::Data as u16);
buf.put_u16(PacketType::Data.into());
buf.put_u16(block_id);
}

Expand Down
1 change: 0 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use nom::multi::many0;
use nom::number::complete::be_u16;
use nom::sequence::tuple;
use nom::IResult;
use num_traits::FromPrimitive;
use std::str::{self, FromStr};

use crate::error::Result;
Expand Down

0 comments on commit 128f482

Please sign in to comment.