Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

Commit 9f7c898

Browse files
author
Simon Bernier St-Pierre
committed
Fix some warnings & cargo fmt
1 parent bc436ea commit 9f7c898

File tree

16 files changed

+237
-158
lines changed

16 files changed

+237
-158
lines changed

examples/add_any_port.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::net::{SocketAddrV4, Ipv4Addr};
1+
use std::net::{Ipv4Addr, SocketAddrV4};
22

33
extern crate igd;
44

@@ -13,15 +13,14 @@ fn main() {
1313
let local_addr = local_addr.parse::<Ipv4Addr>().unwrap();
1414
let local_addr = SocketAddrV4::new(local_addr, 8080u16);
1515

16-
match gateway.add_any_port(igd::PortMappingProtocol::TCP,
17-
local_addr, 60, "add_port example") {
16+
match gateway.add_any_port(igd::PortMappingProtocol::TCP, local_addr, 60, "add_port example") {
1817
Err(ref err) => {
1918
println!("There was an error! {}", err);
20-
},
19+
}
2120
Ok(port) => {
2221
println!("It worked! Got port {}", port);
23-
},
22+
}
2423
}
25-
},
24+
}
2625
}
2726
}

examples/add_port.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::net::{SocketAddrV4, Ipv4Addr};
1+
use std::net::{Ipv4Addr, SocketAddrV4};
22

33
extern crate igd;
44

@@ -13,15 +13,14 @@ fn main() {
1313
let local_addr = local_addr.parse::<Ipv4Addr>().unwrap();
1414
let local_addr = SocketAddrV4::new(local_addr, 8080u16);
1515

16-
match gateway.add_port(igd::PortMappingProtocol::TCP, 80,
17-
local_addr, 60, "add_port example") {
16+
match gateway.add_port(igd::PortMappingProtocol::TCP, 80, local_addr, 60, "add_port example") {
1817
Err(ref err) => {
1918
println!("There was an error! {}", err);
20-
},
19+
}
2120
Ok(()) => {
2221
println!("It worked");
23-
},
22+
}
2423
}
25-
},
24+
}
2625
}
2726
}

examples/add_remove.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::net::SocketAddrV4;
21
use std::env;
2+
use std::net::SocketAddrV4;
33

44
extern crate igd;
55

@@ -21,17 +21,16 @@ fn main() {
2121

2222
let local_addr = SocketAddrV4::new(local_ip, local_port);
2323

24-
match gateway.add_port(igd::PortMappingProtocol::TCP, remote_port,
25-
local_addr, 60, "crust") {
24+
match gateway.add_port(igd::PortMappingProtocol::TCP, remote_port, local_addr, 60, "crust") {
2625
Err(ref err) => println!("{:?}", err),
2726
Ok(()) => {
2827
println!("AddPortMapping successful.");
2928
match gateway.remove_port(igd::PortMappingProtocol::TCP, remote_port) {
3029
Err(ref err) => println!("Error removing: {:?}", err),
3130
Ok(_) => println!("DeletePortMapping successful."),
3231
}
33-
},
32+
}
3433
}
35-
},
34+
}
3635
}
3736
}

examples/aio.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use std::env;
1212
use std::net::SocketAddrV4;
1313

14-
use igd::aio::{search_gateway};
14+
use igd::aio::search_gateway;
1515
use igd::PortMappingProtocol;
16-
use simplelog::{SimpleLogger, LevelFilter, Config as LogConfig};
16+
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
1717

