os_checker_plugin_cargo/
database.rs1use indexmap::Equivalent;
2use plugin::prelude::*;
3use serde::Deserialize;
4use std::{hash::Hash, sync::LazyLock};
5
6const URL: &str = "https://raw.githubusercontent.com/os-checker/database/refs/heads/main/ui/home/split/All-Targets.json";
8
9#[derive(Debug, Deserialize)]
10pub struct Item {
11 children: Vec<Child>,
12}
13
14#[derive(Debug, Deserialize)]
15pub struct Child {
16 data: Data,
17}
18
19#[derive(Debug, Deserialize)]
20pub struct Data {
21 user: String,
22 repo: String,
23 pkg: String,
24 total_count: usize,
25}
26
27#[derive(Debug, Deserialize)]
28#[serde(from = "Vec<Item>")]
29pub struct DiagnosticsCount {
30 map: IndexMap<Key, usize>,
31}
32
33impl DiagnosticsCount {
34 fn new() -> Result<Self> {
35 let json = duct::cmd!("wget", URL, "-O", "-").read()?;
36 Ok(serde_json::from_str(&json)?)
37 }
38}
39
40impl From<Vec<Item>> for DiagnosticsCount {
41 fn from(value: Vec<Item>) -> Self {
42 DiagnosticsCount {
43 map: value
44 .into_iter()
45 .flat_map(|val| {
46 val.children.into_iter().map(|child| {
47 let Data {
48 user,
49 repo,
50 pkg,
51 total_count,
52 } = child.data;
53 (Key { user, repo, pkg }, total_count)
54 })
55 })
56 .collect(),
57 }
58 }
59}
60
61#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
62struct Key {
63 user: String,
64 repo: String,
65 pkg: String,
66}
67
68impl Hash for Key {
69 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
70 [&*self.user, &*self.repo, &*self.pkg].hash(state);
71 }
72}
73
74impl Equivalent<Key> for [&'_ str; 3] {
75 fn equivalent(&self, key: &Key) -> bool {
76 let &[user, repo, pkg] = self;
77 user == key.user && repo == key.repo && pkg == key.pkg
78 }
79}
80
81pub static DIAGNOSTICS_COUNT: LazyLock<DiagnosticsCount> =
82 LazyLock::new(|| DiagnosticsCount::new().unwrap());
83
84#[test]
85fn test_diagnostics_count() {
86 dbg!(&*DIAGNOSTICS_COUNT);
87}
88
89pub fn diag_total_count(key: [&str; 3]) -> Option<usize> {
90 DIAGNOSTICS_COUNT.map.get(&key).copied()
91}