summaryrefslogtreecommitdiff
path: root/src/core/score.rs
blob: 8ab2c25186e791a87cd4fdab7bd2967c8791534b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/// Tracks the best scoring match for license similarity
#[derive(Debug, Clone, PartialEq)]
pub struct Score {
    pub license_id: String,
    pub score: f64,
}

impl Score {
    /// Create a new Score with the given license ID and score
    pub fn new(license_id: String, score: f64) -> Self {
        Self { license_id, score }
    }

    /// Create a Score from string slice
    pub fn from_str(license_id: &str, score: f64) -> Self {
        Self::new(license_id.to_string(), score)
    }

    /// Get the license ID
    pub fn license_id(&self) -> &str {
        &self.license_id
    }

    /// Get the score
    pub fn score(&self) -> f64 {
        self.score
    }

    /// Check if this score is better (higher) than another
    pub fn is_better_than(&self, other: &Score) -> bool {
        self.score > other.score
    }

    /// Check if this score meets or exceeds a threshold
    pub fn meets_threshold(&self, threshold: f64) -> bool {
        self.score >= threshold
    }

    /// Update the score if the new score is better
    pub fn update_if_better(&mut self, license_id: String, score: f64) -> bool {
        if score > self.score {
            self.license_id = license_id;
            self.score = score;
            true
        } else {
            false
        }
    }

    /// Create a zero score (useful for initialization)
    pub fn zero() -> Self {
        Self::new("unknown".to_string(), 0.0)
    }

    /// Check if this is a zero score
    pub fn is_zero(&self) -> bool {
        self.score == 0.0
    }

    /// Check if this score indicates a perfect match
    pub fn is_perfect(&self) -> bool {
        (self.score - 100.0).abs() < f64::EPSILON
    }

    /// Get score as a percentage string
    pub fn as_percentage(&self) -> String {
        format!("{:.1}%", self.score)
    }
}

impl Default for Score {
    fn default() -> Self {
        Self::zero()
    }
}

impl std::fmt::Display for Score {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {:.1}%", self.license_id, self.score)
    }
}

impl PartialOrd for Score {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.score.partial_cmp(&other.score)
    }
}

impl From<(String, f64)> for Score {
    fn from((license_id, score): (String, f64)) -> Self {
        Self::new(license_id, score)
    }
}

impl From<(&str, f64)> for Score {
    fn from((license_id, score): (&str, f64)) -> Self {
        Self::from_str(license_id, score)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_score_creation() {
        let score = Score::new("MIT".to_string(), 85.5);
        assert_eq!(score.license_id(), "MIT");
        assert_eq!(score.score(), 85.5);
    }

    #[test]
    fn test_from_str() {
        let score = Score::from_str("Apache-2.0", 90.0);
        assert_eq!(score.license_id(), "Apache-2.0");
        assert_eq!(score.score(), 90.0);
    }

    #[test]
    fn test_is_better_than() {
        let score1 = Score::new("MIT".to_string(), 85.0);
        let score2 = Score::new("Apache-2.0".to_string(), 90.0);
        let score3 = Score::new("BSD".to_string(), 80.0);

        assert!(score2.is_better_than(&score1));
        assert!(score1.is_better_than(&score3));
        assert!(!score1.is_better_than(&score2));
    }

    #[test]
    fn test_meets_threshold() {
        let score = Score::new("MIT".to_string(), 85.0);
        
        assert!(score.meets_threshold(80.0));
        assert!(score.meets_threshold(85.0));
        assert!(!score.meets_threshold(90.0));
    }

    #[test]
    fn test_update_if_better() {
        let mut score = Score::new("MIT".to_string(), 80.0);
        
        // Better score should update
        assert!(score.update_if_better("Apache-2.0".to_string(), 90.0));
        assert_eq!(score.license_id(), "Apache-2.0");
        assert_eq!(score.score(), 90.0);
        
        // Worse score should not update
        assert!(!score.update_if_better("BSD".to_string(), 85.0));
        assert_eq!(score.license_id(), "Apache-2.0");
        assert_eq!(score.score(), 90.0);
        
        // Equal score should not update
        assert!(!score.update_if_better("GPL".to_string(), 90.0));
        assert_eq!(score.license_id(), "Apache-2.0");
        assert_eq!(score.score(), 90.0);
    }

    #[test]
    fn test_zero_score() {
        let score = Score::zero();
        assert_eq!(score.license_id(), "unknown");
        assert_eq!(score.score(), 0.0);
        assert!(score.is_zero());
        assert!(!score.is_perfect());
    }

    #[test]
    fn test_default() {
        let score = Score::default();
        assert_eq!(score.license_id(), "unknown");
        assert_eq!(score.score(), 0.0);
        assert!(score.is_zero());
    }

    #[test]
    fn test_is_perfect() {
        let perfect_score = Score::new("MIT".to_string(), 100.0);
        let imperfect_score = Score::new("Apache-2.0".to_string(), 99.9);
        
        assert!(perfect_score.is_perfect());
        assert!(!imperfect_score.is_perfect());
    }

    #[test]
    fn test_as_percentage() {
        let score = Score::new("MIT".to_string(), 85.6789);
        assert_eq!(score.as_percentage(), "85.7%");
    }

    #[test]
    fn test_display() {
        let score = Score::new("MIT".to_string(), 85.6);
        assert_eq!(format!("{}", score), "MIT: 85.6%");
    }

    #[test]
    fn test_partial_ord() {
        let score1 = Score::new("MIT".to_string(), 80.0);
        let score2 = Score::new("Apache-2.0".to_string(), 90.0);
        let score3 = Score::new("BSD".to_string(), 80.0);

        assert!(score2 > score1);
        assert!(score1 < score2);
        assert!(score1 == score3); // Note: PartialEq compares both fields, PartialOrd only score
    }

    #[test]
    fn test_from_tuple() {
        let score1 = Score::from(("MIT".to_string(), 85.0));
        let score2 = Score::from(("Apache-2.0", 90.0));
        
        assert_eq!(score1.license_id(), "MIT");
        assert_eq!(score1.score(), 85.0);
        
        assert_eq!(score2.license_id(), "Apache-2.0");
        assert_eq!(score2.score(), 90.0);
    }

    #[test]
    fn test_edge_cases() {
        let zero_score = Score::new("Zero".to_string(), 0.0);
        let negative_score = Score::new("Negative".to_string(), -10.0);
        let over_hundred = Score::new("Over".to_string(), 150.0);
        
        assert!(zero_score.is_zero());
        assert!(!negative_score.is_zero());
        assert!(!over_hundred.is_perfect());
        
        assert!(over_hundred.is_better_than(&zero_score));
        assert!(!negative_score.meets_threshold(0.0));
    }
}