1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct SimilarityRatio {
7 scale: f64,
8}
9
10impl SimilarityRatio {
11 #[must_use]
13 pub const fn new(scale: f64) -> Option<Self> {
14 if scale.is_finite() && scale > 0.0 {
15 Some(Self { scale })
16 } else {
17 None
18 }
19 }
20
21 #[must_use]
23 pub const fn scale(self) -> f64 {
24 self.scale
25 }
26
27 #[must_use]
29 pub fn apply_length(self, length: f64) -> f64 {
30 length * self.scale
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use super::SimilarityRatio;
37
38 #[test]
39 fn scales_lengths() {
40 let ratio = SimilarityRatio::new(2.0).expect("valid ratio");
41
42 assert_eq!(ratio.scale(), 2.0);
43 assert_eq!(ratio.apply_length(3.0), 6.0);
44 assert_eq!(SimilarityRatio::new(0.0), None);
45 }
46}