Skip to content

Commit

Permalink
chore: Switch to failure
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 1, 2019
1 parent 6dee6d4 commit 6fb1bbb
Show file tree
Hide file tree
Showing 24 changed files with 155 additions and 182 deletions.
19 changes: 10 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -35,7 +35,8 @@ pulldown-cmark = {version="0.2", default-features = false}
notify = "4.0"
ghp = "0.1"
regex = "1.1"
error-chain = "0.12"
failure = "0.1"
exitfailure = "0.5"
lazy_static = "1.1"
itertools = "0.8"
ignore = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cobalt/args.rs
Expand Up @@ -4,6 +4,7 @@ use std::path;

use clap;
use env_logger;
use failure::ResultExt;

use crate::error::*;
use cobalt;
Expand Down Expand Up @@ -41,7 +42,7 @@ pub fn get_config(matches: &clap::ArgMatches) -> Result<cobalt::ConfigBuilder> {
// Fetch config information if available
let mut config = if let Some(config_path) = config_path {
cobalt::ConfigBuilder::from_file(config_path)
.chain_err(|| format!("Error reading config file {:?}", config_path))?
.with_context(|_| failure::format_err!("Error reading config file {:?}", config_path))?
} else {
let cwd = env::current_dir().expect("How does this fail?");
cobalt::ConfigBuilder::from_cwd(cwd)?
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cobalt/build.rs
Expand Up @@ -60,7 +60,7 @@ pub fn clean(config: &cobalt::Config) -> Result<()> {
}
};
if cwd.starts_with(&destdir) {
bail!(
failure::bail!(
"Attempting to delete current directory ({:?}), \
Cancelling the operation",
destdir
Expand Down Expand Up @@ -116,7 +116,7 @@ fn import(config: &cobalt::Config, branch: &str, message: &str) -> Result<()> {
info!("Importing {:?} to {}", config.destination, branch);

if !config.destination.is_dir() {
bail!("`{:?}` is not a directory", config.destination);
failure::bail!("`{:?}` is not a directory", config.destination);
}
ghp::import_dir(&config.destination, branch, message)?;

Expand Down
8 changes: 4 additions & 4 deletions src/bin/cobalt/debug.rs
Expand Up @@ -44,7 +44,7 @@ pub fn debug_command(matches: &clap::ArgMatches) -> Result<()> {
println!("{}", name);
}
}
_ => bail!(matches.usage()),
_ => failure::bail!(matches.usage().to_owned()),
},
("files", Some(matches)) => {
let config = args::get_config(matches)?;
Expand Down Expand Up @@ -80,14 +80,14 @@ pub fn debug_command(matches: &clap::ArgMatches) -> Result<()> {
}
}
None => {
bail!("Must specify collection");
failure::bail!("Must specify collection");
}
_ => {
bail!("Collection is not yet supported");
failure::bail!("Collection is not yet supported");
}
}
}
_ => bail!(matches.usage()),
_ => failure::bail!(matches.usage().to_owned()),
}

Ok(())
Expand Down
31 changes: 2 additions & 29 deletions src/bin/cobalt/error.rs
@@ -1,29 +1,2 @@
use std::io;
use std::sync;

use clap;
use cobalt;
use ghp;
use hyper;
use notify;
use serde_yaml;

error_chain! {

links {
}

foreign_links {
Clap(clap::Error);
Cobalt(cobalt::Error);
Ghp(ghp::Error);
Hyper(hyper::Error);
Io(io::Error);
Notify(notify::Error);
Recv(sync::mpsc::RecvError);
SerdeYaml(serde_yaml::Error);
}

errors {
}
}
pub use failure::Error;
pub type Result<T> = std::result::Result<T, Error>;
20 changes: 7 additions & 13 deletions src/bin/cobalt/main.rs
@@ -1,18 +1,8 @@
#![warn(warnings)]

extern crate cobalt;
extern crate env_logger;
extern crate ghp;
extern crate hyper;
extern crate notify;
extern crate serde_yaml;

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate error_chain;

#[macro_use]
extern crate clap;

Expand All @@ -29,13 +19,17 @@ mod serve;
use std::alloc;

use clap::{App, AppSettings};
use failure::ResultExt;

use crate::error::*;

#[global_allocator]
static GLOBAL: alloc::System = alloc::System;

quick_main!(run);
fn main() -> std::result::Result<(), exitfailure::ExitFailure> {
run()?;
Ok(())
}

fn run() -> Result<()> {
let app_cli = App::new("Cobalt")
Expand Down Expand Up @@ -76,10 +70,10 @@ fn run() -> Result<()> {
"import" => build::import_command(matches),
"debug" => debug::debug_command(matches),
_ => {
bail!(global_matches.usage());
failure::bail!(global_matches.usage().to_owned());
}
}
.chain_err(|| format!("{} command failed", command))?;
.with_context(|_| failure::format_err!("{} command failed", command))?;

Ok(())
}
37 changes: 20 additions & 17 deletions src/bin/cobalt/new.rs
Expand Up @@ -7,6 +7,7 @@ use std::path;

use clap;
use cobalt::cobalt_model;
use failure::ResultExt;

