Skip to content

Commit

Permalink
Apply rustfmt style globally
Browse files Browse the repository at this point in the history
This applies the current rustfmt style across all source code.

Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
  • Loading branch information
otavio committed Jan 5, 2019
1 parent 32b789d commit 8e28056
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 57 deletions.
10 changes: 6 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{App, Arg};

use std::env;
use std::ffi::OsStr;
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::path::PathBuf;
use std::ffi::OsStr;
use std::str::FromStr;

const DEFAULT_GATEWAY: &str = "192.168.42.1";
const DEFAULT_DHCP_RANGE: &str = "192.168.42.2,192.168.42.254";
Expand Down Expand Up @@ -129,7 +129,8 @@ pub fn get_config() -> Config {
let gateway = Ipv4Addr::from_str(&matches.value_of("portal-gateway").map_or_else(
|| env::var("PORTAL_GATEWAY").unwrap_or_else(|_| DEFAULT_GATEWAY.to_string()),
String::from,
)).expect("Cannot parse gateway address");
))
.expect("Cannot parse gateway address");

let dhcp_range = matches.value_of("portal-dhcp-range").map_or_else(
|| env::var("PORTAL_DHCP_RANGE").unwrap_or_else(|_| DEFAULT_DHCP_RANGE.to_string()),
Expand All @@ -151,7 +152,8 @@ pub fn get_config() -> Config {
let activity_timeout = u64::from_str(&matches.value_of("activity-timeout").map_or_else(
|| env::var("ACTIVITY_TIMEOUT").unwrap_or_else(|_| DEFAULT_ACTIVITY_TIMEOUT.to_string()),
String::from,
)).expect("Cannot parse activity timeout");
))
.expect("Cannot parse activity timeout");

let ui_directory = get_ui_directory(matches.value_of("ui-directory"));

Expand Down
2 changes: 1 addition & 1 deletion src/dnsmasq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::process::{Child, Command};

use network_manager::Device;

use errors::*;
use config::Config;
use errors::*;

