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 15, 2017
1 parent 7397b1a commit b472213
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 21 deletions.
38 changes: 29 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
language: rust

dist: trusty

os:
- linux
- osx

addons:
apt:
sources:
# Provides newer gcc.
- ubuntu-toolchain-r-test
# Provides libclang 3.9.
- llvm-toolchain-trusty-3.9
packages:
- autoconf2.13
# bindgen requires libclang >= 3.9.
- clang-3.9
# SpiderMonkey needs gcc >= 4.9 and 5 is ICEing.
- gcc-6
- g++-6

rust:
- stable
- beta
- nightly

cache: cargo

env:
matrix:
- PROFILE="--release"
- PROFILE=""
- PROFILE="--release" FEATURES=""
- PROFILE="" FEATURES=""
- PROFILE="--release" FEATURES="--features debugmozjs"
- PROFILE="" FEATURES="--features debugmozjs"

before_install:
- source ./ci/before_install.sh

script:
- cargo build $PROFILE --verbose
- cargo test $PROFILE --verbose
- if [[ "$PROFILE" == "--release" ]]; then
cargo bench;
fi
- PROFILE="$PROFILE" FEATURES="$FEATURES" travis_wait ./ci/script.sh
- ccache --show-stats
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.

22 changes: 18 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
[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
required-features = ["clap"]

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

[dependencies.clap]
optional = true
version = "2.26.0"

[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"

[features]
default = ["clap"]
debugmozjs = ['js/debugmozjs']
20 changes: 20 additions & 0 deletions ci/before_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

# We always want backtraces for everything.
export RUST_BACKTRACE=1

if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
brew install [email protected] ccache [email protected] yasm
export LIBCLANG_PATH=$(find /usr/local/Cellar/llvm -type f -name libclang.dylib | head -n 1)
export LIBCLANG_PATH=$(dirname $LIBCLANG_PATH)
elif [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
export CC="gcc-6"
export CXX="g++-6"
export LIBCLANG_PATH=/usr/lib/llvm-3.9/lib
else
echo "Error: unknown \$TRAVIS_OS_NAME: $TRAVIS_OS_NAME"
exit 1
fi

ccache -c
export CCACHE=$(which ccache)
10 changes: 10 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -eux

cargo build -vv $PROFILE $FEATURES
cargo test -vv $PROFILE $FEATURES

if [[ "$PROFILE" == "--release" && "$FEATURES" == "" ]]; then
cargo bench -vv
fi
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
31 changes: 29 additions & 2 deletions src/bin/starling.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
extern crate clap;
extern crate error_chain;
extern crate starling;

use clap::{App, Arg};
use error_chain::ChainedError;
use std::process;

/// Parse the given CLI arguments into a `starling::Options` configuration
/// object.
///
/// If argument parsing fails, then a usage string is printed, and the process
/// is exited with 1.
fn parse_cli_args() -> starling::Options {
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();

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

opts
}

fn main() {
if let Err(e) = starling::run() {
println!("Error: {}", e);
let opts = parse_cli_args();
if let Err(e) = opts.run() {
println!("{}", e.display());
process::exit(1);
}
}
65 changes: 59 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,74 @@
//! 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)]

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

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

/// Run the main `starling` loop.
pub fn run() -> Result<(), io::Error> {
error_chain! {
foreign_links {
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 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 b472213

Please sign in to comment.