diff_priv/test/
adult.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3use bimap::BiMap;
4use uuid::Uuid;
5
6use crate::data_manipulation::anonymizable::{
7    Anonymizable, QuasiIdentifierType, QuasiIdentifierTypes, SensitiveAttribute,
8};
9
10lazy_static! {
11    static ref CLASS_BIMAP: BiMap<&'static str, i32> =
12        BiMap::from_iter(vec![("<=50K", 0), (">50K", 1),]);
13}
14
15#[derive(Debug, Serialize, Clone, Deserialize)]
16pub struct Adult {
17    timestamp: i32,
18    age: i32,
19    fnlwgt: i32,
20    education_num: i32,
21    capital_gain: i32,
22    capital_loss: i32,
23    hours_per_week: i32,
24    class: String,
25    #[serde(skip_deserializing, default = "default_time")]
26    time_generated: SystemTime,
27}
28
29fn default_time() -> SystemTime {
30    SystemTime::now()
31}
32
33impl Default for Adult {
34    fn default() -> Self {
35        Self {
36            timestamp: 0,
37            age: 0,
38            fnlwgt: 0,
39            education_num: 0,
40            capital_gain: 0,
41            capital_loss: 0,
42            hours_per_week: 0,
43            class: "".to_string(),
44            time_generated: SystemTime::now(),
45        }
46    }
47}
48
49impl Adult {
50    fn get_age_qi(&self) -> QuasiIdentifierTypes {
51        QuasiIdentifierTypes::Interval((
52            QuasiIdentifierType::Integer(self.age),
53            QuasiIdentifierType::Integer(1),
54            QuasiIdentifierType::Integer(100),
55            1,
56        ))
57    }
58
59    fn get_fnlwgt_qi(&self) -> QuasiIdentifierTypes {
60        QuasiIdentifierTypes::Interval((
61            QuasiIdentifierType::Integer(self.fnlwgt),
62            QuasiIdentifierType::Integer(1),
63            QuasiIdentifierType::Integer(1500000),
64            1,
65        ))
66    }
67
68    fn get_education_num_qi(&self) -> QuasiIdentifierTypes {
69        QuasiIdentifierTypes::Interval((
70            QuasiIdentifierType::Integer(self.education_num),
71            QuasiIdentifierType::Integer(1),
72            QuasiIdentifierType::Integer(20),
73            1,
74        ))
75    }
76
77    fn get_capital_gain_qi(&self) -> QuasiIdentifierTypes {
78        QuasiIdentifierTypes::Interval((
79            QuasiIdentifierType::Integer(self.capital_gain),
80            QuasiIdentifierType::Integer(0),
81            QuasiIdentifierType::Integer(100000),
82            1,
83        ))
84    }
85
86    fn get_capital_loss_qi(&self) -> QuasiIdentifierTypes {
87        QuasiIdentifierTypes::Interval((
88            QuasiIdentifierType::Integer(self.capital_loss),
89            QuasiIdentifierType::Integer(0),
90            QuasiIdentifierType::Integer(5000),
91            1,
92        ))
93    }
94
95    fn get_hours_per_week_qi(&self) -> QuasiIdentifierTypes {
96        QuasiIdentifierTypes::Interval((
97            QuasiIdentifierType::Integer(self.hours_per_week),
98            QuasiIdentifierType::Integer(1),
99            QuasiIdentifierType::Integer(100),
100            1,
101        ))
102    }
103}
104
105impl Anonymizable for Adult {
106    fn quasi_identifiers(&self) -> Vec<QuasiIdentifierTypes> {
107        let age = self.get_age_qi();
108        let fnlwgt = self.get_fnlwgt_qi();
109        let education_num = self.get_education_num_qi();
110        let capital_gain = self.get_capital_gain_qi();
111        let capital_loss = self.get_capital_loss_qi();
112        let hours_per_week = self.get_hours_per_week_qi();
113
114        vec![
115            age,
116            fnlwgt,
117            education_num,
118            capital_gain,
119            capital_loss,
120            hours_per_week,
121        ]
122    }
123
124    fn update_quasi_identifiers(&self, mut qi: Vec<QuasiIdentifierTypes>) -> Self {
125        if let (
126            QuasiIdentifierType::Integer(hours_per_week),
127            QuasiIdentifierType::Integer(capital_loss),
128            QuasiIdentifierType::Integer(capital_gain),
129            QuasiIdentifierType::Integer(education_num),
130            QuasiIdentifierType::Integer(fnlwgt),
131            QuasiIdentifierType::Integer(age),
132        ) = (
133            qi.pop().unwrap().extract_value(),
134            qi.pop().unwrap().extract_value(),
135            qi.pop().unwrap().extract_value(),
136            qi.pop().unwrap().extract_value(),
137            qi.pop().unwrap().extract_value(),
138            qi.pop().unwrap().extract_value(),
139        ) {
140            Self {
141                timestamp: self.timestamp,
142                age,
143                fnlwgt,
144                education_num,
145                capital_gain,
146                capital_loss,
147                hours_per_week,
148                class: self.class.to_owned(),
149                time_generated: self.time_generated,
150            }
151        } else {
152            panic!("Couldn't Adult with QI's")
153        }
154    }
155
156    fn sensitive_value(&self) -> SensitiveAttribute {
157        SensitiveAttribute::String(self.class.to_owned())
158    }
159
160    fn extract_string_values(&self, uuid: Uuid, dr: f64) -> Vec<String> {
161        vec![
162            uuid.to_string(),
163            dr.to_string(),
164            self.timestamp.to_string(),
165            self.age.to_string(),
166            self.fnlwgt.to_string(),
167            self.education_num.to_string(),
168            self.capital_gain.to_string(),
169            self.capital_loss.to_string(),
170            self.hours_per_week.to_string(),
171            self.class.to_owned(),
172        ]
173    }
174
175    fn get_timestamp(&self) -> SystemTime {
176        self.time_generated
177    }
178}