Skip to content

Commit

Permalink
cross-platform modules setup
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Mar 1, 2024
1 parent 9256f91 commit 740b181
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::net::{IpAddr, SocketAddr};

use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo, TcpState};

pub use platform::hi_cross;

mod platform;

/// A struct representing a process that is listening on a socket
pub struct Listener {
/// The process ID of the listener process
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::net::IpAddr;
use std::str::FromStr;

use listeners::hi_cross;

fn main() {
let print_title = |title: &str| {
let n_repeat = 40 - title.len() / 2;
Expand All @@ -15,12 +17,13 @@ fn main() {

let ip = IpAddr::from_str(
&std::env::args()
.skip(1)
.next()
.nth(1)
.expect("Expected IP address as argument to program"),
)
.expect("The provided IP address is not valid");

hi_cross();

print_title(&format!("get_for_nullnet({ip})"));
for pname in listeners::get_for_nullnet(ip) {
println!("{pname}");
Expand Down
5 changes: 5 additions & 0 deletions src/platform/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::platform::hi;

pub fn hi_cross() {
hi();
}
3 changes: 3 additions & 0 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) fn hi() {
println!("Hi from Linux");
}
3 changes: 3 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) fn hi() {
println!("Hi from macOS");
}
20 changes: 20 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod common;
pub use common::hi_cross;

/* windows */
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
use windows::hi;

/* macos */
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
use macos::hi;

/* linux */
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux::hi;
3 changes: 3 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) fn hi() {
println!("Hi from Windows");
}

0 comments on commit 740b181

Please sign in to comment.