Clean up code using Rustfmt

This commit is contained in:
James 2018-01-27 22:58:56 +11:00
parent cf41f552c4
commit 4ed50c885b
5 changed files with 58 additions and 62 deletions

View File

@ -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,
}
}

View File

@ -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<bool>
pub name: String,
pub description: String,
pub default: Option<bool>,
}
/// 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<PackageDescription>
pub general: GeneralConfig,
pub packages: Vec<PackageDescription>,
}
impl Config {
@ -35,7 +35,7 @@ impl Config {
}
/// Builds a configuration from a specified TOML string.
pub fn from_toml_str(contents : &str) -> Result<Self, TomlError> {
pub fn from_toml_str(contents: &str) -> Result<Self, TomlError> {
toml::from_str(contents)
}
}

View File

@ -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 }
}
}

View File

@ -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,
);
}

View File

@ -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<String>
path: Option<String>,
}
/// 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<Self, HyperError> {
WebServer::with_addr(framework, SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0))
pub fn new(framework: InstallerFramework) -> Result<Self, HyperError> {
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<Self, HyperError> {
pub fn with_addr(framework: InstallerFramework, addr: SocketAddr) -> Result<Self, HyperError> {
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<InstallerFramework>
framework: Arc<InstallerFramework>,
}
impl Service for WebService {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = FutureResult<Self::Response, Self::Error>;
type Future = FutureResult<Self::Response, Self::Error>;
fn call(&self, req: Self::Request) -> Self::Future {
future::ok(match (req.method(), req.path()) {
// This endpoint should be usable directly from a <script> tag during loading.
// TODO: Handle errors
(&Get, "/api/config") => {
let file = enscapsulate_json("config",
&self.framework.get_config().to_json_str().unwrap());
let file = enscapsulate_json(
"config",
&self.framework.get_config().to_json_str().unwrap(),
);
Response::<hyper::Body>::new()
.with_header(ContentLength(file.len() as u64))
.with_header(ContentType::json())
.with_body(file)
},
}
(&Get, "/api/file-select") => {
let file_dialog = nfd::open_pick_folder(None).unwrap();
let file = match file_dialog {
NfdResponse::Okay(path) => Some(path),
_ => None
_ => None,
};
let response = FileSelection {
path : file
};
let response = FileSelection { path: file };
let file = serde_json::to_string(&response).unwrap();
@ -117,13 +120,11 @@ impl Service for WebService {
.with_header(ContentLength(file.len() as u64))
.with_header(ContentType::json())
.with_body(file)
},
}
(&Get, "/api/default-path") => {
let path = self.framework.get_default_path();
let response = FileSelection {
path
};
let response = FileSelection { path };
let file = serde_json::to_string(&response).unwrap();
@ -131,16 +132,16 @@ impl Service for WebService {
.with_header(ContentLength(file.len() as u64))
.with_header(ContentType::json())
.with_body(file)
},
}
(&Get, "/api/exit") => {
exit(0);
},
}
// Static file handler
(&Get, _) => {
// At this point, we have a web browser client. Search for a index page
// if needed
let mut path : String = req.path().to_owned();
let mut path: String = req.path().to_owned();
if path.ends_with("/") {
path += "index.html";
}
@ -152,19 +153,16 @@ impl Service for WebService {
.with_header(ContentLength(file.len() as u64))
.with_header(content_type)
.with_body(file),
None => Response::new()
.with_status(StatusCode::NotFound)
None => Response::new().with_status(StatusCode::NotFound),
}
},
// Fallthrough for POST/PUT/CONNECT/...
_ => {
Response::new().with_status(StatusCode::NotFound)
}
// Fallthrough for POST/PUT/CONNECT/...
_ => Response::new().with_status(StatusCode::NotFound),
})
}
}
/// Encapsulates JSON as a injectable Javascript script.
fn enscapsulate_json(field_name : &str, json : &str) -> String {
fn enscapsulate_json(field_name: &str, json: &str) -> String {
format!("var {} = {};", field_name, json)
}