Skip to content

Commit

Permalink
Add infrastructure for an Error type, config options, and CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Aug 10, 2017
1 parent 7397b1a commit b59bdd7
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 11 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
[package]
authors = ["Nick Fitzgerald <[email protected]>"]
authors = ["The Starling Project Developers"]
description = "The project that will henceforth be known as `starling`."
name = "starling"
version = "0.1.0"

[[bin]]
doc = false
name = "starling"
path = "src/bin/starling.rs"
doc = false

[dependencies]
clap = "2.26.0"
error-chain = "0.10.0"
futures = "0.1.13"
tokio-core = "0.1.6"

[dependencies.backtrace]
features = ["cpp_demangle"]
version = "0.3.2"

[dependencies.js]
git = "https://github.com/fitzgen/mozjs"
branch = "smup"
git = "https://github.com/fitzgen/mozjs"
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reorder_imports = true
reorder_imported_names = true
write_mode = "Overwrite"
closure_block_indent_threshold = 0
use_try_shorthand = true
4 changes: 3 additions & 1 deletion src/bin/starling.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
extern crate error_chain;
extern crate starling;

use error_chain::ChainedError;
use std::process;

fn main() {
if let Err(e) = starling::run() {
println!("Error: {}", e);
println!("{}", e.display());
process::exit(1);
}
}
38 changes: 38 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Parsing command line arguments into `starling::Options`.
use super::{Options, Result};
use clap::{App, Arg};
use std::ffi;

/// Parse the given CLI arguments into a `starling::Options` configuration
/// object.
///
/// ```
/// extern crate starling;
/// use std::env;
///
/// # fn foo() -> starling::Result<()> {
/// let opts = starling::cli::parse(env::args_os())?;
/// # Ok(())
/// # }
/// ```
pub fn parse<I, T>(args: I) -> Result<Options>
where
I: IntoIterator<Item = T>,
T: Into<ffi::OsString> + Clone,
{
let matches = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(
Arg::with_name("file")
.required(true)
.help("The JavaScript file to evaluate as the main task."),
)
.get_matches_from_safe(args)?;

let opts = Options::new(matches.value_of("file").unwrap());

Ok(opts)
}
87 changes: 81 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,96 @@
//! The project that will henceforth be known as `starling`.
//!
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unsafe_code)]
// Annoying warning emitted from the `error_chain!` macro.
#![allow(unused_doc_comment)]

extern crate clap;
#[macro_use]
extern crate error_chain;
extern crate futures;
extern crate tokio_core;

pub mod cli;

use futures::future;
use std::io;
use std::env;
use std::path;
use tokio_core::reactor::Core;

/// Run the main `starling` loop.
pub fn run() -> Result<(), io::Error> {
error_chain! {
foreign_links {
Config(::clap::Error)
/// A configuration error.
;

Io(::std::io::Error)
/// An IO error.
;
}
}

/// Configuration options for building a `starling` event loop.
///
/// ```
/// extern crate starling;
///
/// # fn foo() -> starling::Result<()> {
/// // Construct a new `Options` builder, providing the file containing
/// // the main JavaScript task.
/// starling::Options::new("path/to/main.js")
/// // Finish configuring the `Options` builder and run the event
/// // loop!
/// .run()?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct Options {
main: path::PathBuf,
}

impl Options {
/// Construct a new `Options` object for configuring the `starling` event
/// loop.
///
/// The given `main` JavaScript file will be evaluated as the main task.
pub fn new<P>(main: P) -> Options
where
P: Into<path::PathBuf>,
{
Options { main: main.into() }
}

/// Finish this `Options` builder and run the `starling` event loop with its
/// specified configuration.
pub fn run(self) -> Result<()> {
run_with_options(self)
}
}

/// Run the main `starling` event loop, configured with the command line options
/// given to this process.
///
/// ```
/// extern crate starling;
///
/// # fn foo() -> starling::Result<()> {
/// starling::run()?;
/// # Ok(())
/// # }
pub fn run() -> Result<()> {
cli::parse(env::args_os())?.run()
}

/// Run the main `starling` event loop with the specified options.
fn run_with_options(_opts: Options) -> Result<()> {
let mut core = Core::new()?;
core.run(future::ok(()))
}

#[test]
fn it_works() {
}
fn it_works() {}

0 comments on commit b59bdd7

Please sign in to comment.