Skip to content

Commit

Permalink
Implement PartialEq for WordSeparator and WrapAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeisler committed Oct 23, 2022
1 parent ff039c2 commit 2fcefcc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/word_separators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@ pub enum WordSeparator {
Custom(fn(line: &str) -> Box<dyn Iterator<Item = Word<'_>> + '_>),
}

impl PartialEq for WordSeparator {
/// Compare two word separators.
///
/// ```
/// use textwrap::WordSeparator;
///
/// assert_eq!(WordSeparator::AsciiSpace, WordSeparator::AsciiSpace);
/// #[cfg(feature = "unicode-linebreak")] {
/// assert_eq!(WordSeparator::UnicodeBreakProperties,
/// WordSeparator::UnicodeBreakProperties);
/// }
/// ```
///
/// Note that `WordSeparator::Custom` values never compare equal:
///
/// ```
/// use textwrap::WordSeparator;
/// use textwrap::core::Word;
/// fn word_separator(line: &str) -> Box<dyn Iterator<Item = Word<'_>> + '_> {
/// Box::new(line.split_inclusive(' ').map(Word::from))
/// }
/// assert_ne!(WordSeparator::Custom(word_separator),
/// WordSeparator::Custom(word_separator));
/// ```
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(WordSeparator::AsciiSpace, WordSeparator::AsciiSpace) => true,
#[cfg(feature = "unicode-linebreak")]
(WordSeparator::UnicodeBreakProperties, WordSeparator::UnicodeBreakProperties) => true,
(_, _) => false,
}
}
}

impl std::fmt::Debug for WordSeparator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
30 changes: 30 additions & 0 deletions src/wrap_algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ pub enum WrapAlgorithm {
Custom(for<'a, 'b> fn(words: &'b [Word<'a>], line_widths: &'b [usize]) -> Vec<&'b [Word<'a>]>),
}

impl PartialEq for WrapAlgorithm {
/// Compare two wrap algorithms.
///
/// ```
/// use textwrap::WrapAlgorithm;
///
/// assert_eq!(WrapAlgorithm::FirstFit, WrapAlgorithm::FirstFit);
/// #[cfg(feature = "smawk")] {
/// assert_eq!(WrapAlgorithm::new_optimal_fit(), WrapAlgorithm::new_optimal_fit());
/// }
/// ```
///
/// Note that `WrapAlgorithm::Custom1` values never compare equal:
///
/// ```
/// use textwrap::WrapAlgorithm;
///
/// assert_ne!(WrapAlgorithm::Custom(|words, line_widths| vec![words]),
/// WrapAlgorithm::Custom(|words, line_widths| vec![words]));
/// ```
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(WrapAlgorithm::FirstFit, WrapAlgorithm::FirstFit) => true,
#[cfg(feature = "smawk")]
(WrapAlgorithm::OptimalFit(a), WrapAlgorithm::OptimalFit(b)) => a == b,
(_, _) => false,
}
}
}

impl std::fmt::Debug for WrapAlgorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/wrap_algorithms/optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::core::Fragment;
///
/// **Note:** Only available when the `smawk` Cargo feature is
/// enabled.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Penalties {
/// Per-line penalty. This is added for every line, which makes it
/// expensive to output more lines than the minimum required.
Expand Down

0 comments on commit 2fcefcc

Please sign in to comment.