1818
#[tokio::main]
1919
async fn main() {
@@ -33,19 +33,25 @@ async fn main() {
3333

3434
let gateway = match search_gateway(Default::default()).await {
3535
Ok(g) => g,
36-
Err(err) => return println!("Faild to find IGD: {}", err)
36+
Err(err) => return println!("Faild to find IGD: {}", err),
3737
};
3838
let pub_ip = match gateway.get_external_ip().await {
3939
Ok(ip) => ip,
40-
Err(err) => return println!("Failed to get external IP: {}", err)
40+
Err(err) => return println!("Failed to get external IP: {}", err),
4141
};
4242
println!("Our public IP is {}", pub_ip);
43-
if let Err(e) = gateway.add_port(PortMappingProtocol::TCP, 1234, ip, 120, "rust-igd-async-example").await {
43+
if let Err(e) = gateway
44+
.add_port(PortMappingProtocol::TCP, 1234, ip, 120, "rust-igd-async-example")
45+
.await
46+
{
4447
println!("Failed to add port mapping: {}", e);
4548
}
4649
println!("New port mapping was successfully added.");
4750

48-
if let Err(e) = gateway.add_port(PortMappingProtocol::TCP, 2345, ip, 120, "rust-igd-async-example").await {
51+
if let Err(e) = gateway
52+
.add_port(PortMappingProtocol::TCP, 2345, ip, 120, "rust-igd-async-example")
53+
.await
54+
{
4955
println!("Failed to add port mapping: {}", e);
5056
}
5157
println!("New port mapping was successfully added.");

examples/external_ip.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ extern crate igd;
33
fn main() {
44
match igd::search_gateway(Default::default()) {
55
Err(ref err) => println!("Error: {}", err),
6-
Ok(gateway) => {
7-
match gateway.get_external_ip() {
8-
Err(ref err) => {
9-
println!("There was an error! {}", err);
10-
},
11-
Ok(ext_addr) => {
12-
println!("Local gateway: {}, External ip address: {}", gateway, ext_addr);
13-
},
6+
Ok(gateway) => match gateway.get_external_ip() {
7+
Err(ref err) => {
8+
println!("There was an error! {}", err);
9+
}
10+
Ok(ext_addr) => {
11+
println!("Local gateway: {}, External ip address: {}", gateway, ext_addr);
1412
}
1513
},
1614
}

examples/remove_port.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ extern crate igd;
33
fn main() {
44
match igd::search_gateway(Default::default()) {
55
Err(ref err) => println!("Error: {}", err),
6-
Ok(gateway) => {
7-
match gateway.remove_port(igd::PortMappingProtocol::TCP, 80) {
8-
Err(ref err) => {
9-
println!("There was an error! {}", err);
10-
},
11-
Ok(()) => {
12-
println!("It worked");
13-
},
6+
Ok(gateway) => match gateway.remove_port(igd::PortMappingProtocol::TCP, 80) {
7+
Err(ref err) => {
8+
println!("There was an error! {}", err);
9+
}
10+
Ok(()) => {
11+
println!("It worked");
1412
}
1513
},
1614
}

src/aio/gateway.rs

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::net::{Ipv4Addr, SocketAddrV4};
55
use super::soap;
66
use crate::errors::{self, AddAnyPortError, AddPortError, GetExternalIpError, RemovePortError, RequestError};
77

8-
use crate::common::{self, parsing::RequestReponse, messages, parsing};
8+
use crate::common::{self, messages, parsing, parsing::RequestReponse};
99
use crate::PortMappingProtocol;
1010

1111
/// This structure represents a gateway found by the search functions.
@@ -26,12 +26,7 @@ impl Gateway {
2626
}
2727
}
2828

29-
async fn perform_request(
30-
&self,
31-
header: &str,
32-
body: &str,
33-
ok: &str,
34-
) -> Result<RequestReponse, RequestError> {
29+
async fn perform_request(&self, header: &str, body: &str, ok: &str) -> Result<RequestReponse, RequestError> {
3530
let url = format!("{}", self);
3631
let text = soap::send_async(&url, soap::Action::new(header), body).await?;
3732
parsing::parse_response(text, ok)
@@ -44,7 +39,8 @@ impl Gateway {
4439
messages::GET_EXTERNAL_IP_HEADER,
4540
&messages::format_get_external_ip_message(),
4641
"GetExternalIPAddressResponse",
47-
).await;
42+
)
43+
.await;
4844
parsing::parse_get_external_ip_response(result)
4945
}
5046

@@ -66,7 +62,9 @@ impl Gateway {
6662
) -> Result<SocketAddrV4, AddAnyPortError> {
6763
let description = description.to_owned();
6864
let ip = self.get_external_ip().await?;
69-
let port = self.add_any_port(protocol, local_addr, lease_duration, &description).await?;
65+
let port = self
66+
.add_any_port(protocol, local_addr, lease_duration, &description)
67+
.await?;
7068
Ok(SocketAddrV4::new(ip, port))
7169
}
7270

@@ -112,13 +110,16 @@ impl Gateway {
112110
&description,
113111
),
114112
"AddAnyPortMappingResponse",
115-
).await;
113+
)
114+
.await;
116115
match parsing::parse_add_any_port_mapping_response(resp) {
117116
Ok(port) => Ok(port),
118117
Err(None) => {
119118
// The router does not have the AddAnyPortMapping method.
120119
// Fall back to using AddPortMapping with a random port.
121-
gateway.retry_add_random_port_mapping(protocol, local_addr, lease_duration, &description).await
120+
gateway
121+
.retry_add_random_port_mapping(protocol, local_addr, lease_duration, &description)
122+
.await
122123
}
123124
Err(Some(err)) => Err(err),
124125
}
@@ -132,7 +133,10 @@ impl Gateway {
132133
description: &str,
133134
) -> Result<u16, AddAnyPortError> {
134135
for _ in 0u8..20u8 {
135-
match self.add_random_port_mapping(protocol, local_addr, lease_duration, &description).await {
136+
match self
137+
.add_random_port_mapping(protocol, local_addr, lease_duration, &description)
138+
.await
139+
{
136140
Ok(port) => return Ok(port),
137141
Err(AddAnyPortError::NoPortsAvailable) => continue,
138142
e => return e,
@@ -152,14 +156,20 @@ impl Gateway {
152156
let gateway = self.clone();
153157

154158
let external_port = common::random_port();
155-
let res = self.add_port_mapping(protocol, external_port, local_addr, lease_duration, &description).await;
156-
159+
let res = self
160+
.add_port_mapping(protocol, external_port, local_addr, lease_duration, &description)
161+
.await;
162+
157163
match res {
158164
Ok(_) => Ok(external_port),
159165
Err(err) => match parsing::convert_add_random_port_mapping_error(err) {
160166
Some(err) => Err(err),
161-
None => gateway.add_same_port_mapping(protocol, local_addr, lease_duration, &description).await
162-
}
167+
None => {
168+
gateway
169+
.add_same_port_mapping(protocol, local_addr, lease_duration, &description)
170+
.await
171+
}
172+
},
163173
}
164174
}
165175

@@ -171,10 +181,11 @@ impl Gateway {
171181
description: &str,
172182
) -> Result<u16, AddAnyPortError> {
173183
let res = self
174-
.add_port_mapping(protocol, local_addr.port(), local_addr, lease_duration, description).await;
184+
.add_port_mapping(protocol, local_addr.port(), local_addr, lease_duration, description)
185+
.await;
175186
match res {
176187
Ok(_) => Ok(local_addr.port()),
177-
Err(err) => Err(parsing::convert_add_same_port_mapping_error(err))
188+
Err(err) => Err(parsing::convert_add_same_port_mapping_error(err)),
178189
}
179190
}
180191

@@ -186,18 +197,18 @@ impl Gateway {
186197
lease_duration: u32,
187198
description: &str,
188199
) -> Result<(), RequestError> {
189-
self
190-
.perform_request(
191-
messages::ADD_PORT_MAPPING_HEADER,
192-
&messages::format_add_port_mapping_message(
193-
protocol,
194-
external_port,
195-
local_addr,
196-
lease_duration,
197-
description,
198-
),
199-
"AddPortMappingResponse",
200-
).await?;
200+
self.perform_request(
201+
messages::ADD_PORT_MAPPING_HEADER,
202+
&messages::format_add_port_mapping_message(
203+
protocol,
204+
external_port,
205+
local_addr,
206+
lease_duration,
207+
description,
208+
),
209+
"AddPortMappingResponse",
210+
)
211+
.await?;
201212
Ok(())
202213
}
203214

@@ -220,39 +231,43 @@ impl Gateway {
220231
return Err(AddPortError::InternalPortZeroInvalid);
221232
}
222233

223-
let res = self.add_port_mapping(protocol, external_port, local_addr, lease_duration, description).await;
234+
let res = self
235+
.add_port_mapping(protocol, external_port, local_addr, lease_duration, description)
236+
.await;
224237
if let Err(err) = res {
225238
return Err(parsing::convert_add_port_error(err));
226239
};
227240
Ok(())
228241
}
229242

230243
/// Remove a port mapping.
231-
pub async fn remove_port(
232-
&self,
233-
protocol: PortMappingProtocol,
234-
external_port: u16,
235-
) -> Result<(), RemovePortError> {
244+
pub async fn remove_port(&self, protocol: PortMappingProtocol, external_port: u16) -> Result<(), RemovePortError> {
236245
let res = self
237246
.perform_request(
238247
messages::DELETE_PORT_MAPPING_HEADER,
239248
&messages::format_delete_port_message(protocol, external_port),
240249
"DeletePortMappingResponse",
241-
).await;
250+
)
251+
.await;
242252
parsing::parse_delete_port_mapping_response(res)
243253
}
244254

245255
/// Get one port mapping entry
246-
///
256+
///
247257
/// Gets one port mapping entry by its index.
248258
/// Not all existing port mappings might be visible to this client.
249259
/// If the index is out of bound, GetGenericPortMappingEntryError::SpecifiedArrayIndexInvalid will be returned
250-
pub async fn get_generic_port_mapping_entry(&self, index: u32) -> Result<parsing::PortMappingEntry, errors::GetGenericPortMappingEntryError> {
251-
let result = self.perform_request(
252-
messages::GET_GENERIC_PORT_MAPPING_ENTRY,
253-
&messages::formate_get_generic_port_mapping_entry_message(index),
254-
"GetGenericPortMappingEntryResponse"
255-
).await;
260+
pub async fn get_generic_port_mapping_entry(
261+
&self,
262+
index: u32,
263+
) -> Result<parsing::PortMappingEntry, errors::GetGenericPortMappingEntryError> {
264+
let result = self
265+
.perform_request(
266+
messages::GET_GENERIC_PORT_MAPPING_ENTRY,
267+
&messages::formate_get_generic_port_mapping_entry_message(index),
268+
"GetGenericPortMappingEntryResponse",
269+
)
270+
.await;
256271
parsing::parse_get_generic_port_mapping_entry(result)
257272
}
258273
}

0 commit comments

Comments
 (0)