From 7ace1035089ec88e8f5dd0a9a68fea52f39b89ce Mon Sep 17 00:00:00 2001 From: majorz Date: Thu, 1 Jun 2017 13:26:30 +0300 Subject: [PATCH 1/3] Clean up clippy warnings Change-Type: patch --- .travis.yml | 3 -- Cargo.toml | 2 +- examples/create.rs | 4 +- examples/hotspot.rs | 2 +- src/connection.rs | 17 +++--- src/dbus_api.rs | 123 +++++++++++++++++--------------------------- src/dbus_nm.rs | 60 +++++++-------------- src/device.rs | 12 ++--- src/manager.rs | 7 ++- src/service.rs | 4 +- src/ssid.rs | 2 +- src/wifi.rs | 9 ++-- 12 files changed, 94 insertions(+), 151 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6074a62..febd00b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,6 @@ rust: - nightly - beta - stable -matrix: - allow_failures: - - rust: nightly before_install: - sudo apt-get -qq update - sudo apt-get install -yq --no-install-recommends libdbus-1-dev diff --git a/Cargo.toml b/Cargo.toml index 6d067a3..7b37108 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "network_manager" -version = "0.2.3" +version = "0.2.4" authors = ["Joseph Roberts ", "Zahari Petkov "] [dependencies] diff --git a/examples/create.rs b/examples/create.rs index 4ab00af..79c4674 100644 --- a/examples/create.rs +++ b/examples/create.rs @@ -19,7 +19,7 @@ fn main() { let devices = manager.get_devices().unwrap(); let device_index = devices .iter() - .position(|ref d| *d.device_type() == DeviceType::WiFi) + .position(|d| *d.device_type() == DeviceType::WiFi) .unwrap(); let wifi_device = devices[device_index].as_wifi_device().unwrap(); @@ -27,7 +27,7 @@ fn main() { let ap_index = access_points .iter() - .position(|ref ap| ap.ssid().as_str().unwrap() == &args[1]) + .position(|ap| ap.ssid().as_str().unwrap() == args[1]) .unwrap(); wifi_device diff --git a/examples/hotspot.rs b/examples/hotspot.rs index cadee61..9023eeb 100644 --- a/examples/hotspot.rs +++ b/examples/hotspot.rs @@ -66,7 +66,7 @@ fn find_device(manager: &NetworkManager, interface: Option) -> Option Ok(ConnectionState::Activated), ConnectionState::Activating => { - wait(self, ConnectionState::Activated, self.dbus_manager.method_timeout()) + wait(self, &ConnectionState::Activated, self.dbus_manager.method_timeout()) }, ConnectionState::Unknown => Err("Unable to get connection state".to_string()), _ => { self.dbus_manager.activate_connection(&self.path)?; - wait(self, ConnectionState::Activated, self.dbus_manager.method_timeout()) + wait(self, &ConnectionState::Activated, self.dbus_manager.method_timeout()) }, } } @@ -93,7 +93,7 @@ impl Connection { match state { ConnectionState::Deactivated => Ok(ConnectionState::Deactivated), ConnectionState::Deactivating => { - wait(self, ConnectionState::Deactivated, self.dbus_manager.method_timeout()) + wait(self, &ConnectionState::Deactivated, self.dbus_manager.method_timeout()) }, ConnectionState::Unknown => Err("Unable to get connection state".to_string()), _ => { @@ -103,7 +103,7 @@ impl Connection { if let Some(active_path) = active_path_option { self.dbus_manager.deactivate_connection(&active_path)?; - wait(self, ConnectionState::Deactivated, self.dbus_manager.method_timeout()) + wait(self, &ConnectionState::Deactivated, self.dbus_manager.method_timeout()) } else { Ok(ConnectionState::Deactivated) } @@ -180,7 +180,6 @@ pub enum ConnectionState { impl From for ConnectionState { fn from(state: i64) -> Self { match state { - 0 => ConnectionState::Unknown, 1 => ConnectionState::Activating, 2 => ConnectionState::Activated, 3 => ConnectionState::Deactivating, @@ -240,7 +239,7 @@ pub fn connect_to_access_point

( let connection = Connection::init(dbus_manager, &path)?; - let state = wait(&connection, ConnectionState::Activated, dbus_manager.method_timeout())?; + let state = wait(&connection, &ConnectionState::Activated, dbus_manager.method_timeout())?; Ok((connection, state)) } @@ -260,7 +259,7 @@ pub fn create_hotspot( let connection = Connection::init(dbus_manager, &path)?; - let state = wait(&connection, ConnectionState::Activated, dbus_manager.method_timeout())?; + let state = wait(&connection, &ConnectionState::Activated, dbus_manager.method_timeout())?; Ok((connection, state)) } @@ -284,7 +283,7 @@ fn get_connection_active_path( fn wait( connection: &Connection, - target_state: ConnectionState, + target_state: &ConnectionState, timeout: u64, ) -> Result { if timeout == 0 { @@ -302,7 +301,7 @@ fn wait( total_time += 1; - if state == target_state { + if state == *target_state { debug!("Connection target state reached: {:?} / {}s elapsed", state, total_time); return Ok(state); diff --git a/src/dbus_api.rs b/src/dbus_api.rs index 01894f0..6f1e6f0 100644 --- a/src/dbus_api.rs +++ b/src/dbus_api.rs @@ -105,7 +105,7 @@ impl DBusApi { ) -> Option> { match Message::new_method_call(self.base, path, interface, method) { Ok(mut message) => { - if args.len() > 0 { + if !args.is_empty() { message = message.append_ref(args); } @@ -155,7 +155,7 @@ impl DBusApi { match path.get(interface, name) { Ok(variant) => { - match DBusApi::variant_to(variant) { + match DBusApi::variant_to(&variant) { Some(data) => Ok(data), None => property_error("wrong property type"), } @@ -174,7 +174,7 @@ impl DBusApi { { response .get1() - .ok_or("D-Bus wrong response type".to_string()) + .ok_or_else(|| "D-Bus wrong response type".to_string()) } pub fn extract_two<'a, T1, T2>(&self, response: &'a Message) -> Result<(T1, T2), String> @@ -192,7 +192,7 @@ impl DBusApi { Err("D-Bus wrong response type".to_string()) } - fn with_path<'a, P: Into>>(&'a self, path: P) -> ConnPath<'a, &'a DBusConnection> { + fn with_path<'a, P: Into>>(&'a self, path: P) -> ConnPath<&'a DBusConnection> { self.connection .with_path(self.base, path, self.method_timeout as i32 * 1000) } @@ -200,121 +200,90 @@ impl DBusApi { pub trait VariantTo { - fn variant_to(value: Variant>) -> Option; + fn variant_to(value: &Variant>) -> Option; } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_string(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_str().and_then(|v| Some(v.to_string())) } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_i64(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_i64() } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_u32(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_i64().and_then(|v| Some(v as u32)) } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_bool(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_i64().and_then(|v| Some(v == 0)) } } impl VariantTo> for DBusApi { - fn variant_to(value: Variant>) -> Option> { - variant_to_string_vec(value) - } -} - - -impl VariantTo> for DBusApi { - fn variant_to(value: Variant>) -> Option> { - variant_to_u8_vec(value) - } -} - - -fn variant_to_string_vec(value: Variant>) -> Option> { - let mut result = Vec::new(); - - if let Some(list) = value.0.as_iter() { - for element in list { - if let Some(string) = element.as_str() { - result.push(string.to_string()); - } else { - return None; + fn variant_to(value: &Variant>) -> Option> { + let mut result = Vec::new(); + + if let Some(list) = value.0.as_iter() { + for element in list { + if let Some(string) = element.as_str() { + result.push(string.to_string()); + } else { + return None; + } } - } - Some(result) - } else { - None + Some(result) + } else { + None + } } } -fn variant_to_u8_vec(value: Variant>) -> Option> { - let mut result = Vec::new(); - - if let Some(list) = value.0.as_iter() { - for element in list { - if let Some(value) = element.as_i64() { - result.push(value as u8); - } else { - return None; +impl VariantTo> for DBusApi { + fn variant_to(value: &Variant>) -> Option> { + let mut result = Vec::new(); + + if let Some(list) = value.0.as_iter() { + for element in list { + if let Some(value) = element.as_i64() { + result.push(value as u8); + } else { + return None; + } } - } - Some(result) - } else { - None + Some(result) + } else { + None + } } } -fn variant_to_string(value: Variant>) -> Option { - value.0.as_str().and_then(|v| Some(v.to_string())) -} - - -fn variant_to_i64(value: Variant>) -> Option { - value.0.as_i64() -} - - -fn variant_to_u32(value: Variant>) -> Option { - value.0.as_i64().and_then(|v| Some(v as u32)) -} - - -fn variant_to_bool(value: Variant>) -> Option { - value.0.as_i64().and_then(|v| Some(v == 0)) -} - - -pub fn extract<'a, T>(var: &'a Variant) -> Result +pub fn extract<'a, T>(var: &mut Variant>) -> Result where T: Get<'a> { var.0 - .clone() .get::() - .ok_or(format!("D-Bus variant type does not match: {:?}", var)) + .ok_or_else(|| format!("D-Bus variant type does not match: {:?}", var)) } -pub fn variant_iter_to_vec_u8(var: &Variant) -> Result, String> { - let array_option = &var.0.clone().get::>(); +pub fn variant_iter_to_vec_u8(var: &mut Variant) -> Result, String> { + let array_option = &var.0.get::>(); if let Some(array) = *array_option { Ok(array.collect()) diff --git a/src/dbus_nm.rs b/src/dbus_nm.rs index 2f2687d..c96294f 100644 --- a/src/dbus_nm.rs +++ b/src/dbus_nm.rs @@ -118,16 +118,16 @@ impl DBusNetworkManager { let mut ssid = Ssid::new(); for (_, v1) in dict { - for (k2, v2) in v1 { + for (k2, mut v2) in v1 { match k2 { "id" => { - id = extract::(&v2)?; + id = extract::(&mut v2)?; }, "uuid" => { - uuid = extract::(&v2)?; + uuid = extract::(&mut v2)?; }, "ssid" => { - ssid = Ssid::from_bytes(variant_iter_to_vec_u8(&v2)?)?; + ssid = Ssid::from_bytes(variant_iter_to_vec_u8(&mut v2)?)?; }, _ => {}, } @@ -282,7 +282,7 @@ impl DBusNetworkManager { "AddAndActivateConnection", &[ &settings as &RefArg, - &Path::new(device_path.clone())? as &RefArg, + &Path::new(device_path)? as &RefArg, &Path::new("/")? as &RefArg, ], )?; @@ -383,59 +383,39 @@ impl DBusNetworkManager { impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_device_type(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_i64().map(DeviceType::from) } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_device_state(value) + fn variant_to(value: &Variant>) -> Option { + value.0.as_i64().map(DeviceState::from) } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_ap_flags(value) + fn variant_to(value: &Variant>) -> Option { + value + .0 + .as_i64() + .and_then(|v| NM80211ApFlags::from_bits(v as u32)) } } impl VariantTo for DBusApi { - fn variant_to(value: Variant>) -> Option { - variant_to_ap_security_flags(value) + fn variant_to(value: &Variant>) -> Option { + value + .0 + .as_i64() + .and_then(|v| NM80211ApSecurityFlags::from_bits(v as u32)) } } -fn variant_to_device_type(value: Variant>) -> Option { - value.0.as_i64().map(DeviceType::from) -} - - -fn variant_to_device_state(value: Variant>) -> Option { - value.0.as_i64().map(DeviceState::from) -} - - -fn variant_to_ap_flags(value: Variant>) -> Option { - value - .0 - .as_i64() - .and_then(|v| NM80211ApFlags::from_bits(v as u32)) -} - - -fn variant_to_ap_security_flags(value: Variant>) -> Option { - value - .0 - .as_i64() - .and_then(|v| NM80211ApSecurityFlags::from_bits(v as u32)) -} - - pub fn add_val(map: &mut SettingsMap, key: K, value: V) where K: Into, V: RefArg + 'static @@ -450,7 +430,7 @@ pub fn add_str(map: &mut SettingsMap, key: K, value: V) map.insert(key.into(), Variant(Box::new(value.into()))); } -fn verify_password<'a, T: AsAsciiStr + ?Sized>(password: &'a T) -> Result<&'a str, String> { +fn verify_password(password: &T) -> Result<&str, String> { match password.as_ascii_str() { Err(_) => Err("Not an ASCII password".to_string()), Ok(p) => { diff --git a/src/device.rs b/src/device.rs index be4a309..6d6d08f 100644 --- a/src/device.rs +++ b/src/device.rs @@ -42,7 +42,7 @@ impl Device { self.dbus_manager.get_device_state(&self.path) } - pub fn as_wifi_device<'a>(&'a self) -> Option> { + pub fn as_wifi_device(&self) -> Option { if self.device_type == DeviceType::WiFi { Some(new_wifi_device(&self.dbus_manager, self)) } else { @@ -69,7 +69,7 @@ impl Device { _ => { self.dbus_manager.connect_device(&self.path)?; - wait(self, DeviceState::Activated, self.dbus_manager.method_timeout()) + wait(self, &DeviceState::Activated, self.dbus_manager.method_timeout()) }, } } @@ -93,7 +93,7 @@ impl Device { _ => { self.dbus_manager.disconnect_device(&self.path)?; - wait(self, DeviceState::Disconnected, self.dbus_manager.method_timeout()) + wait(self, &DeviceState::Disconnected, self.dbus_manager.method_timeout()) }, } } @@ -133,7 +133,6 @@ pub enum DeviceType { impl From for DeviceType { fn from(state: i64) -> Self { match state { - 0 => DeviceType::Unknown, 14 => DeviceType::Generic, 1 => DeviceType::Ethernet, 2 => DeviceType::WiFi, @@ -159,7 +158,6 @@ pub enum DeviceState { impl From for DeviceState { fn from(state: i64) -> Self { match state { - 0 => DeviceState::Unknown, 10 => DeviceState::Unmanaged, 20 => DeviceState::Unavailable, 30 => DeviceState::Disconnected, @@ -213,7 +211,7 @@ pub fn get_active_connection_devices( Ok(result) } -fn wait(device: &Device, target_state: DeviceState, timeout: u64) -> Result { +fn wait(device: &Device, target_state: &DeviceState, timeout: u64) -> Result { if timeout == 0 { return device.get_state(); } @@ -229,7 +227,7 @@ fn wait(device: &Device, target_state: DeviceState, timeout: u64) -> Result Self { + Self::new() + } +} #[derive(Clone, Debug, PartialEq)] pub enum NetworkManagerState { @@ -132,7 +137,6 @@ pub enum NetworkManagerState { impl From for NetworkManagerState { fn from(state: i64) -> Self { match state { - 0 => NetworkManagerState::Unknown, 10 => NetworkManagerState::Asleep, 20 => NetworkManagerState::Disconnected, 30 => NetworkManagerState::Disconnecting, @@ -159,7 +163,6 @@ pub enum Connectivity { impl From for Connectivity { fn from(state: i64) -> Self { match state { - 0 => Connectivity::Unknown, 1 => Connectivity::None, 2 => Connectivity::Portal, 3 => Connectivity::Limited, diff --git a/src/service.rs b/src/service.rs index 6e61fe0..0d75e39 100644 --- a/src/service.rs +++ b/src/service.rs @@ -151,9 +151,9 @@ fn handler(timeout: u64, target_state: ServiceState) -> Result().ok_or(Error::NotFound)?; + let response = v.0.get::<&str>().ok_or(Error::NotFound)?; let state: ServiceState = response.parse()?; if state == target_state { return Ok(target_state); diff --git a/src/ssid.rs b/src/ssid.rs index 7908adf..1f2ab4b 100644 --- a/src/ssid.rs +++ b/src/ssid.rs @@ -28,7 +28,7 @@ impl Ssid { where B: Into> { Ssid { - vec: mem::transmute(bytes.into()), + vec: bytes.into(), } } } diff --git a/src/wifi.rs b/src/wifi.rs index 07f0baa..64b3fb4 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -159,7 +159,7 @@ pub fn new_wifi_device<'a>( fn get_access_point( manager: &DBusNetworkManager, - path: &String, + path: &str, ) -> Result, String> { if let Some(ssid) = manager.get_access_point_ssid(path) { let strength = manager.get_access_point_strength(path)?; @@ -167,7 +167,7 @@ fn get_access_point( let security = get_access_point_security(manager, path)?; let access_point = AccessPoint { - path: path.clone(), + path: path.to_string(), ssid: ssid, strength: strength, security: security, @@ -180,10 +180,7 @@ fn get_access_point( } -fn get_access_point_security( - manager: &DBusNetworkManager, - path: &String, -) -> Result { +fn get_access_point_security(manager: &DBusNetworkManager, path: &str) -> Result { let flags = manager.get_access_point_flags(path)?; let wpa_flags = manager.get_access_point_wpa_flags(path)?; From 33ba19532afe53bdc02063e62bbded9dcd029925 Mon Sep 17 00:00:00 2001 From: majorz Date: Thu, 1 Jun 2017 16:20:40 +0300 Subject: [PATCH 2/3] `warn!` on undefined NM enums and adding missing device types and states --- src/connection.rs | 6 +++- src/device.rs | 70 +++++++++++++++++++++++++++++++++++++++++------ src/manager.rs | 12 ++++++-- 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 79e6150..1e33b68 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -180,11 +180,15 @@ pub enum ConnectionState { impl From for ConnectionState { fn from(state: i64) -> Self { match state { + 0 => ConnectionState::Unknown, 1 => ConnectionState::Activating, 2 => ConnectionState::Activated, 3 => ConnectionState::Deactivating, 4 => ConnectionState::Deactivated, - _ => ConnectionState::Unknown, + _ => { + warn!("Undefined connection state: {}", state); + ConnectionState::Unknown + }, } } } diff --git a/src/device.rs b/src/device.rs index 6d6d08f..893403a 100644 --- a/src/device.rs +++ b/src/device.rs @@ -124,21 +124,60 @@ impl PathGetter for Device { #[derive(Clone, Debug, PartialEq)] pub enum DeviceType { Unknown, - Generic, Ethernet, WiFi, + Unused1, + Unused2, + Bt, + OlpcMesh, + Wimax, + Modem, + Infiniband, + Bond, + Vlan, + Adsl, Bridge, + Generic, + Team, + Tun, + IpTunnel, + Macvlan, + Vxlan, + Veth, + Macsec, + Dummy, } impl From for DeviceType { - fn from(state: i64) -> Self { - match state { - 14 => DeviceType::Generic, + fn from(device_type: i64) -> Self { + match device_type { + 0 => DeviceType::Unknown, 1 => DeviceType::Ethernet, 2 => DeviceType::WiFi, + 3 => DeviceType::Unused1, + 4 => DeviceType::Unused2, + 5 => DeviceType::Bt, + 6 => DeviceType::OlpcMesh, + 7 => DeviceType::Wimax, + 8 => DeviceType::Modem, + 9 => DeviceType::Infiniband, + 10 => DeviceType::Bond, + 11 => DeviceType::Vlan, + 12 => DeviceType::Adsl, 13 => DeviceType::Bridge, - _ => DeviceType::Unknown, - + 14 => DeviceType::Generic, + 15 => DeviceType::Team, + 16 => DeviceType::Tun, + 17 => DeviceType::IpTunnel, + 18 => DeviceType::Macvlan, + 19 => DeviceType::Vxlan, + 20 => DeviceType::Veth, + 21 => DeviceType::Macsec, + 22 => DeviceType::Dummy, + _ => { + warn!("Undefined device type: {}", device_type); + DeviceType::Unknown + }, } } } @@ -150,6 +189,12 @@ pub enum DeviceState { Unmanaged, Unavailable, Disconnected, + Prepare, + Config, + NeedAuth, + IpConfig, + IpCheck, + Secondaries, Activated, Deactivating, Failed, @@ -158,14 +203,23 @@ pub enum DeviceState { impl From for DeviceState { fn from(state: i64) -> Self { match state { + 0 => DeviceState::Unknown, 10 => DeviceState::Unmanaged, 20 => DeviceState::Unavailable, 30 => DeviceState::Disconnected, + 40 => DeviceState::Prepare, + 50 => DeviceState::Config, + 60 => DeviceState::NeedAuth, + 70 => DeviceState::IpConfig, + 80 => DeviceState::IpCheck, + 90 => DeviceState::Secondaries, 100 => DeviceState::Activated, 110 => DeviceState::Deactivating, 120 => DeviceState::Failed, - _ => DeviceState::Unknown, - + _ => { + warn!("Undefined device state: {}", state); + DeviceState::Unknown + }, } } } diff --git a/src/manager.rs b/src/manager.rs index 1d382e0..4ddf52b 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -137,6 +137,7 @@ pub enum NetworkManagerState { impl From for NetworkManagerState { fn from(state: i64) -> Self { match state { + 0 => NetworkManagerState::Unknown, 10 => NetworkManagerState::Asleep, 20 => NetworkManagerState::Disconnected, 30 => NetworkManagerState::Disconnecting, @@ -144,7 +145,10 @@ impl From for NetworkManagerState { 50 => NetworkManagerState::ConnectedLocal, 60 => NetworkManagerState::ConnectedSite, 70 => NetworkManagerState::ConnectedGlobal, - _ => NetworkManagerState::Unknown, + _ => { + warn!("Undefined Network Manager state: {}", state); + NetworkManagerState::Unknown + }, } } @@ -163,11 +167,15 @@ pub enum Connectivity { impl From for Connectivity { fn from(state: i64) -> Self { match state { + 0 => Connectivity::Unknown, 1 => Connectivity::None, 2 => Connectivity::Portal, 3 => Connectivity::Limited, 4 => Connectivity::Full, - _ => Connectivity::Unknown, + _ => { + warn!("Undefined connectivity state: {}", state); + Connectivity::Unknown + }, } } From 8cedba0fdb0c26496ac0dce6f072e0917b1106a4 Mon Sep 17 00:00:00 2001 From: "resin-io-modules-versionbot[bot]" Date: Thu, 1 Jun 2017 13:32:50 +0000 Subject: [PATCH 3/3] v0.2.4 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c7c687..06316bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to [Semantic Versioning](http://semver.org/). +## v0.2.4 - 2017-06-01 + +* Clean up clippy warnings [majorz] + ## v0.2.3 - 2017-05-31 * Treat clippy warnings as errors [majorz]