From 4ed50c885b127df2fe9d3620619d8aab27616520 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Jan 2018 22:58:56 +1100 Subject: [PATCH] Clean up code using Rustfmt --- src/assets.rs | 10 +++---- src/config.rs | 16 +++++----- src/installer.rs | 10 +++---- src/main.rs | 8 ++--- src/rest.rs | 76 +++++++++++++++++++++++------------------------- 5 files changed, 58 insertions(+), 62 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index f9bbfe3..d24e4a2 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -12,14 +12,14 @@ include!(concat!(env!("OUT_DIR"), "/data.rs")); /// Returns a static file based upon a given String as a Path. /// /// file_path: String path, beginning with a / -pub fn file_from_string(file_path : &str) -> Option<(ContentType, Cow<'static, [u8]>)> { +pub fn file_from_string(file_path: &str) -> Option<(ContentType, Cow<'static, [u8]>)> { let guessed_mime = match file_path.rfind(".") { Some(ext_ptr) => { - let ext = &file_path[ext_ptr + 1 ..]; + let ext = &file_path[ext_ptr + 1..]; get_mime_type(ext) - }, - None => octet_stream() + } + None => octet_stream(), }; let string_mime = guessed_mime.to_string(); @@ -30,6 +30,6 @@ pub fn file_from_string(file_path : &str) -> Option<(ContentType, Cow<'static, [ match FILES.get(&format!("static{}", file_path)) { Ok(val) => Some((content_type, val)), // Only error is a not found one - Err(_) => None + Err(_) => None, } } diff --git a/src/config.rs b/src/config.rs index fc4ff22..3d3be6b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,22 +10,22 @@ use serde_json::{self, Error as SerdeError}; /// Describes a overview of a individual package. #[derive(Deserialize, Serialize, Clone)] pub struct PackageDescription { - pub name : String, - pub description : String, - pub default : Option + pub name: String, + pub description: String, + pub default: Option, } /// Describes the application itself. #[derive(Deserialize, Serialize, Clone)] pub struct GeneralConfig { - pub name : String, - pub installing_message : String + pub name: String, + pub installing_message: String, } #[derive(Deserialize, Serialize, Clone)] pub struct Config { - pub general : GeneralConfig, - pub packages : Vec + pub general: GeneralConfig, + pub packages: Vec, } impl Config { @@ -35,7 +35,7 @@ impl Config { } /// Builds a configuration from a specified TOML string. - pub fn from_toml_str(contents : &str) -> Result { + pub fn from_toml_str(contents: &str) -> Result { toml::from_str(contents) } } diff --git a/src/installer.rs b/src/installer.rs index ccba867..9f30526 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -12,7 +12,7 @@ use config::Config; /// The installer framework contains metadata about packages, what is installable, what isn't, /// etc. pub struct InstallerFramework { - config : Config + config: Config, } impl InstallerFramework { @@ -27,7 +27,7 @@ impl InstallerFramework { let base_dir = match var("LOCALAPPDATA") { Ok(path) => PathBuf::from(path), - Err(_) => home_dir()? + Err(_) => home_dir()?, }; println!("{:?}", base_dir); @@ -39,9 +39,7 @@ impl InstallerFramework { } /// Creates a new instance of the Installer Framework with a specified Config. - pub fn new(config : Config) -> Self { - InstallerFramework { - config - } + pub fn new(config: Config) -> Self { + InstallerFramework { config } } } diff --git a/src/main.rs b/src/main.rs index 357d6b8..4d5af29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ extern crate web_view; +extern crate futures; extern crate hyper; extern crate hyper_tls; -extern crate tokio_core; -extern crate futures; extern crate mime_guess; +extern crate tokio_core; extern crate includedir; extern crate phf; @@ -33,7 +33,7 @@ use installer::InstallerFramework; use rest::WebServer; // TODO: Fetch this over a HTTP request? -static RAW_CONFIG : &'static str = include_str!("../config.toml"); +static RAW_CONFIG: &'static str = include_str!("../config.toml"); fn main() { let config = Config::from_toml_str(RAW_CONFIG).unwrap(); @@ -63,6 +63,6 @@ fn main() { debug, init_cb, /* frontend_cb: */ |_, _, _| {}, - userdata + userdata, ); } diff --git a/src/rest.rs b/src/rest.rs index 33c90d6..086b39a 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -12,11 +12,11 @@ use futures::future; use futures::future::FutureResult; use hyper; -use hyper::{Get, StatusCode, Error as HyperError}; +use hyper::{Error as HyperError, Get, StatusCode}; use hyper::header::{ContentLength, ContentType}; -use hyper::server::{Http, Service, Request, Response}; +use hyper::server::{Http, Request, Response, Service}; -use std::net::{SocketAddr, IpAddr, Ipv4Addr}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread::{self, JoinHandle}; use std::process::exit; use std::sync::Arc; @@ -28,13 +28,13 @@ use installer::InstallerFramework; #[derive(Serialize)] struct FileSelection { - path : Option + path: Option, } /// Encapsulates Hyper's state. pub struct WebServer { - handle : JoinHandle<()>, - addr : SocketAddr + _handle: JoinHandle<()>, + addr: SocketAddr, } impl WebServer { @@ -44,25 +44,27 @@ impl WebServer { } /// Creates a new web server, bound to a random port on localhost. - pub fn new(framework : InstallerFramework) -> Result { - WebServer::with_addr(framework, SocketAddr::new( - IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0)) + pub fn new(framework: InstallerFramework) -> Result { + WebServer::with_addr( + framework, + SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0), + ) } /// Creates a new web server with the specified address. - pub fn with_addr(framework : InstallerFramework, addr : SocketAddr) - -> Result { + pub fn with_addr(framework: InstallerFramework, addr: SocketAddr) -> Result { let (sender, receiver) = channel(); let handle = thread::spawn(move || { let shared_framework = Arc::new(framework); - let server = - Http::new().bind(&addr, move || + let server = Http::new() + .bind(&addr, move || { Ok(WebService { - framework : shared_framework.clone() + framework: shared_framework.clone(), }) - ).unwrap(); + }) + .unwrap(); sender.send(server.local_addr().unwrap()).unwrap(); @@ -72,44 +74,45 @@ impl WebServer { let addr = receiver.recv().unwrap(); Ok(WebServer { - handle, addr + _handle: handle, + addr, }) } } struct WebService { - framework : Arc + framework: Arc, } impl Service for WebService { type Request = Request; type Response = Response; type Error = hyper::Error; - type Future = FutureResult; + type Future = FutureResult; fn call(&self, req: Self::Request) -> Self::Future { future::ok(match (req.method(), req.path()) { // This endpoint should be usable directly from a