pub fn start_dnsmasq(config: &Config, device: &Device) -> Result<Child> {
let args = [
Expand Down
4 changes: 2 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use log::{LogLevel, LogLevelFilter, LogRecord};
use env_logger::LogBuilder;
use log::{LogLevel, LogLevelFilter, LogRecord};
use std::env;

pub fn init() {
let mut builder = LogBuilder::new();
Expand Down
24 changes: 13 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ extern crate router;
extern crate serde_json;
extern crate staticfile;

mod errors;
mod config;
mod network;
mod server;
mod dnsmasq;
mod logger;
mod errors;
mod exit;
mod logger;
mod network;
mod privileges;
mod server;

use std::path;
use std::thread;
use std::sync::mpsc::channel;
use std::io::Write;
use std::path;
use std::process;
use std::sync::mpsc::channel;
use std::thread;

use errors::*;
use config::get_config;
use network::{init_networking, process_network_commands};
use errors::*;
use exit::block_exit_signals;
use network::{init_networking, process_network_commands};
use privileges::require_root;

fn main() {
Expand Down Expand Up @@ -78,8 +78,10 @@ fn run() -> Result<()> {
});

match exit_rx.recv() {
Ok(result) => if let Err(reason) = result {
return Err(reason);
Ok(result) => {
if let Err(reason) = result {
return Err(reason);
}
},
Err(e) => {
return Err(e.into());
Expand Down
21 changes: 12 additions & 9 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::thread;
use std::process;
use std::time::Duration;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::error::Error;
use std::net::Ipv4Addr;
use std::process;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use std::time::Duration;

use network_manager::{AccessPoint, AccessPointCredentials, Connection, ConnectionState,
Connectivity, Device, DeviceType, NetworkManager, Security, ServiceState};
use network_manager::{
AccessPoint, AccessPointCredentials, Connection, ConnectionState, Connectivity, Device,
DeviceType, NetworkManager, Security, ServiceState,
};

use errors::*;
use exit::{exit, trap_exit_signals, ExitResult};
use config::Config;
use dnsmasq::start_dnsmasq;
use errors::*;
use exit::{exit, trap_exit_signals, ExitResult};
use server::start_server;

pub enum NetworkCommand {
Expand Down Expand Up @@ -495,7 +497,8 @@ pub fn start_network_manager_service() -> Result<()> {
};

if state != ServiceState::Active {
let state = NetworkManager::start_service(15).chain_err(|| ErrorKind::StartNetworkManager)?;
let state =
NetworkManager::start_service(15).chain_err(|| ErrorKind::StartNetworkManager)?;
if state != ServiceState::Active {
bail!(ErrorKind::StartActiveNetworkManager);
} else {
Expand Down
68 changes: 38 additions & 30 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use std::sync::mpsc::{Receiver, Sender};
use std::error::Error as StdError;
use std::fmt;
use std::net::Ipv4Addr;
use std::error::Error as StdError;
use std::sync::mpsc::{Receiver, Sender};

use serde_json;
use path::PathBuf;
use iron::prelude::*;
use iron::{headers, status, typemap, AfterMiddleware, Iron, IronError, IronResult, Request,
Response, Url};
use iron::modifiers::Redirect;
use iron::prelude::*;
use iron::{
headers, status, typemap, AfterMiddleware, Iron, IronError, IronResult, Request, Response, Url,
};
use iron_cors::CorsMiddleware;
use router::Router;
use staticfile::Static;
use mount::Mount;
use persistent::Write;
use params::{FromValue, Params};
use path::PathBuf;
use persistent::Write;
use router::Router;
use serde_json;
use staticfile::Static;

use errors::*;
use network::{NetworkCommand, NetworkCommandResponse};
use exit::{exit, ExitResult};
use network::{NetworkCommand, NetworkCommandResponse};

struct RequestSharedState {
gateway: Ipv4Addr,
Expand Down Expand Up @@ -47,47 +48,54 @@ impl StdError for StringError {
}

macro_rules! get_request_ref {
($req:ident, $ty:ty, $err:expr) => (
($req:ident, $ty:ty, $err:expr) => {
match $req.get_ref::<$ty>() {
Ok(val) => val,
Err(err) => {
error!($err);
return Err(IronError::new(err, status::InternalServerError));
}
},
}
)
};
}

macro_rules! get_param {
($params:ident, $param:expr, $ty:ty) => (
($params:ident, $param:expr, $ty:ty) => {
match $params.get($param) {
Some(value) => {
match <$ty as FromValue>::from_value(value) {
Some(converted) => converted,
None => {
let err = format!("Unexpected type for '{}'", $param);
error!("{}", err);
return Err(IronError::new(StringError(err), status::InternalServerError));
}
}
Some(value) => match <$ty as FromValue>::from_value(value) {
Some(converted) => converted,
None => {
let err = format!("Unexpected type for '{}'", $param);
error!("{}", err);
return Err(IronError::new(
StringError(err),
status::InternalServerError,
));
},
},
None => {
let err = format!("'{}' not found in request params: {:?}", $param, $params);
error!("{}", err);
return Err(IronError::new(StringError(err), status::InternalServerError));
}
return Err(IronError::new(
StringError(err),
status::InternalServerError,
));
},
}
)
};
}

macro_rules! get_request_state {
($req:ident) => (
($req:ident) => {
get_request_ref!(
$req,
Write<RequestSharedState>,
"Getting reference to request shared state failed"
).as_ref().lock().unwrap()
)
)
.as_ref()
.lock()
.unwrap()
};
}

fn exit_with_error<E>(state: &RequestSharedState, e: E, e_kind: ErrorKind) -> IronResult<Response>
Expand Down

0 comments on commit 8e28056

Please sign in to comment.