1use alloc::borrow::Cow;
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Direction {
7 Higher,
9 Lower,
11 Unordered,
13}
14#[derive(Clone, Debug, PartialEq)]
17#[non_exhaustive]
18pub struct ScoreSpec {
19 key: Cow<'static, str>,
21 direction: Direction,
23 unit: Cow<'static, str>,
25 perfect: Option<f64>,
27 positive_infinity: bool,
29}
30impl ScoreSpec {
31 pub const fn new(key: &'static str, direction: Direction) -> Self {
33 Self {
34 key: Cow::Borrowed(key),
35 direction,
36 unit: Cow::Borrowed(""),
37 perfect: None,
38 positive_infinity: false,
39 }
40 }
41 pub fn with_key(mut self, key: impl Into<Cow<'static, str>>) -> Self {
43 self.key = key.into();
44 self
45 }
46 pub fn key(&self) -> &str {
48 &self.key
49 }
50 pub const fn direction(&self) -> Direction {
52 self.direction
53 }
54 pub fn unit(&self) -> &str {
56 &self.unit
57 }
58 pub const fn perfect(&self) -> Option<f64> {
60 self.perfect
61 }
62 pub const fn positive_infinity(&self) -> bool {
64 self.positive_infinity
65 }
66 pub const fn with_positive_infinity(mut self, allowed: bool) -> Self {
68 self.positive_infinity = allowed;
69 self
70 }
71 pub fn with_unit(mut self, unit: impl Into<Cow<'static, str>>) -> Self {
73 self.unit = unit.into();
74 self
75 }
76 pub const fn with_perfect(mut self, value: f64) -> Self {
81 assert!(
82 !value.is_nan() && value != f64::NEG_INFINITY,
83 "invalid identical-pair value"
84 );
85 self.perfect = Some(value);
86 self.positive_infinity = value == f64::INFINITY;
87 self
88 }
89}
90#[derive(Clone, Copy, Debug)]
92#[non_exhaustive]
93pub struct Score<'a> {
94 pub spec: &'a ScoreSpec,
96 pub value: f64,
98}
99impl<'a> Score<'a> {
100 pub const fn new(spec: &'a ScoreSpec, value: f64) -> Self {
102 Self { spec, value }
103 }
104}
105pub trait Evaluation {
109 fn scores(&self) -> impl Iterator<Item = Score<'_>>;
111 fn visit_scores<'a>(&'a self, visitor: &mut dyn FnMut(Score<'a>)) {
114 self.scores().for_each(visitor);
115 }
116 fn visit_maps<'a>(&'a self, visitor: &mut dyn FnMut(crate::MapView<'a>)) {
119 self.maps().for_each(visitor);
120 }
121 fn maps(&self) -> impl Iterator<Item = crate::MapView<'_>> {
125 core::iter::empty()
126 }
127}
128
129#[derive(Clone, Copy)]
133pub struct EvaluationRef<'a> {
134 inner: &'a dyn EvaluationAccess,
135}
136impl<'a> EvaluationRef<'a> {
137 pub fn new<T: Evaluation>(value: &'a T) -> Self {
139 Self { inner: value }
140 }
141 pub fn visit_scores(self, mut visitor: impl FnMut(Score<'a>)) {
143 self.inner.visit_scores(&mut visitor);
144 }
145 pub fn visit_maps(self, mut visitor: impl FnMut(crate::MapView<'a>)) {
147 self.inner.visit_maps(&mut visitor);
148 }
149}
150pub(crate) trait EvaluationAccess {
151 fn visit_scores<'a>(&'a self, visitor: &mut dyn FnMut(Score<'a>));
152 fn visit_maps<'a>(&'a self, visitor: &mut dyn FnMut(crate::MapView<'a>));
153}
154impl<T: Evaluation> EvaluationAccess for T {
155 fn visit_scores<'a>(&'a self, visitor: &mut dyn FnMut(Score<'a>)) {
156 Evaluation::visit_scores(self, visitor);
157 }
158 fn visit_maps<'a>(&'a self, visitor: &mut dyn FnMut(crate::MapView<'a>)) {
159 Evaluation::visit_maps(self, visitor);
160 }
161}