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

ambee/giterated

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

More progress :)

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨92c3f32

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