From b2305156759ae174d6b923e03993b73fbd30fe7d Mon Sep 17 00:00:00 2001 From: Valmo Trindade Date: Wed, 16 Apr 2025 16:51:21 -0300 Subject: [PATCH] moved rust util functions to use folder structure --- src/lib.rs | 8 ++++---- src/util.rs | 43 ------------------------------------------- src/utils/address.rs | 23 +++++++++++++++++++++++ src/utils/log.rs | 12 ++++++++++++ src/utils/mod.rs | 3 +++ src/utils/uuid.rs | 7 +++++++ 6 files changed, 49 insertions(+), 47 deletions(-) delete mode 100644 src/util.rs create mode 100644 src/utils/address.rs create mode 100644 src/utils/log.rs create mode 100644 src/utils/mod.rs create mode 100644 src/utils/uuid.rs diff --git a/src/lib.rs b/src/lib.rs index e81836e..0679636 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,10 @@ use arma_rs::{arma, Extension, Group}; mod structs; mod tests; mod websocket; -mod util; mod cot_router; mod cot_generator; mod video_stream; +mod utils; #[arma] pub fn init() -> Extension { @@ -37,9 +37,9 @@ pub fn init() -> Extension { .command("message", websocket::message) .command("location", websocket::location) ) - .command("local_ip", util::get_local_address) - .command("uuid", util::get_uuid) - .command("log", util::log_info) + .command("local_ip", utils::address::get_local_address) + .command("uuid", utils::uuid::get_uuid) + .command("log", utils::log::log_info) .group( "cot_router", Group::new() diff --git a/src/util.rs b/src/util.rs deleted file mode 100644 index e5f51d0..0000000 --- a/src/util.rs +++ /dev/null @@ -1,43 +0,0 @@ -use crate::structs::LogPayload; -use log::{error, info, warn}; -use std::net::{IpAddr, UdpSocket}; - -pub fn log_info(data: LogPayload) -> String { - match data.status.as_str() { - "info" => info!("{}", data.message), - "warn" => warn!("{}", data.message), - "error" => error!("{}", data.message), - _ => error!("{}", "Wrong log call"), - } - "logged".to_string() -} - -pub fn get_uuid() -> String { - use uuid::Uuid; - - let id = Uuid::new_v4().to_string(); - - return id; -} - -pub fn get_local_address() -> String { - fn get_local_ip() -> Result { - let socket = UdpSocket::bind("0.0.0.0:0").map_err(|e| e.to_string())?; - socket.connect("8.8.8.8:80").map_err(|e| e.to_string())?; - socket - .local_addr() - .map(|addr| addr.ip()) - .map_err(|e| e.to_string()) - } - - let parsed_data = get_local_ip(); - - match parsed_data { - Ok(ip) => { - return format!("ws://{}:4152", ip.to_string()); - } - Err(_) => { - return "not provided".to_string(); - } - } -} diff --git a/src/utils/address.rs b/src/utils/address.rs new file mode 100644 index 0000000..86e1c25 --- /dev/null +++ b/src/utils/address.rs @@ -0,0 +1,23 @@ +use std::net::{IpAddr, UdpSocket}; + +pub fn get_local_address() -> String { + fn get_local_ip() -> Result { + let socket = UdpSocket::bind("0.0.0.0:0").map_err(|e| e.to_string())?; + socket.connect("8.8.8.8:80").map_err(|e| e.to_string())?; + socket + .local_addr() + .map(|addr| addr.ip()) + .map_err(|e| e.to_string()) + } + + let parsed_data = get_local_ip(); + + match parsed_data { + Ok(ip) => { + return format!("ws://{}:4152", ip.to_string()); + } + Err(_) => { + return "not provided".to_string(); + } + } +} diff --git a/src/utils/log.rs b/src/utils/log.rs new file mode 100644 index 0000000..a95194c --- /dev/null +++ b/src/utils/log.rs @@ -0,0 +1,12 @@ +use crate::structs::LogPayload; +use log::{error, info, warn}; + +pub fn log_info(data: LogPayload) -> String { + match data.status.as_str() { + "info" => info!("{}", data.message), + "warn" => warn!("{}", data.message), + "error" => error!("{}", data.message), + _ => error!("{}", "Wrong log call"), + } + "logged".to_string() +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..b378494 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,3 @@ +pub mod uuid; +pub mod address; +pub mod log; \ No newline at end of file diff --git a/src/utils/uuid.rs b/src/utils/uuid.rs new file mode 100644 index 0000000..8f57de8 --- /dev/null +++ b/src/utils/uuid.rs @@ -0,0 +1,7 @@ +pub fn get_uuid() -> String { + use uuid::Uuid; + + let id = Uuid::new_v4().to_string(); + + return id; +}