cock_tier/modules/
user.rs

1use crate::{FromString, GetVariants};
2
3/// Represents a [User] of the cock analyzer.
4/// `name` field is the name of the user.
5/// `discord_name` field is the discord name of the user.
6#[derive(Debug, PartialEq, Clone)]
7pub struct User {
8    pub name: String,
9    pub discord_name: String,
10}
11
12/// Represents a unique identifier for a user, their [ID].
13/// Variants include an anonymous identifier or a specific user.
14#[derive(Debug, PartialEq, Clone)]
15pub enum ID {
16    Anonymous,
17    User(User),
18}
19
20/// Implementing [GetVariants] for [ID] enum to provide the different variant options for [ID].
21impl GetVariants for ID {
22    fn get_variants() -> Vec<String> {
23        vec![String::from("Anonymous"), String::from("User")]
24    }
25}
26
27/// Implementing [FromString] for [ID] enum to create an [ID] instance from a string.
28impl FromString for ID {
29    fn from_string(id: &str) -> ID {
30        match id {
31            "Anonymous" => ID::Anonymous,
32            "User" => ID::User(User {
33                name: String::from(""),
34                discord_name: String::from(""),
35            }),
36            _ => panic!("Invalid ID"),
37        }
38    }
39}
40
41/// Implementing [std::fmt::Display] trait for [ID] for formatted print.
42impl std::fmt::Display for ID {
43    /// Returnis a string representation of the [ID] variant.
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            ID::Anonymous => write!(f, "Anonymous User"),
47            ID::User(user) => write!(
48                f,
49                "Username: {}\nDiscord name: {}",
50                user.name, user.discord_name
51            ),
52        }
53    }
54}
55
56/// Tests for the [User] and [ID] structs
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_user() {
63        let user = User {
64            name: String::from("S"),
65            discord_name: String::from("test"),
66        };
67
68        assert_eq!(user.name, "S");
69        assert_eq!(user.discord_name, String::from("test"));
70    }
71
72    #[test]
73    fn test_id() {
74        let user = User {
75            name: String::from("S"),
76            discord_name: String::from("test"),
77        };
78
79        let id = ID::User(user);
80
81        match id {
82            ID::User(user) => {
83                assert_eq!(user.name, "S");
84                assert_eq!(user.discord_name, String::from("test"));
85            }
86            _ => panic!("Expected ID::User"),
87        }
88    }
89}