1use std::net::{Ipv4Addr, Ipv6Addr};
4
5use bytes::{Buf, BufMut};
6use thiserror::Error;
7
8use crate::VarInt;
9
10#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
12#[error("unexpected end of buffer")]
13pub struct UnexpectedEnd;
14
15pub type Result<T> = ::std::result::Result<T, UnexpectedEnd>;
17
18pub trait Encodable {
20 fn encode<B: BufMut>(&self, buf: &mut B);
22}
23
24pub trait Decodable: Sized {
26 fn decode<B: Buf>(buf: &mut B) -> Result<Self>;
28}
29
30impl Decodable for u8 {
31 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
32 if buf.remaining() < 1 {
33 return Err(UnexpectedEnd);
34 }
35 Ok(buf.get_u8())
36 }
37}
38
39impl Encodable for u8 {
40 fn encode<B: BufMut>(&self, buf: &mut B) {
41 buf.put_u8(*self);
42 }
43}
44
45impl Decodable for u16 {
46 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
47 if buf.remaining() < 2 {
48 return Err(UnexpectedEnd);
49 }
50 Ok(buf.get_u16())
51 }
52}
53
54impl Encodable for u16 {
55 fn encode<B: BufMut>(&self, buf: &mut B) {
56 buf.put_u16(*self);
57 }
58}
59
60impl Decodable for u32 {
61 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
62 if buf.remaining() < 4 {
63 return Err(UnexpectedEnd);
64 }
65 Ok(buf.get_u32())
66 }
67}
68
69impl Encodable for u32 {
70 fn encode<B: BufMut>(&self, buf: &mut B) {
71 buf.put_u32(*self);
72 }
73}
74
75impl Decodable for u64 {
76 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
77 if buf.remaining() < 8 {
78 return Err(UnexpectedEnd);
79 }
80 Ok(buf.get_u64())
81 }
82}
83
84impl Encodable for u64 {
85 fn encode<B: BufMut>(&self, buf: &mut B) {
86 buf.put_u64(*self);
87 }
88}
89
90impl Decodable for Ipv4Addr {
91 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
92 if buf.remaining() < 4 {
93 return Err(UnexpectedEnd);
94 }
95 let mut octets = [0; 4];
96 buf.copy_to_slice(&mut octets);
97 Ok(octets.into())
98 }
99}
100
101impl Encodable for Ipv4Addr {
102 fn encode<B: BufMut>(&self, buf: &mut B) {
103 buf.put_slice(&self.octets());
104 }
105}
106
107impl Decodable for Ipv6Addr {
108 fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
109 if buf.remaining() < 16 {
110 return Err(UnexpectedEnd);
111 }
112 let mut octets = [0; 16];
113 buf.copy_to_slice(&mut octets);
114 Ok(octets.into())
115 }
116}
117
118impl Encodable for Ipv6Addr {
119 fn encode<B: BufMut>(&self, buf: &mut B) {
120 buf.put_slice(&self.octets());
121 }
122}
123
124pub(crate) trait BufExt {
125 fn get<T: Decodable>(&mut self) -> Result<T>;
126 fn get_var(&mut self) -> Result<u64>;
127}
128
129impl<T: Buf> BufExt for T {
130 fn get<U: Decodable>(&mut self) -> Result<U> {
131 U::decode(self)
132 }
133
134 fn get_var(&mut self) -> Result<u64> {
135 Ok(VarInt::decode(self)?.into_inner())
136 }
137}
138
139pub(crate) trait BufMutExt {
140 fn write<T: Encodable>(&mut self, x: T);
141 fn write_var(&mut self, x: u64);
142}
143
144impl<T: BufMut> BufMutExt for T {
145 fn write<U: Encodable>(&mut self, x: U) {
146 x.encode(self);
147 }
148
149 fn write_var(&mut self, x: u64) {
150 VarInt::from_u64(x).unwrap().encode(self);
151 }
152}