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

ambee/giterated

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

Add docs

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨51aad53

⁨src/model/user.rs⁩ - ⁨1472⁩ bytes
Raw
1 use std::fmt::{Display, Formatter};
2 use std::str::FromStr;
3
4 use serde::{Deserialize, Serialize};
5
6 use super::instance::Instance;
7
8 /// A user, defined by its username and instance.
9 ///
10 /// # Textual Format
11 /// A user's textual reference is defined as:
12 ///
13 /// `{username: String}:{instance: Instance}`
14 ///
15 /// # Examples
16 /// For the user with the username `barson` and the instance `giterated.dev`,
17 /// the following [`User`] initialization would be valid:
18 ///
19 /// ```
20 /// let user = User {
21 /// username: String::from("barson"),
22 /// instance: Instance::from_str("giterated.dev").unwrap()
23 /// };
24 ///
25 /// // This is correct
26 /// assert_eq!(User::from_str("barson:giterated.dev").unwrap(), user);
27 /// ```
28 #[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
29 pub struct User {
30 pub username: String,
31 pub instance: Instance,
32 }
33
34 impl Display for User {
35 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{}:{}", self.username, self.instance.url)
37 }
38 }
39
40 impl From<String> for User {
41 fn from(user_string: String) -> Self {
42 User::from_str(&user_string).unwrap()
43 }
44 }
45
46 impl FromStr for User {
47 type Err = ();
48
49 fn from_str(s: &str) -> Result<Self, Self::Err> {
50 let mut colon_split = s.split(':');
51 let username = colon_split.next().unwrap().to_string();
52 let instance = Instance::from_str(colon_split.next().unwrap()).unwrap();
53
54 Ok(Self { username, instance })
55 }
56 }
57