aboutsummaryrefslogtreecommitdiff
path: root/src/data_type.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/data_type.rs')
-rw-r--r--src/data_type.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/data_type.rs b/src/data_type.rs
new file mode 100644
index 0000000..43a7f1a
--- /dev/null
+++ b/src/data_type.rs
@@ -0,0 +1,42 @@
+use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+
+pub trait DataType {
+ const TYPE: u32;
+ const LEN: u32;
+
+ fn data(&self) -> Vec<u8>;
+}
+
+impl DataType for Ipv4Addr {
+ const TYPE: u32 = 7;
+ const LEN: u32 = 4;
+
+ fn data(&self) -> Vec<u8> {
+ self.octets().to_vec()
+ }
+}
+
+impl DataType for Ipv6Addr {
+ const TYPE: u32 = 8;
+ const LEN: u32 = 16;
+
+ fn data(&self) -> Vec<u8> {
+ self.octets().to_vec()
+ }
+}
+
+impl<const N: usize> DataType for [u8; N] {
+ const TYPE: u32 = 5;
+ const LEN: u32 = N as u32;
+
+ fn data(&self) -> Vec<u8> {
+ self.to_vec()
+ }
+}
+
+pub fn ip_to_vec(ip: IpAddr) -> Vec<u8> {
+ match ip {
+ IpAddr::V4(x) => x.octets().to_vec(),
+ IpAddr::V6(x) => x.octets().to_vec(),
+ }
+}