Skip to content

Commit

Permalink
Auto-merge for PR #110 via VersionBot
Browse files Browse the repository at this point in the history
Error handling with error-chain
  • Loading branch information
resin-io-modules-versionbot[bot] authored Feb 15, 2018
2 parents a016f66 + eab4335 commit 10e75ab
Show file tree
Hide file tree
Showing 14 changed files with 369 additions and 222 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.9.0 - 2018-02-15

* Error handling with error-chain #110 [majorz]

## v0.8.6 - 2018-01-10

* Incomplete NM80211ApFlags enumeration definitions #108 [majorz]
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "network-manager"
version = "0.8.6"
version = "0.9.0"
authors = ["Zahari Petkov <[email protected]>", "Aaron Brodersen <[email protected]>"]
description = "Rust NetworkManager bindings"
homepage = "https://github.com/resin-io-modules/network-manager"
Expand All @@ -17,3 +17,7 @@ tokio-timer = "0.1"
bitflags = "0.9"
ascii = "0.8"
log = "0.3"

[dependencies.error-chain]
version = "0.11"
default-features = false
92 changes: 77 additions & 15 deletions examples/create.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,97 @@
#[macro_use]
extern crate error_chain;

extern crate network_manager;

use std::env;
use std::process;
use std::io::Write;

use network_manager::{AccessPoint, Device, DeviceType, NetworkManager};

mod errors {
use network_manager;

error_chain! {
links {
NetworkManager(network_manager::errors::Error, network_manager::errors::ErrorKind);
}

errors {
Runtime(info: String) {
description("Runtime error")
display("{}", info)
}
}
}
}

use network_manager::{DeviceType, NetworkManager};
use errors::*;

fn main() {
if let Err(ref e) = run() {
let stderr = &mut ::std::io::stderr();
let errmsg = "Error writing to stderr";

writeln!(stderr, "{}", e).expect(errmsg);

for e in e.iter().skip(1) {
writeln!(stderr, " caused by: {}", e).expect(errmsg);
}

::std::process::exit(1);
}
}

fn run() -> Result<()> {
let args: Vec<String> = env::args().collect();

if args.len() < 3 {
if args.len() != 3 {
println!("USAGE: create SSID PASSWORD");
process::exit(1);
}

let manager = NetworkManager::new();

let devices = manager.get_devices().unwrap();
let device_index = devices
.iter()
.position(|d| *d.device_type() == DeviceType::WiFi)
.unwrap();
let wifi_device = devices[device_index].as_wifi_device().unwrap();
let device = find_device(&manager)?;

let access_points = wifi_device.get_access_points().unwrap();
let wifi_device = device.as_wifi_device().unwrap();

let ap_index = access_points
let access_points = wifi_device.get_access_points()?;

let ap_index = find_access_point(&access_points, &args[1] as &str)?;

wifi_device.connect(&access_points[ap_index], &args[2] as &str)?;

Ok(())
}

fn find_device(manager: &NetworkManager) -> Result<Device> {
let devices = manager.get_devices()?;

let index = devices
.iter()
.position(|ap| ap.ssid().as_str().unwrap() == args[1])
.unwrap();
.position(|d| *d.device_type() == DeviceType::WiFi);

wifi_device
.connect(&access_points[ap_index], &args[2] as &str)
.unwrap();
if let Some(index) = index {
Ok(devices[index].clone())
} else {
bail!(ErrorKind::Runtime("Cannot find a WiFi device".into()))
}
}

fn find_access_point(access_points: &[AccessPoint], ssid: &str) -> Result<usize> {
if let Some(index) = access_points.iter().position(|ap| same_ssid(ap, ssid)) {
Ok(index)
} else {
bail!(ErrorKind::Runtime("Access point not found".into()))
}
}

fn same_ssid(ap: &AccessPoint, ssid: &str) -> bool {
if let Ok(ap_ssid) = ap.ssid().as_str() {
ap_ssid == ssid
} else {
false
}
}
105 changes: 73 additions & 32 deletions examples/hotspot.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
#[macro_use]
extern crate error_chain;

extern crate network_manager;

use std::env;
use std::process;
use std::io::Write;

use network_manager::{Device, DeviceType, NetworkManager};

mod errors {
use network_manager;

error_chain! {
links {
NetworkManager(network_manager::errors::Error, network_manager::errors::ErrorKind);
}

errors {
Runtime(info: String) {
description("Runtime error")
display("{}", info)
}
}
}
}

use errors::*;

struct Options {
interface: Option<String>,
ssid: String,
password: Option<String>,
}

fn print_usage_and_exit() {
println!("USAGE: hotspot [-i INTERFACE] SSID [PASSWORD]");
process::exit(1);
fn main() {
if let Err(ref e) = run() {
let stderr = &mut ::std::io::stderr();
let errmsg = "Error writing to stderr";

writeln!(stderr, "{}", e).expect(errmsg);

for e in e.iter().skip(1) {
writeln!(stderr, " caused by: {}", e).expect(errmsg);
}

::std::process::exit(1);
}
}

fn run() -> Result<()> {
let Options {
interface,
ssid,
password,
} = parse_options();

let pass_str = match password {
Some(ref s) => Some(s as &str),
None => None,
};

let manager = NetworkManager::new();

let device = find_device(&manager, interface)?;
let wifi_device = device.as_wifi_device().unwrap();

wifi_device.create_hotspot(&ssid as &str, pass_str, None)?;

Ok(())
}

fn parse_options() -> Options {
Expand Down Expand Up @@ -48,48 +103,34 @@ fn parse_options() -> Options {
}
}

fn find_device(manager: &NetworkManager, interface: Option<String>) -> Option<Device> {
fn print_usage_and_exit() {
println!("USAGE: hotspot [-i INTERFACE] SSID [PASSWORD]");
process::exit(1);
}

fn find_device(manager: &NetworkManager, interface: Option<String>) -> Result<Device> {
if let Some(interface) = interface {
let device = manager.get_device_by_interface(&interface).unwrap();
let device = manager.get_device_by_interface(&interface)?;

if *device.device_type() == DeviceType::WiFi {
Some(device)
Ok(device)
} else {
None
bail!(ErrorKind::Runtime(format!(
"{} is not a WiFi device",
interface
)))
}
} else {
let devices = manager.get_devices().unwrap();
let devices = manager.get_devices()?;

let index = devices
.iter()
.position(|d| *d.device_type() == DeviceType::WiFi);

if let Some(index) = index {
Some(devices[index].clone())
Ok(devices[index].clone())
} else {
None
bail!(ErrorKind::Runtime("Cannot find a WiFi device".into()))
}
}
}

fn main() {
let Options {
interface,
ssid,
password,
} = parse_options();

let pass_str = match password {
Some(ref s) => Some(s as &str),
None => None,
};

let manager = NetworkManager::new();

let device = find_device(&manager, interface).unwrap();
let wifi_device = device.as_wifi_device().unwrap();

wifi_device
.create_hotspot(&ssid as &str, pass_str, None)
.unwrap();
}
Loading

0 comments on commit 10e75ab

Please sign in to comment.