Skip to content

Commit 51f5dbb

Browse files
author
luozijun
committed
fmt
1 parent d57eef8 commit 51f5dbb

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

system-configuration/examples/network_configuration.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ fn main() {
1212

1313
let service = SCNetworkService::global(&store).unwrap();
1414
println!("Global Service:\n{:?}\n", service);
15-
println!(
16-
"Global Interface:\n{:?}\n",
17-
service.interface()
18-
);
15+
println!("Global Interface:\n{:?}\n", service.interface());
1916
println!("Global Service Router:\n{:?}\n", global_router(&store));
2017

2118
println!("\n-listnetworkserviceorder:");

system-configuration/examples/set_dns.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate system_configuration;
22

33
use system_configuration::dynamic_store::SCDynamicStoreBuilder;
4-
use system_configuration::network_configuration::{SCNetworkService};
4+
use system_configuration::network_configuration::SCNetworkService;
55

66
use std::net::{IpAddr, Ipv4Addr};
77

@@ -22,7 +22,9 @@ fn main() {
2222
let store = SCDynamicStoreBuilder::new("session_name").build();
2323

2424
let global_service = SCNetworkService::global(&store).expect("No PrimaryService active");
25-
let global_interface = global_service.interface().expect("No PrimaryInterface active");
25+
let global_interface = global_service
26+
.interface()
27+
.expect("No PrimaryInterface active");
2628

2729
println!("Global Service:");
2830
println!("\tid: {:?}", global_service.id());
@@ -36,9 +38,12 @@ fn main() {
3638
addrs,
3739
global_service.name()
3840
);
39-
4041

41-
println!("Success: {:?}", global_service.set_dns_server_addresses(&store, Some(addrs) ));
42+
43+
println!(
44+
"Success: {:?}",
45+
global_service.set_dns_server_addresses(&store, Some(addrs))
46+
);
4247

4348
// Check
4449
// networksetup -getdnsservers "Wi-Fi"

system-configuration/src/network_configuration.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use core_foundation::array::CFArray;
1616
use core_foundation::base::{CFType, TCFType};
1717
use core_foundation::base::kCFAllocatorDefault;
1818
use core_foundation::dictionary::CFDictionary;
19-
use core_foundation::string::{CFString};
19+
use core_foundation::string::CFString;
2020

21-
use dynamic_store::{SCDynamicStore};
21+
use dynamic_store::SCDynamicStore;
2222
pub use system_configuration_sys::network_configuration::*;
2323
use system_configuration_sys::preferences::SCPreferencesCreate;
2424

@@ -59,7 +59,6 @@ pub struct DnsSetting {
5959

6060
fn global_query(store: &SCDynamicStore, key: &str) -> Option<String> {
6161
let path = CFString::from_static_string("State:/Network/Global/IPv4");
62-
6362
if let Some(value) = store.get(path.clone()) {
6463
if let Some(dict) = value.downcast_into::<CFDictionary>() {
6564
if let Some(val) = dict.find2(&CFString::new(key)) {
@@ -77,7 +76,6 @@ fn global_query(store: &SCDynamicStore, key: &str) -> Option<String> {
7776

7877
/// Returns default route on primary network service.
7978
pub fn global_router(store: &SCDynamicStore) -> Option<IpAddr> {
80-
// let store = SCDynamicStoreBuilder::new(session_name).build();
8179
if let Some(router_str) = global_query(store, "Router") {
8280
if let Ok(router_ip) = router_str.parse::<IpAddr>() {
8381
return Some(router_ip);
@@ -103,7 +101,6 @@ impl_TCFType!(
103101
impl SCNetworkService {
104102
/// Returns primary network service
105103
pub fn global(store: &SCDynamicStore) -> Option<Self> {
106-
// let store = SCDynamicStoreBuilder::new(session_name).build();
107104
if let Some(service_id) = global_query(store, "PrimaryService") {
108105
for service in SCNetworkService::list() {
109106
if service.id() == service_id {
@@ -128,9 +125,10 @@ impl SCNetworkService {
128125
let array: CFArray<CFType> =
129126
unsafe { CFArray::wrap_under_get_rule(SCNetworkServiceCopyAll(prefs)) };
130127

131-
array.iter()
132-
.map(|item| item.downcast::<SCNetworkService>().unwrap())
133-
.collect::<Vec<SCNetworkService>>()
128+
array
129+
.iter()
130+
.map(|item| item.downcast::<SCNetworkService>().unwrap())
131+
.collect::<Vec<SCNetworkService>>()
134132
}
135133

136134
/// Returns the user-specified ordering of network services within the specified set.
@@ -152,7 +150,12 @@ impl SCNetworkService {
152150

153151
for item in array.iter() {
154152
if let Some(id) = item.downcast::<CFString>() {
155-
let service_ref = unsafe { CFType::wrap_under_get_rule(SCNetworkServiceCopy(prefs, id.as_concrete_TypeRef() )) };
153+
let service_ref = unsafe {
154+
CFType::wrap_under_get_rule(SCNetworkServiceCopy(
155+
prefs,
156+
id.as_concrete_TypeRef(),
157+
))
158+
};
156159
if let Some(serv) = service_ref.downcast::<SCNetworkService>() {
157160
services.push(serv);
158161
}
@@ -180,7 +183,6 @@ impl SCNetworkService {
180183

181184
/// Returns the DNS infomation on this network service
182185
pub fn dns(&self, store: &SCDynamicStore) -> SCNetworkServiceDns {
183-
184186
let query = |path: String| -> DnsSetting {
185187
let mut dns_domain_name: Option<String> = None;
186188
let mut dns_server_addresses: Option<Vec<IpAddr>> = None;
@@ -224,17 +226,15 @@ impl SCNetworkService {
224226
}
225227
};
226228

227-
let state_dns_setting =
228-
query(format!("State:/Network/Service/{}/DNS", self.id()));
229-
let setup_dns_setting =
230-
query(format!("Setup:/Network/Service/{}/DNS", self.id()));
229+
let state_dns_setting = query(format!("State:/Network/Service/{}/DNS", self.id()));
230+
let setup_dns_setting = query(format!("Setup:/Network/Service/{}/DNS", self.id()));
231231

232232
SCNetworkServiceDns {
233233
state: state_dns_setting,
234-
setup: setup_dns_setting
234+
setup: setup_dns_setting,
235235
}
236236
}
237-
237+
238238
/// Setting DNS Domain Name on this network service
239239
pub fn set_dns_domain_name(&self, store: &SCDynamicStore, domain_name: Option<String>) -> bool {
240240
let key = CFString::from_static_string("DomainName");
@@ -247,11 +247,18 @@ impl SCNetworkService {
247247
}
248248

249249
/// Setting DNS Server Addresses on this network service
250-
pub fn set_dns_server_addresses(&self, store: &SCDynamicStore, server_addrs: Option<Vec<IpAddr>>) -> bool {
250+
pub fn set_dns_server_addresses(
251+
&self,
252+
store: &SCDynamicStore,
253+
server_addrs: Option<Vec<IpAddr>>,
254+
) -> bool {
251255
let key = CFString::from_static_string("ServerAddresses");
252256
let addrs: Vec<CFString> = match server_addrs {
253-
Some(addrs) => addrs.iter().map(|s| CFString::new(&format!("{}", s))).collect(),
254-
None => vec![ CFString::new("Empty") ]
257+
Some(addrs) => addrs
258+
.iter()
259+
.map(|s| CFString::new(&format!("{}", s)))
260+
.collect(),
261+
None => vec![CFString::new("Empty")],
255262
};
256263
let value = CFArray::from_CFTypes(&addrs);
257264
let dictionary = CFDictionary::from_CFType_pairs(&[(key, value)]);
@@ -263,7 +270,8 @@ impl SCNetworkService {
263270

264271
/// Returns the network interface associated with this network service.
265272
pub fn interface(&self) -> Option<SCNetworkInterface> {
266-
let interface_ref = unsafe { CFType::wrap_under_get_rule(SCNetworkServiceGetInterface(self.0)) };
273+
let interface_ref =
274+
unsafe { CFType::wrap_under_get_rule(SCNetworkServiceGetInterface(self.0)) };
267275
interface_ref.downcast::<SCNetworkInterface>()
268276
}
269277
}
@@ -306,9 +314,10 @@ impl SCNetworkInterface {
306314
let array: CFArray<CFType> =
307315
unsafe { CFArray::wrap_under_get_rule(SCNetworkInterfaceCopyAll()) };
308316

309-
array.iter()
310-
.map(|item| item.downcast::<SCNetworkInterface>().unwrap() )
311-
.collect::<Vec<SCNetworkInterface>>()
317+
array
318+
.iter()
319+
.map(|item| item.downcast::<SCNetworkInterface>().unwrap())
320+
.collect::<Vec<SCNetworkInterface>>()
312321
}
313322

314323
/// Returns the current MTU setting and the range of allowable values

0 commit comments

Comments
 (0)