cock_tier/modules/
user.rs1use crate::{FromString, GetVariants};
2
3#[derive(Debug, PartialEq, Clone)]
7pub struct User {
8 pub name: String,
9 pub discord_name: String,
10}
11
12#[derive(Debug, PartialEq, Clone)]
15pub enum ID {
16 Anonymous,
17 User(User),
18}
19
20impl GetVariants for ID {
22 fn get_variants() -> Vec<String> {
23 vec![String::from("Anonymous"), String::from("User")]
24 }
25}
26
27impl 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
41impl std::fmt::Display for ID {
43 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#[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}