Port from Failure to Snafu (#281)

* Failure -> Snafu for build command

* Port skeletal remains of init to snafu

* failure -> snafu for serve

* failure -> snafu for upload, remove impl_from macro

* failure -> custom error in vfs

* Bye bye, failure

* Fix Rust 1.36 build regression
This commit is contained in:
Lucien Greathouse
2019-12-18 15:44:46 -08:00
committed by GitHub
parent 41396367ac
commit 1b9e90e786
10 changed files with 99 additions and 104 deletions

1
Cargo.lock generated
View File

@@ -1615,7 +1615,6 @@ dependencies = [
"crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -52,7 +52,6 @@ harness = false
crossbeam-channel = "0.3.9" crossbeam-channel = "0.3.9"
csv = "1.0" csv = "1.0"
env_logger = "0.6" env_logger = "0.6"
failure = "0.1.3"
futures = "0.1" futures = "0.1"
humantime = "1.3.0" humantime = "1.3.0"
hyper = "0.12" hyper = "0.12"

View File

@@ -1,6 +1,5 @@
use std::{panic, process}; use std::{error::Error, panic, process};
use failure::Error;
use log::error; use log::error;
use structopt::StructOpt; use structopt::StructOpt;
@@ -45,7 +44,7 @@ fn main() {
} }
} }
fn run(subcommand: Subcommand) -> Result<(), Error> { fn run(subcommand: Subcommand) -> Result<(), Box<dyn Error>> {
match subcommand { match subcommand {
Subcommand::Init(init_options) => cli::init(init_options)?, Subcommand::Init(init_options) => cli::init(init_options)?,
Subcommand::Serve(serve_options) => cli::serve(serve_options)?, Subcommand::Serve(serve_options) => cli::serve(serve_options)?,

View File

@@ -3,13 +3,13 @@ use std::{
io::{self, BufWriter, Write}, io::{self, BufWriter, Write},
}; };
use failure::Fail; use snafu::{ResultExt, Snafu};
use crate::{ use crate::{
cli::BuildCommand, cli::BuildCommand,
common_setup, common_setup,
project::ProjectError, project::ProjectError,
vfs::{FsError, RealFetcher, Vfs, WatchMode}, vfs::{RealFetcher, Vfs, WatchMode},
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -32,41 +32,46 @@ fn detect_output_kind(options: &BuildCommand) -> Option<OutputKind> {
} }
} }
#[derive(Debug, Fail)] #[derive(Debug, Snafu)]
pub enum BuildError { pub struct BuildError(Error);
#[fail(display = "Could not detect what kind of file to create")]
#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Could not detect what kind of file to create"))]
UnknownOutputKind, UnknownOutputKind,
#[fail(display = "IO error: {}", _0)] #[snafu(display("{}", source))]
IoError(#[fail(cause)] io::Error), Io { source: io::Error },
#[fail(display = "XML model error: {}", _0)] #[snafu(display("{}", source))]
XmlModelEncodeError(#[fail(cause)] rbx_xml::EncodeError), XmlModelEncode { source: rbx_xml::EncodeError },
#[fail(display = "Binary model error: {:?}", _0)] #[snafu(display("Binary model error: {:?}", source))]
BinaryModelEncodeError(rbx_binary::EncodeError), BinaryModelEncode {
#[snafu(source(false))]
source: rbx_binary::EncodeError,
},
#[fail(display = "{}", _0)] #[snafu(display("{}", source))]
ProjectError(#[fail(cause)] ProjectError), Project { source: ProjectError },
#[fail(display = "{}", _0)]
FsError(#[fail(cause)] FsError),
} }
impl_from!(BuildError { impl From<rbx_binary::EncodeError> for Error {
io::Error => IoError, fn from(source: rbx_binary::EncodeError) -> Self {
rbx_xml::EncodeError => XmlModelEncodeError, Error::BinaryModelEncode { source }
rbx_binary::EncodeError => BinaryModelEncodeError, }
ProjectError => ProjectError, }
FsError => FsError,
});
fn xml_encode_config() -> rbx_xml::EncodeOptions { fn xml_encode_config() -> rbx_xml::EncodeOptions {
rbx_xml::EncodeOptions::new().property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown) rbx_xml::EncodeOptions::new().property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown)
} }
pub fn build(options: BuildCommand) -> Result<(), BuildError> { pub fn build(options: BuildCommand) -> Result<(), BuildError> {
let output_kind = detect_output_kind(&options).ok_or(BuildError::UnknownOutputKind)?; Ok(build_inner(options)?)
}
fn build_inner(options: BuildCommand) -> Result<(), Error> {
let output_kind = detect_output_kind(&options).ok_or(Error::UnknownOutputKind)?;
log::debug!("Hoping to generate file of type {:?}", output_kind); log::debug!("Hoping to generate file of type {:?}", output_kind);
@@ -77,14 +82,17 @@ pub fn build(options: BuildCommand) -> Result<(), BuildError> {
let root_id = tree.get_root_id(); let root_id = tree.get_root_id();
log::trace!("Opening output file for write"); log::trace!("Opening output file for write");
let mut file = BufWriter::new(File::create(&options.output)?);
let file = File::create(&options.output).context(Io)?;
let mut file = BufWriter::new(file);
match output_kind { match output_kind {
OutputKind::Rbxmx => { OutputKind::Rbxmx => {
// Model files include the root instance of the tree and all its // Model files include the root instance of the tree and all its
// descendants. // descendants.
rbx_xml::to_writer(&mut file, tree.inner(), &[root_id], xml_encode_config())?; rbx_xml::to_writer(&mut file, tree.inner(), &[root_id], xml_encode_config())
.context(XmlModelEncode)?;
} }
OutputKind::Rbxlx => { OutputKind::Rbxlx => {
// Place files don't contain an entry for the DataModel, but our // Place files don't contain an entry for the DataModel, but our
@@ -93,7 +101,8 @@ pub fn build(options: BuildCommand) -> Result<(), BuildError> {
let root_instance = tree.get_instance(root_id).unwrap(); let root_instance = tree.get_instance(root_id).unwrap();
let top_level_ids = root_instance.children(); let top_level_ids = root_instance.children();
rbx_xml::to_writer(&mut file, tree.inner(), top_level_ids, xml_encode_config())?; rbx_xml::to_writer(&mut file, tree.inner(), top_level_ids, xml_encode_config())
.context(XmlModelEncode)?;
} }
OutputKind::Rbxm => { OutputKind::Rbxm => {
rbx_binary::encode(tree.inner(), &[root_id], &mut file)?; rbx_binary::encode(tree.inner(), &[root_id], &mut file)?;
@@ -110,7 +119,7 @@ pub fn build(options: BuildCommand) -> Result<(), BuildError> {
} }
} }
file.flush()?; file.flush().context(Io)?;
log::trace!("Done!"); log::trace!("Done!");

View File

@@ -1,17 +1,17 @@
use failure::Fail; use snafu::Snafu;
use crate::{cli::InitCommand, project::ProjectError}; use crate::cli::InitCommand;
#[derive(Debug, Fail)] #[derive(Debug, Snafu)]
pub enum InitError { pub struct InitError(Error);
#[fail(display = "Project init error: {}", _0)]
ProjectError(#[fail(cause)] ProjectError), #[derive(Debug, Snafu)]
enum Error {}
pub fn init(options: InitCommand) -> Result<(), InitError> {
Ok(init_inner(options)?)
} }
impl_from!(InitError { fn init_inner(_options: InitCommand) -> Result<(), Error> {
ProjectError => ProjectError,
});
pub fn init(_options: InitCommand) -> Result<(), InitError> {
unimplemented!("init command"); unimplemented!("init command");
} }

View File

@@ -3,12 +3,11 @@ use std::{
sync::Arc, sync::Arc,
}; };
use failure::Fail; use snafu::Snafu;
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
use crate::{ use crate::{
cli::ServeCommand, cli::ServeCommand,
project::ProjectError,
serve_session::ServeSession, serve_session::ServeSession,
vfs::{RealFetcher, Vfs, WatchMode}, vfs::{RealFetcher, Vfs, WatchMode},
web::LiveServer, web::LiveServer,
@@ -16,17 +15,17 @@ use crate::{
const DEFAULT_PORT: u16 = 34872; const DEFAULT_PORT: u16 = 34872;
#[derive(Debug, Fail)] #[derive(Debug, Snafu)]
pub enum ServeError { pub struct ServeError(Error);
#[fail(display = "Couldn't load project: {}", _0)]
ProjectError(#[fail(cause)] ProjectError),
}
impl_from!(ServeError { #[derive(Debug, Snafu)]
ProjectError => ProjectError, enum Error {}
});
pub fn serve(options: ServeCommand) -> Result<(), ServeError> { pub fn serve(options: ServeCommand) -> Result<(), ServeError> {
Ok(serve_inner(options)?)
}
fn serve_inner(options: ServeCommand) -> Result<(), Error> {
let vfs = Vfs::new(RealFetcher::new(WatchMode::Enabled)); let vfs = Vfs::new(RealFetcher::new(WatchMode::Enabled));
let session = Arc::new(ServeSession::new(vfs, &options.project)); let session = Arc::new(ServeSession::new(vfs, &options.project));

View File

@@ -1,5 +1,5 @@
use failure::Fail;
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT}; use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
use snafu::{ResultExt, Snafu};
use crate::{ use crate::{
auth_cookie::get_auth_cookie, auth_cookie::get_auth_cookie,
@@ -8,31 +8,35 @@ use crate::{
vfs::{RealFetcher, Vfs, WatchMode}, vfs::{RealFetcher, Vfs, WatchMode},
}; };
#[derive(Debug, Fail)] #[derive(Debug, Snafu)]
pub enum UploadError { pub struct UploadError(Error);
#[fail(display = "Rojo could not find your Roblox auth cookie. Please pass one via --cookie.")]
#[derive(Debug, Snafu)]
enum Error {
#[snafu(display(
"Rojo could not find your Roblox auth cookie. Please pass one via --cookie.",
))]
NeedAuthCookie, NeedAuthCookie,
#[fail(display = "XML model file encode error: {}", _0)] #[snafu(display("XML model file encode error: {}", source))]
XmlModelEncode(#[fail(cause)] rbx_xml::EncodeError), XmlModel { source: rbx_xml::EncodeError },
#[fail(display = "HTTP error: {}", _0)] #[snafu(display("HTTP error: {}", source))]
Http(#[fail(cause)] reqwest::Error), Http { source: reqwest::Error },
#[fail(display = "Roblox API error: {}", _0)] #[snafu(display("Roblox API error: {}", body))]
RobloxApi(String), RobloxApi { body: String },
} }
impl_from!(UploadError {
rbx_xml::EncodeError => XmlModelEncode,
reqwest::Error => Http,
});
pub fn upload(options: UploadCommand) -> Result<(), UploadError> { pub fn upload(options: UploadCommand) -> Result<(), UploadError> {
Ok(upload_inner(options)?)
}
fn upload_inner(options: UploadCommand) -> Result<(), Error> {
let cookie = options let cookie = options
.cookie .cookie
.or_else(get_auth_cookie) .or_else(get_auth_cookie)
.ok_or(UploadError::NeedAuthCookie)?; .ok_or(Error::NeedAuthCookie)?;
log::trace!("Constructing in-memory filesystem"); log::trace!("Constructing in-memory filesystem");
let vfs = Vfs::new(RealFetcher::new(WatchMode::Disabled)); let vfs = Vfs::new(RealFetcher::new(WatchMode::Disabled));
@@ -45,7 +49,7 @@ pub fn upload(options: UploadCommand) -> Result<(), UploadError> {
log::trace!("Encoding XML model"); log::trace!("Encoding XML model");
let config = rbx_xml::EncodeOptions::new() let config = rbx_xml::EncodeOptions::new()
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown); .property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown);
rbx_xml::to_writer(&mut buffer, tree.inner(), &[root_id], config)?; rbx_xml::to_writer(&mut buffer, tree.inner(), &[root_id], config).context(XmlModel)?;
let url = format!( let url = format!(
"https://data.roblox.com/Data/Upload.ashx?assetid={}", "https://data.roblox.com/Data/Upload.ashx?assetid={}",
@@ -62,10 +66,13 @@ pub fn upload(options: UploadCommand) -> Result<(), UploadError> {
.header(CONTENT_TYPE, "application/xml") .header(CONTENT_TYPE, "application/xml")
.header(ACCEPT, "application/json") .header(ACCEPT, "application/json")
.body(buffer) .body(buffer)
.send()?; .send()
.context(Http)?;
if !response.status().is_success() { if !response.status().is_success() {
return Err(UploadError::RobloxApi(response.text()?)); return Err(Error::RobloxApi {
body: response.text().context(Http)?,
});
} }
Ok(()) Ok(())

View File

@@ -1,17 +0,0 @@
/// Implements 'From' for a list of variants, intended for use with error enums
/// that are wrapping a number of errors from other methods.
macro_rules! impl_from {
(
$enum_name: ident {
$($error_type: ty => $variant_name: ident),* $(,)*
}
) => {
$(
impl From<$error_type> for $enum_name {
fn from(error: $error_type) -> $enum_name {
$enum_name::$variant_name(error)
}
}
)*
}
}

View File

@@ -2,9 +2,6 @@
// Rojo's web UI currently. // Rojo's web UI currently.
#![recursion_limit = "1024"] #![recursion_limit = "1024"]
#[macro_use]
mod impl_from;
pub mod cli; pub mod cli;
#[cfg(test)] #[cfg(test)]

View File

@@ -1,6 +1,4 @@
use std::{fmt, io, path::PathBuf}; use std::{error::Error, fmt, io, path::PathBuf};
use failure::Fail;
pub type FsResult<T> = Result<T, FsError>; pub type FsResult<T> = Result<T, FsError>;
pub use io::ErrorKind as FsErrorKind; pub use io::ErrorKind as FsErrorKind;
@@ -21,32 +19,37 @@ impl<T> FsResultExt<T> for Result<T, FsError> {
/// A wrapper around io::Error that also attaches the path associated with the /// A wrapper around io::Error that also attaches the path associated with the
/// error. /// error.
#[derive(Debug, Fail)] #[derive(Debug)]
pub struct FsError { pub struct FsError {
#[fail(cause)] source: io::Error,
inner: io::Error,
path: PathBuf, path: PathBuf,
} }
impl FsError { impl FsError {
pub fn new<P: Into<PathBuf>>(inner: io::Error, path: P) -> FsError { pub fn new<P: Into<PathBuf>>(source: io::Error, path: P) -> FsError {
FsError { FsError {
inner, source,
path: path.into(), path: path.into(),
} }
} }
pub fn kind(&self) -> FsErrorKind { pub fn kind(&self) -> FsErrorKind {
self.inner.kind() self.source.kind()
} }
pub fn into_raw(self) -> (io::Error, PathBuf) { pub fn into_raw(self) -> (io::Error, PathBuf) {
(self.inner, self.path) (self.source, self.path)
}
}
impl Error for FsError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.source)
} }
} }
impl fmt::Display for FsError { impl fmt::Display for FsError {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
write!(output, "{}: {}", self.path.display(), self.inner) write!(output, "{}: {}", self.path.display(), self.source)
} }
} }