vndb_api/format/
user.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use strum::IntoEnumIterator;
4use strum_macros::EnumIter;
5
6/// Contains results of a user search
7#[derive(Deserialize, Serialize, Debug)]
8pub struct UserSearch(pub HashMap<String, Option<User>>);
9
10#[derive(Deserialize, Serialize, Debug)]
11pub struct User {
12    pub id: String,
13    pub username: String,
14    /// Searched user's number of play time votes submitted
15    pub lengthvotes: Option<u32>,
16    /// Searched user's play time votes sum in minutes
17    pub lengthvotes_sum: Option<u32>,
18}
19
20/// Contains the desired data fields for a user search
21pub struct UserSearchFields(pub Vec<UserDataField>);
22
23#[derive(Serialize, EnumIter)]
24#[serde(rename_all = "snake_case")]
25pub enum UserDataField {
26    /// The number of play time votes this user has submitted
27    Lengthvotes,
28    /// The sum of the user’s play time votes in minutes
29    LengthvotesSum,
30}
31
32impl UserSearchFields {
33    /// Include the id and username only
34    pub fn new() -> Self {
35        UserSearchFields(vec![])
36    }
37
38    /// Include the desired UserDataFields in addition to id and username
39    pub fn from(vec: Vec<UserDataField>) -> Self {
40        UserSearchFields(vec)
41    }
42
43    /// Include all UserDataFields in addition to id and username
44    pub fn all() -> Self {
45        let mut vec = Vec::with_capacity(UserDataField::iter().len());
46        for variant in UserDataField::iter() {
47            vec.push(variant);
48        }
49        UserSearchFields(vec)
50    }
51
52    /// Format UserSearchFields for user queries
53    pub fn to_csv(&self) -> String {
54        self.0
55            .iter()
56            .map(|field| serde_json::to_string(&field).unwrap().replace("\"", ""))
57            .collect::<Vec<String>>()
58            .join(",")
59    }
60}