Skip to content

Canonicalize all_equal_value's error #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/all_equal_value_err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[cfg(doc)]
use crate::Itertools;
#[cfg(feature = "use_std")]
use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};

/// Value returned for the error case of [`Itertools::all_equal_value`].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct AllEqualValueError<Item>(pub Option<[Item; 2]>);

impl<Item> Display for AllEqualValueError<Item> {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self.0 {

Check warning on line 13 in src/all_equal_value_err.rs

View check run for this annotation

Codecov / codecov/patch

src/all_equal_value_err.rs#L12-L13

Added lines #L12 - L13 were not covered by tests
None => {
write!(
f,
"got zero elements when all elements were expected to be equal"

Check warning on line 17 in src/all_equal_value_err.rs

View check run for this annotation

Codecov / codecov/patch

src/all_equal_value_err.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
)
}
Some([_, _]) => {
write!(
f,
"got different elements when all elements were expected to be equal"

Check warning on line 23 in src/all_equal_value_err.rs

View check run for this annotation

Codecov / codecov/patch

src/all_equal_value_err.rs#L21-L23

Added lines #L21 - L23 were not covered by tests
)
}
}
}

Check warning on line 27 in src/all_equal_value_err.rs

View check run for this annotation

Codecov / codecov/patch

src/all_equal_value_err.rs#L27

Added line #L27 was not covered by tests
}

#[cfg(feature = "use_std")]
impl<Item> Error for AllEqualValueError<Item> where Item: Debug {}
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub mod structs {
FilterOk, Interleave, InterleaveShortest, MapInto, MapOk, Positions, Product, PutBack,
TakeWhileRef, TupleCombinations, Update, WhileSome,
};
pub use crate::all_equal_value_err::AllEqualValueError;
#[cfg(feature = "use_alloc")]
pub use crate::combinations::{ArrayCombinations, Combinations};
#[cfg(feature = "use_alloc")]
Expand Down Expand Up @@ -177,6 +178,7 @@ pub use crate::either_or_both::EitherOrBoth;
pub mod free;
#[doc(inline)]
pub use crate::free::*;
mod all_equal_value_err;
#[cfg(feature = "use_alloc")]
mod combinations;
#[cfg(feature = "use_alloc")]
Expand Down Expand Up @@ -2232,27 +2234,27 @@ pub trait Itertools: Iterator {
/// two non-equal elements found.
///
/// ```
/// use itertools::Itertools;
/// use itertools::{Itertools, AllEqualValueError};
///
/// let data = vec![1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5];
/// assert_eq!(data.iter().all_equal_value(), Err(Some((&1, &2))));
/// assert_eq!(data.iter().all_equal_value(), Err(AllEqualValueError(Some([&1, &2]))));
/// assert_eq!(data[0..3].iter().all_equal_value(), Ok(&1));
/// assert_eq!(data[3..5].iter().all_equal_value(), Ok(&2));
/// assert_eq!(data[5..8].iter().all_equal_value(), Ok(&3));
///
/// let data : Option<usize> = None;
/// assert_eq!(data.into_iter().all_equal_value(), Err(None));
/// assert_eq!(data.into_iter().all_equal_value(), Err(AllEqualValueError(None)));
/// ```
#[allow(clippy::type_complexity)]
fn all_equal_value(&mut self) -> Result<Self::Item, Option<(Self::Item, Self::Item)>>
fn all_equal_value(&mut self) -> Result<Self::Item, AllEqualValueError<Self::Item>>
where
Self: Sized,
Self::Item: PartialEq,
{
let first = self.next().ok_or(None)?;
let first = self.next().ok_or(AllEqualValueError(None))?;
let other = self.find(|x| x != &first);
if let Some(other) = other {
Err(Some((first, other)))
Err(AllEqualValueError(Some([first, other])))
} else {
Ok(first)
}
Expand Down
10 changes: 7 additions & 3 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::it::multipeek;
use crate::it::multizip;
use crate::it::peek_nth;
use crate::it::repeat_n;
use crate::it::AllEqualValueError;
use crate::it::ExactlyOneError;
use crate::it::FoldWhile;
use crate::it::Itertools;
Expand Down Expand Up @@ -282,14 +283,17 @@ fn all_equal() {

#[test]
fn all_equal_value() {
assert_eq!("".chars().all_equal_value(), Err(None));
assert_eq!("".chars().all_equal_value(), Err(AllEqualValueError(None)));
assert_eq!("A".chars().all_equal_value(), Ok('A'));
assert_eq!("AABBCCC".chars().all_equal_value(), Err(Some(('A', 'B'))));
assert_eq!(
"AABBCCC".chars().all_equal_value(),
Err(AllEqualValueError(Some(['A', 'B'])))
);
assert_eq!("AAAAAAA".chars().all_equal_value(), Ok('A'));
{
let mut it = [1, 2, 3].iter().copied();
let result = it.all_equal_value();
assert_eq!(result, Err(Some((1, 2))));
assert_eq!(result, Err(AllEqualValueError(Some([1, 2]))));
let remaining = it.next();
assert_eq!(remaining, Some(3));
assert!(it.next().is_none());
Expand Down
Loading