generate_test_data/
generate-test-data.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use relevancy::{
6    url_hash::{hash_url, UrlHash},
7    Interest,
8};
9use std::{collections::HashMap, fs::File, io::Write};
10
11// Generate a set of test data and output it to the `test-data` file.
12//
13// This is meant to be a placeholder until we can get this data stored in remote settings.
14
15const TEST_INTEREST_DATA: &[(&str, Interest)] = &[
16    ("https://espn.com/", Interest::Sports),
17    ("https://dogs.com/", Interest::Animals),
18    ("https://cars.com/", Interest::Autos),
19    ("https://www.vouge.com/", Interest::Fashion),
20    ("https://slashdot.org/", Interest::Tech),
21    ("https://www.nascar.com/", Interest::Autos),
22    ("https://www.nascar.com/", Interest::Sports),
23];
24
25fn main() {
26    let mut interest_map: HashMap<Interest, Vec<UrlHash>> =
27        HashMap::from_iter(Interest::all().into_iter().map(|i| (i, vec![])));
28    for (url, interest) in TEST_INTEREST_DATA {
29        if let Some(hash) = hash_url(url) {
30            interest_map.get_mut(interest).unwrap().push(hash)
31        }
32    }
33
34    let mut f = File::create("test-data").expect("Error opening file");
35    // Loop over all possible interests
36    for interest in Interest::all() {
37        // Get the list of URL hashes for that interest
38        let hashes = interest_map.get(&interest).unwrap();
39        // Write the count
40        f.write_all(&(hashes.len() as u32).to_le_bytes())
41            .expect("Error writing file");
42        // Write the hashes
43        for hash in hashes {
44            f.write_all(hash).expect("Error writing file");
45        }
46    }
47}