From 1c34fe3947aaf8af2d773d59bdebf19e17d78527 Mon Sep 17 00:00:00 2001 From: Thomas Karpiniec Date: Sat, 8 Jun 2024 13:25:04 +1000 Subject: [PATCH 1/1] Split out list and watch implementations --- src/lib.rs | 36 ++++++++++++++++++--------------- src/{imp_mac.rs => list_mac.rs} | 0 src/{imp_win.rs => list_win.rs} | 2 +- src/watch_mac.rs | 10 +++++++++ src/watch_win.rs | 8 ++++++++ 5 files changed, 39 insertions(+), 17 deletions(-) rename src/{imp_mac.rs => list_mac.rs} (100%) rename src/{imp_win.rs => list_win.rs} (98%) create mode 100644 src/watch_mac.rs create mode 100644 src/watch_win.rs diff --git a/src/lib.rs b/src/lib.rs index 9f82092..61e4940 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,14 @@ use std::{ net::{IpAddr, Ipv4Addr, Ipv6Addr}, }; -#[cfg_attr(windows, path = "imp_win.rs")] -#[cfg_attr(target_vendor = "apple", path = "imp_mac.rs")] -mod imp; +#[cfg_attr(windows, path = "list_win.rs")] +#[cfg_attr(target_vendor = "apple", path = "list_mac.rs")] +mod list; + +#[cfg_attr(windows, path = "watch_win.rs")] +#[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")] +mod watch; + mod util; type IfIndex = u32; @@ -40,14 +45,21 @@ pub struct Update { pub diff: UpdateDiff, } -#[derive(Debug, Clone, PartialEq, Eq)] +impl Update { + pub fn diff_from_previous(_prev: &Update) -> UpdateDiff { + // TODO: real calculation + UpdateDiff::default() + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct UpdateDiff { pub added: Vec, pub removed: Vec, pub modified: HashMap, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct InterfaceDiff { pub hw_addr_changed: bool, pub addrs_added: Vec, @@ -56,18 +68,10 @@ pub struct InterfaceDiff { #[derive(Debug, Clone, PartialEq, Eq)] pub enum Error { + // TODO: handle all cases with proper sources Internal, } -pub fn list_interfaces() -> Result, Error> { - imp::list_interfaces() -} +pub use list::list_interfaces; +pub use watch::{watch_interfaces, WatchHandle}; -pub struct WatchHandle; - -pub fn watch_interfaces(callback: F) -> WatchHandle { - // stop current worker thread - // post this into a thread that will use it - drop(callback); - WatchHandle -} diff --git a/src/imp_mac.rs b/src/list_mac.rs similarity index 100% rename from src/imp_mac.rs rename to src/list_mac.rs diff --git a/src/imp_win.rs b/src/list_win.rs similarity index 98% rename from src/imp_win.rs rename to src/list_win.rs index f34b207..e842244 100644 --- a/src/imp_win.rs +++ b/src/list_win.rs @@ -18,7 +18,7 @@ use windows::Win32::Networking::WinSock::{ use crate::{Error, IfIndex, Interface}; -pub(crate) fn list_interfaces() -> Result, Error> { +pub fn list_interfaces() -> Result, Error> { let mut ifs = HashMap::new(); // Microsoft recommends a 15 KB initial buffer let start_size = 15 * 1024; diff --git a/src/watch_mac.rs b/src/watch_mac.rs new file mode 100644 index 0000000..547db48 --- /dev/null +++ b/src/watch_mac.rs @@ -0,0 +1,10 @@ +use crate::Update; + +pub struct WatchHandle; + +pub fn watch_interfaces(callback: F) -> WatchHandle { + // stop current worker thread + // post this into a thread that will use it + drop(callback); + WatchHandle +} diff --git a/src/watch_win.rs b/src/watch_win.rs new file mode 100644 index 0000000..e495e9a --- /dev/null +++ b/src/watch_win.rs @@ -0,0 +1,8 @@ +use crate::Update; + +pub struct WatchHandle; + +pub fn watch_interfaces(callback: F) -> WatchHandle { + drop(callback); + WatchHandle +} -- 2.39.5