summaryrefslogtreecommitdiffstats
path: root/third_party/rust/relevancy/src/interest.rs
blob: 0573c743fcab5994001bcce57886567d3eb79e16 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// List of possible interests for a domain.  Domains can have be associated with one or multiple
/// interests.  `Inconclusive` is used for domains in the user's top sites that we can't classify
/// because there's no corresponding entry in the interest database.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Interest {
    Animals,
    Arts,
    Autos,
    Business,
    Career,
    Education,
    Fashion,
    Finance,
    Food,
    Government,
    Health,
    Hobbies,
    Home,
    News,
    RealEstate,
    Society,
    Sports,
    Tech,
    Travel,
    Inconclusive,
}

impl From<Interest> for u32 {
    fn from(interest: Interest) -> Self {
        interest as u32
    }
}

impl From<Interest> for usize {
    fn from(interest: Interest) -> Self {
        interest as usize
    }
}

impl From<u32> for Interest {
    fn from(code: u32) -> Self {
        if code as usize > Self::COUNT {
            panic!("Invalid interest code: {code}")
        }
        // Safety: This is safe since Interest has a u32 representation and we've done a bounds
        // check
        unsafe { std::mem::transmute(code) }
    }
}

impl Interest {
    const COUNT: usize = 20;

    pub fn all() -> [Interest; Self::COUNT] {
        [
            Self::Animals,
            Self::Arts,
            Self::Autos,
            Self::Business,
            Self::Career,
            Self::Education,
            Self::Fashion,
            Self::Finance,
            Self::Food,
            Self::Government,
            Self::Health,
            Self::Hobbies,
            Self::Home,
            Self::News,
            Self::RealEstate,
            Self::Society,
            Self::Sports,
            Self::Tech,
            Self::Travel,
            Self::Inconclusive,
        ]
    }
}

/// Vector storing a count value for each interest
///
/// Here "vector" refers to the mathematical object, not a Rust `Vec`.  It always has a fixed
/// number of elements.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct InterestVector {
    pub animals: u32,
    pub arts: u32,
    pub autos: u32,
    pub business: u32,
    pub career: u32,
    pub education: u32,
    pub fashion: u32,
    pub finance: u32,
    pub food: u32,
    pub government: u32,
    pub health: u32,
    pub hobbies: u32,
    pub home: u32,
    pub news: u32,
    pub real_estate: u32,
    pub society: u32,
    pub sports: u32,
    pub tech: u32,
    pub travel: u32,
    pub inconclusive: u32,
}

impl std::ops::Index<Interest> for InterestVector {
    type Output = u32;

    fn index(&self, index: Interest) -> &u32 {
        match index {
            Interest::Animals => &self.animals,
            Interest::Arts => &self.arts,
            Interest::Autos => &self.autos,
            Interest::Business => &self.business,
            Interest::Career => &self.career,
            Interest::Education => &self.education,
            Interest::Fashion => &self.fashion,
            Interest::Finance => &self.finance,
            Interest::Food => &self.food,
            Interest::Government => &self.government,
            Interest::Health => &self.health,
            Interest::Hobbies => &self.hobbies,
            Interest::Home => &self.home,
            Interest::News => &self.news,
            Interest::RealEstate => &self.real_estate,
            Interest::Society => &self.society,
            Interest::Sports => &self.sports,
            Interest::Tech => &self.tech,
            Interest::Travel => &self.travel,
            Interest::Inconclusive => &self.inconclusive,
        }
    }
}

impl std::ops::IndexMut<Interest> for InterestVector {
    fn index_mut(&mut self, index: Interest) -> &mut u32 {
        match index {
            Interest::Animals => &mut self.animals,
            Interest::Arts => &mut self.arts,
            Interest::Autos => &mut self.autos,
            Interest::Business => &mut self.business,
            Interest::Career => &mut self.career,
            Interest::Education => &mut self.education,
            Interest::Fashion => &mut self.fashion,
            Interest::Finance => &mut self.finance,
            Interest::Food => &mut self.food,
            Interest::Government => &mut self.government,
            Interest::Health => &mut self.health,
            Interest::Hobbies => &mut self.hobbies,
            Interest::Home => &mut self.home,
            Interest::News => &mut self.news,
            Interest::RealEstate => &mut self.real_estate,
            Interest::Society => &mut self.society,
            Interest::Sports => &mut self.sports,
            Interest::Tech => &mut self.tech,
            Interest::Travel => &mut self.travel,
            Interest::Inconclusive => &mut self.inconclusive,
        }
    }
}