near_enough/
lib.rs

1//! Sometimes, in programming as in life, it's more useful
2//! to return the closest match to a given search key than
3//! it is to return no result at all. This crate provides a
4//! (small) selection of traits and default implementations to
5//! facilitate this.
6//! 
7//! Providing complex diffing algorithms is not a goal of this
8//! crate, but simple utilities (for example finding the closest
9//! integer in a set of values) are.
10//! 
11//! ## Simple example with usize:
12//! ```
13//! use near_enough::Closest;
14//! 
15//! let values: [usize; 5] = [0, 1, 3, 5, 9];
16//! assert_eq!(values.closest(&2), Some(&1));
17//! ```
18//! 
19//! ## More complex example with custom type:
20//! ```
21//! use near_enough::Closest;
22//! use near_enough::Diff;
23//! 
24//! #[derive(Debug, Eq, PartialEq)]
25//! struct LengthString(String);
26//! 
27//! impl Diff for LengthString {
28//!     type Output = usize;
29//! 
30//!     fn diff(&self, other: &Self) -> usize {
31//!         self.0.len().diff(&other.0.len())
32//!     }
33//! }
34//! 
35//! let values: [LengthString; 3] = [
36//!     LengthString("a".to_string()),
37//!     LengthString("bb".to_string()),
38//!     LengthString("dddd".to_string()),
39//! ];
40//! assert_eq!(
41//!     values.closest(&LengthString("ccc".to_string())),
42//!     Some(&LengthString("bb".to_string()))
43//! );
44//! ```
45
46mod closest;
47mod diff;
48
49pub use closest::Closest;
50pub use diff::Diff;