Skip to content

Commit

Permalink
Skip extra property when comparing LocatedSpanEx
Browse files Browse the repository at this point in the history
  • Loading branch information
zakcutner authored and progval committed Jan 14, 2020
1 parent be80026 commit 887f7b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
//! ```
//!
//! ## Extra information
//! You can add arbitrary extra information using LocatedSpanEx.
//! You can add arbitrary extra information using `LocatedSpanEx`.
//! This extra property is not used when comparing two `LocatedSpanEx`s.
//!
//! ``̀`
//! use nom_locate::LocatedSpanEx;
Expand Down Expand Up @@ -126,7 +127,7 @@ pub type LocatedSpan<T> = LocatedSpanEx<T, ()>;
///
/// The `LocatedSpanEx` structure can be used as an input of the nom parsers.
/// It implements all the necessary traits for `LocatedSpanEx<&str,X>` and `LocatedSpanEx<&[u8],X>`
#[derive(PartialEq, Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct LocatedSpanEx<T, X> {
/// The offset represents the position of the fragment relatively to
/// the input of the parser. It starts at offset 0.
Expand Down Expand Up @@ -309,6 +310,14 @@ impl<T: AsBytes, X> LocatedSpanEx<T, X> {
}
}

impl<T: AsBytes + PartialEq, X> PartialEq for LocatedSpanEx<T, X> {
fn eq(&self, other: &Self) -> bool {
self.line == other.line && self.offset == other.offset && self.fragment == other.fragment
}
}

impl<T: AsBytes + Eq, X> Eq for LocatedSpanEx<T, X> {}

impl<T: AsBytes, X> AsBytes for LocatedSpanEx<T, X> {
fn as_bytes(&self) -> &[u8] {
self.fragment.as_bytes()
Expand Down
10 changes: 10 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ fn it_should_call_new_for_str_successfully() {
assert_eq!(StrSpan::new(input), output);
}

#[test]
fn it_should_ignore_extra_for_equality() {
let input = &"foobar"[..];

assert_eq!(
StrSpanEx::new_extra(input, "foo"),
StrSpanEx::new_extra(input, "bar")
);
}

#[test]
fn it_should_slice_for_str() {
let str_slice = StrSpanEx::new_extra("foobar", "extra");
Expand Down

0 comments on commit 887f7b4

Please sign in to comment.