JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Progress on refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c9f076f

⁨giterated-models/src/model/user.rs⁩ - ⁨2093⁩ bytes
Raw
1 use std::fmt::{Display, Formatter};
2 use std::str::FromStr;
3
4 use secrecy::{CloneableSecret, DebugSecret, SerializableSecret, Zeroize};
5 use serde::{Deserialize, Serialize};
6
7 use crate::operation::GiteratedObject;
8
9 use super::instance::Instance;
10
11 /// A user, defined by its username and instance.
12 ///
13 /// # Textual Format
14 /// A user's textual reference is defined as:
15 ///
16 /// `{username: String}:{instance: Instance}`
17 ///
18 /// # Examples
19 /// For the user with the username `barson` and the instance `giterated.dev`,
20 /// the following [`User`] initialization would be valid:
21 ///
22 /// ```
23 /// let user = User {
24 /// username: String::from("barson"),
25 /// instance: Instance::from_str("giterated.dev").unwrap()
26 /// };
27 ///
28 /// // This is correct
29 /// assert_eq!(User::from_str("barson:giterated.dev").unwrap(), user);
30 /// ```
31 #[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
32 pub struct User {
33 pub username: String,
34 pub instance: Instance,
35 }
36
37 impl GiteratedObject for User {
38 fn object_name() -> &'static str {
39 "user"
40 }
41
42 fn from_object_str(object_str: &str) -> Result<Self, anyhow::Error> {
43 Ok(User::from_str(object_str).unwrap())
44 }
45 }
46
47 impl Display for User {
48 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49 write!(f, "{}:{}", self.username, self.instance.url)
50 }
51 }
52
53 impl From<String> for User {
54 fn from(user_string: String) -> Self {
55 User::from_str(&user_string).unwrap()
56 }
57 }
58
59 impl FromStr for User {
60 type Err = ();
61
62 fn from_str(s: &str) -> Result<Self, Self::Err> {
63 let mut colon_split = s.split(':');
64 let username = colon_split.next().unwrap().to_string();
65 let instance = Instance::from_str(colon_split.next().unwrap()).unwrap();
66
67 Ok(Self { username, instance })
68 }
69 }
70
71 #[derive(Clone, Debug, Serialize, Deserialize)]
72 pub struct Password(pub String);
73
74 impl Zeroize for Password {
75 fn zeroize(&mut self) {
76 self.0.zeroize()
77 }
78 }
79
80 impl SerializableSecret for Password {}
81 impl CloneableSecret for Password {}
82 impl DebugSecret for Password {}
83