use crate::args;
use crate::error::*;
Expand All @@ -26,7 +27,7 @@ pub fn init_command(matches: &clap::ArgMatches) -> Result<()> {
let directory = matches.value_of("DIRECTORY").unwrap();

create_new_project(&directory.to_string())
.chain_err(|| "Could not create a new cobalt project")?;
.with_context(|_| failure::err_msg("Could not create a new cobalt project"))?;
info!("Created new project at {}", directory);

Ok(())
Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn new_command(matches: &clap::ArgMatches) -> Result<()> {
let ext = matches.value_of("with-ext");

create_new_document(&config, title, file, ext)
.chain_err(|| format!("Could not create `{}`", title))?;
.with_context(|_| failure::format_err!("Could not create `{}`", title))?;

Ok(())
}
Expand Down Expand Up @@ -119,7 +120,7 @@ pub fn rename_command(matches: &clap::ArgMatches) -> Result<()> {
let file = file;

rename_document(&config, source, title, file)
.chain_err(|| format!("Could not rename `{}`", title))?;
.with_context(|_| failure::format_err!("Could not rename `{}`", title))?;

Ok(())
}
Expand All @@ -145,7 +146,8 @@ pub fn publish_command(matches: &clap::ArgMatches) -> Result<()> {
let config = args::get_config(matches)?;
let config = config.build()?;

publish_document(&config, &file).chain_err(|| format!("Could not publish `{:?}`", file))?;
publish_document(&config, &file)
.with_context(|_| failure::format_err!("Could not publish `{:?}`", file))?;

Ok(())
}
Expand Down Expand Up @@ -256,9 +258,10 @@ pub fn create_new_document(
};

let rel_file = file.strip_prefix(&config.source).map_err(|_| {
format!(
"New file {:?} not project directory ({:?})",
file, config.source
failure::format_err!(
"New file {} not project directory ({})",
file.display(),
config.source.display()
)
})?;

Expand All @@ -279,9 +282,9 @@ pub fn create_new_document(
{
pages.slug.as_str()
} else {
bail!(
"Target file wouldn't be a member of any collection: {:?}",
file
failure::bail!(
"Target file wouldn't be a member of any collection: {}",
file.display()
);
};

Expand All @@ -290,17 +293,17 @@ pub fn create_new_document(
.join(format!("_defaults/{}.{}", file_type, extension));
let source = if source_path.is_file() {
cobalt_model::files::read_file(&source_path)
.chain_err(|| format!("Failed to read default: {:?}", source_path))?
.with_context(|_| failure::format_err!("Failed to read default: {:?}", source_path))?
} else {
debug!(
"No custom default provided ({:?}), falling back to built-in",
source_path
);
if extension != "md" {
bail!(
"No builtin default for `{}` files, only `md`: {:?}",
failure::bail!(
"No builtin default for `{}` files, only `md`: {}",
extension,
file
file.display()
);
}
// For custom collections, use a post default.
Expand Down Expand Up @@ -331,7 +334,7 @@ fn create_file_for_path(path: &path::Path, content: &str) -> Result<()> {
.write(true)
.create_new(true)
.open(path)
.chain_err(|| format!("Failed to create file {:?}", path))?;
.with_context(|_| failure::format_err!("Failed to create file {}", path.display()))?;

file.write_all(content.as_bytes())?;

Expand Down Expand Up @@ -389,7 +392,7 @@ pub fn rename_document(
.merge_path(rel_src)
.merge(pages.default.clone())
} else {
bail!(
failure::bail!(
"Target file wouldn't be a member of any collection: {:?}",
target
);
Expand Down Expand Up @@ -461,7 +464,7 @@ fn move_from_drafts_to_posts(
);
if let Some(parent) = target.parent() {
fs::create_dir_all(parent)
.map_err(|e| format!("Could not create {:?}: {}", parent, e))?;
.with_context(|_| failure::format_err!("Could not create {}", parent.display()))?;
}
fs::rename(file, &target)?;
Ok(target)
Expand Down
17 changes: 10 additions & 7 deletions src/bin/cobalt/serve.rs
Expand Up @@ -8,7 +8,7 @@ use std::time;

use clap;
use cobalt::cobalt_model;
use error_chain::ChainedError;
use failure::ResultExt;
use hyper;
use hyper::server::{Request, Response, Server};
use hyper::uri::RequestUri;
Expand Down Expand Up @@ -170,18 +170,20 @@ fn watch(config: &cobalt_model::Config) -> Result<()> {
// Files::includes_file
let source = path::Path::new(&config.source)
.canonicalize()
.chain_err(|| "Failed in processing source")?;
.with_context(|_| failure::err_msg("Failed in processing source"))?;

let (tx, rx) = channel();
let mut watcher =
notify::watcher(tx, time::Duration::from_secs(1)).chain_err(|| "Notify error")?;
let mut watcher = notify::watcher(tx, time::Duration::from_secs(1))
.with_context(|_| failure::err_msg("Notify error"))?;
watcher
.watch(&source, notify::RecursiveMode::Recursive)
.chain_err(|| "Notify error")?;
.with_context(|_| failure::err_msg("Notify error"))?;
info!("Watching {:?} for changes", &config.source);

loop {
let event = rx.recv().chain_err(|| "Notify error")?;
let event = rx
.recv()
.with_context(|_| failure::err_msg("Notify error"))?;
let event_path = match event {
notify::DebouncedEvent::Create(ref path)
| notify::DebouncedEvent::NoticeWrite(ref path)
Expand Down Expand Up @@ -209,7 +211,8 @@ fn watch(config: &cobalt_model::Config) -> Result<()> {
if rebuild {
let result = build::build(config.clone());
if let Err(fail) = result {
error!("build failed\n{}", fail.display_chain());
let fail: exitfailure::ExitFailure = fail.into();
error!("build failed\n{:?}", fail);
}
}
}
Expand Down

0 comments on commit 6fb1bbb

Please sign in to comment.