Skip to content

Commit

Permalink
Switch to failure crate
Browse files Browse the repository at this point in the history
Due to error-chain not being maintain (from my understanding), it would be best to use a crate that would be more up to date and friendlier to use. This should also resolve an issue found in jazzdotdev/jazz#160 due to the way error-chain is designed.
  • Loading branch information
dariusc93 committed Dec 13, 2018
1 parent 9f31126 commit 3f985a7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "Rust diff implementation"

[dependencies]
clap = "2.32.0"
error-chain = "0.12.0"
pest = "2.0.2"
pest_derive = "2.0.1"
failure = "0.1"
failure_derive = "0.1"
55 changes: 30 additions & 25 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,38 @@ use std::{io, num};

use parser::Rule;

error_chain! {
types {
PatchError, PatchErrorKind, PatchResultExt, PatchResult;
}
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "Error reading: {}", _0)]
Reading(io::Error),
#[fail(display = "Error parsing patch: {}", _0)]
ParsingPatch(pest::error::Error<Rule>),
#[fail(display = "Error parsing context: {}", _0)]
ParsingContext(num::ParseIntError),
#[fail(display = "Missing an element: {}", _0)]
NotFound(&'static str),
#[fail(display = "Elements are found in invalid order: {}", _0)]
MalformedPatch(&'static str),
#[fail(display = "Line #{} not found", _0)]
AbruptInput(usize),
#[fail(display = "Invalid line #{}", _0)]
PatchInputMismatch(usize),
}

foreign_links {
Reading(io::Error);
ParsingPatch(pest::error::Error<Rule>);
ParsingContext(num::ParseIntError);
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Reading(err)
}
}

errors {
NotFound(desc: &'static str) {
description("Element is not found")
display("Missing an element: {}", desc)
}
MalformedPatch(desc: &'static str) {
description("Malformed patch")
display("Elements are found in invalid order: {}", desc)
}
AbruptInput(line: usize) {
description("Abrupt input file")
display("Line #{} not found", line)
}
PatchInputMismatch(line: usize) {
description("Patch does not match the input file")
display("Invalid line #{}", line)
}
impl From<pest::error::Error<Rule>> for Error {
fn from(err: pest::error::Error<Rule>) -> Error {
Error::ParsingPatch(err)
}
}

impl From<num::ParseIntError> for Error {
fn from(err: num::ParseIntError) -> Error {
Error::ParsingContext(err)
}
}
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
//! The GNU patch Rust library.
//!


#[macro_use] extern crate pest_derive;
#[macro_use] extern crate failure_derive;
extern crate failure;
extern crate pest;


mod error;
mod parser;

#[macro_use]
extern crate error_chain;
extern crate pest;
#[macro_use]
extern crate pest_derive;


pub use {
error::{PatchError, PatchResult},
std::result,
error::Error as PatchError,
parser::PatchParser,
};

pub type PatchResult<T> = result::Result<T, PatchError>;
23 changes: 13 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::fmt;

use pest::{iterators::Pair, Parser};

use super::error::{PatchErrorKind, PatchResult};
use {
error::Error,
PatchResult
};

#[derive(Parser)]
#[grammar = "../peg/patch.peg"]
Expand Down Expand Up @@ -42,7 +45,7 @@ impl PatchParser {
pub fn process(&self) -> PatchResult<Vec<String>> {
let patch = Self::parse(Rule::patch, &self.patch)?
.next()
.ok_or(PatchErrorKind::NotFound("patch"))?;
.ok_or(Error::NotFound("patch"))?;

let mut file2_text = Vec::new();
let mut file1_ptr: usize = 0;
Expand All @@ -53,11 +56,11 @@ impl PatchParser {
let mut context = patch_element.into_inner();
let context_header = context
.next()
.ok_or(PatchErrorKind::NotFound("context_header"))?;
.ok_or(Error::NotFound("context_header"))?;
let context_header = if let Rule::context_header = context_header.as_rule() {
Self::get_context_header(context_header)?
} else {
return Err(PatchErrorKind::MalformedPatch(
return Err(Error::MalformedPatch(
"Context header is not at the start of a context",
)
.into());
Expand All @@ -66,7 +69,7 @@ impl PatchParser {
file2_text.push(
self.text
.get(i)
.ok_or(PatchErrorKind::AbruptInput(i))?
.ok_or(Error::AbruptInput(i))?
.to_owned(),
);
}
Expand All @@ -77,10 +80,10 @@ impl PatchParser {
if self
.text
.get(file1_ptr)
.ok_or(PatchErrorKind::AbruptInput(file1_ptr))?
.ok_or(Error::AbruptInput(file1_ptr))?
!= line.as_span().as_str()
{
return Err(PatchErrorKind::PatchInputMismatch(file1_ptr).into());
return Err(Error::PatchInputMismatch(file1_ptr).into());
}
file2_text.push(line.as_span().as_str().to_owned());
file1_ptr += 1;
Expand All @@ -89,10 +92,10 @@ impl PatchParser {
if self
.text
.get(file1_ptr)
.ok_or(PatchErrorKind::AbruptInput(file1_ptr))?
.ok_or(Error::AbruptInput(file1_ptr))?
!= line.as_span().as_str()
{
return Err(PatchErrorKind::PatchInputMismatch(file1_ptr).into());
return Err(Error::PatchInputMismatch(file1_ptr).into());
}
file1_ptr += 1;
}
Expand All @@ -111,7 +114,7 @@ impl PatchParser {
file2_text.push(
self.text
.get(i)
.ok_or(PatchErrorKind::AbruptInput(i))?
.ok_or(Error::AbruptInput(i))?
.to_owned(),
);
}
Expand Down

0 comments on commit 3f985a7

Please sign in to comment.