Skip to content

Commit

Permalink
Fixed the bounds on the StdError impl and Positioner trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jun 15, 2015
1 parent af8d527 commit 7f86f3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,26 @@ r"
assert_eq!(parser.parse("Fig123"), Ok(("Fig", "123")));
assert_eq!(parser.parse("GrapeApple"), Ok(("Grape", "Apple")));
}

#[test]
fn std_error() {
use std::fmt;
use std::error::Error as StdError;
#[derive(Debug)]
struct Error;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error")
}
}
impl StdError for Error {
fn description(&self) -> &str { "error" }
}
let result: Result<((), _), _> = string("abc")
.and_then(|_| Err(Error))
.parse("abc");
assert!(result.is_err());
//Test that ParseError can be coerced to a StdError
let _ = result.map_err(|err| { let err: Box<StdError> = Box::new(err); err });
}
}
18 changes: 13 additions & 5 deletions src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ impl <P: Positioner> ParseError<P> {
}

impl <P> StdError for ParseError<P>
where P: fmt::Display + fmt::Debug + Positioner + Any
, P::Position: fmt::Display + fmt::Debug + Positioner + Any {
where P: Positioner + fmt::Display + fmt::Debug + Any
, P::Position: fmt::Display + fmt::Debug + Any {
fn description(&self) -> &str { "parse error" }
}

Expand Down Expand Up @@ -320,13 +320,21 @@ impl <T: fmt::Display> fmt::Display for Error<T> {
}

///The `State<I>` struct keeps track of the current position in the stream `I`
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq)]
pub struct State<I>
where I: Stream {
pub position: <<I as Stream>::Item as Positioner>::Position,
pub position: <I::Item as Positioner>::Position,
pub input: I
}

impl <I> fmt::Debug for State<I>
where I: Stream + fmt::Debug
, <I::Item as Positioner>::Position: fmt::Debug {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "State {{ position: {:?}, input: {:?} }}", self.position, self.input)
}
}

impl <I: Stream> State<I> {
pub fn new(input: I) -> State<I> {
State { position: <I::Item as Positioner>::start(), input: input }
Expand Down Expand Up @@ -419,7 +427,7 @@ impl <I: Iterator + Clone> Stream for IteratorStream<I>
///When implementing stream for custom token type this must be implemented for that token to allow
///the position to be updated
pub trait Positioner: Clone + PartialEq {
type Position: Clone + Ord + fmt::Debug;
type Position: Clone + Ord;
///Creates a start position
fn start() -> Self::Position;
///Updates the position given that `self` has been taken from the stream
Expand Down

0 comments on commit 7f86f3b

Please sign in to comment.