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

ambee/giterated

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

Implement Display for RepositoryVisibility

Type: Chore

emilia - ⁨2⁩ years ago

parent: tbd commit: ⁨b5a1639

⁨src/model/repository.rs⁩ - ⁨5893⁩ 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, user::User};
7
8 /// A repository, defined by the instance it exists on along with
9 /// its owner and name.
10 ///
11 /// # Textual Format
12 /// A repository's textual reference is defined as:
13 ///
14 /// `{owner: User}/{name: String}@{instance: Instance}`
15 ///
16 /// # Examples
17 /// For the repository named `foo` owned by `barson:giterated.dev` on the instance
18 /// `giterated.dev`, the following [`Repository`] initialization would
19 /// be valid:
20 ///
21 /// ```
22 /// let repository = Repository {
23 /// owner: User::from_str("barson:giterated.dev").unwrap(),
24 /// name: String::from("foo"),
25 /// instance: Instance::from_str("giterated.dev").unwrap()
26 /// };
27 ///
28 /// // This is correct
29 /// assert_eq!(Repository::from_str("barson:giterated.dev/[email protected]").unwrap(), repository);
30 /// ```
31 #[derive(Hash, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
32 pub struct Repository {
33 pub owner: User,
34 pub name: String,
35 /// Instance the repository is on
36 pub instance: Instance,
37 }
38
39 impl ToString for Repository {
40 fn to_string(&self) -> String {
41 format!("{}/{}@{}", self.owner, self.name, self.instance.to_string())
42 }
43 }
44
45 impl FromStr for Repository {
46 type Err = ();
47
48 fn from_str(s: &str) -> Result<Self, Self::Err> {
49 let mut by_ampersand = s.split('@');
50 let mut path_split = by_ampersand.next().unwrap().split('/');
51
52 let instance = Instance::from_str(by_ampersand.next().unwrap()).unwrap();
53 let owner = User::from_str(path_split.next().unwrap()).unwrap();
54 let name = path_split.next().unwrap().to_string();
55
56 Ok(Self {
57 instance,
58 owner,
59 name,
60 })
61 }
62 }
63
64 /// Visibility of the repository to the general eye
65 #[derive(PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)]
66 #[sqlx(type_name = "visibility", rename_all = "lowercase")]
67 pub enum RepositoryVisibility {
68 Public,
69 Unlisted,
70 Private,
71 }
72
73 /// Implements [`Display`] for [`RepositoryVisiblity`] using [`Debug`]
74 impl Display for RepositoryVisibility {
75 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76 write!(f, "{:?}", self)
77 }
78 }
79
80 #[derive(Clone, Debug, Serialize, Deserialize)]
81 pub struct RepositoryView {
82 /// Name of the repository
83 ///
84 /// This is different than the [`Repository`] name,
85 /// which may be a path.
86 pub name: String,
87 /// Owner of the Repository
88 pub owner: User,
89 /// Repository description
90 pub description: Option<String>,
91 /// Repository visibility
92 pub visibility: RepositoryVisibility,
93 /// Default branch of the repository
94 pub default_branch: String,
95 /// Last commit made to the repository
96 pub latest_commit: Option<Commit>,
97 /// Revision of the displayed tree
98 pub tree_rev: Option<String>,
99 /// Repository tree
100 pub tree: Vec<RepositoryTreeEntry>,
101 }
102
103 #[derive(Debug, Clone, Serialize, Deserialize)]
104 pub enum RepositoryObjectType {
105 Tree,
106 Blob,
107 }
108
109 /// Stored info for our tree entries
110 #[derive(Debug, Clone, Serialize, Deserialize)]
111 pub struct RepositoryTreeEntry {
112 /// Name of the tree/blob
113 pub name: String,
114 /// Type of the tree entry
115 pub object_type: RepositoryObjectType,
116 /// Git supplies us with the mode at all times, and people like it displayed.
117 pub mode: i32,
118 /// File size
119 pub size: Option<usize>,
120 /// Last commit made to the tree/blob
121 pub last_commit: Option<Commit>,
122 }
123
124 impl RepositoryTreeEntry {
125 // I love you Emilia <3
126 pub fn new(name: &str, object_type: RepositoryObjectType, mode: i32) -> Self {
127 Self {
128 name: name.to_string(),
129 object_type,
130 mode,
131 size: None,
132 last_commit: None,
133 }
134 }
135 }
136
137 #[derive(Debug, Clone, Serialize, Deserialize)]
138 pub struct RepositoryTreeEntryWithCommit {
139 pub tree_entry: RepositoryTreeEntry,
140 pub commit: Commit,
141 }
142
143 /// Info about a git commit
144 #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
145 pub struct Commit {
146 /// Unique commit ID
147 pub oid: String,
148 /// Full commit message
149 pub message: Option<String>,
150 /// Who created the commit
151 pub author: CommitSignature,
152 /// Who committed the commit
153 pub committer: CommitSignature,
154 /// Time when the commit happened
155 pub time: chrono::NaiveDateTime,
156 }
157
158 /// Gets all info from [`git2::Commit`] for easy use
159 impl From<git2::Commit<'_>> for Commit {
160 fn from(commit: git2::Commit<'_>) -> Self {
161 Self {
162 oid: commit.id().to_string(),
163 message: commit.message().map(|message| message.to_string()),
164 author: commit.author().into(),
165 committer: commit.committer().into(),
166 time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),
167 }
168 }
169 }
170
171 /// Git commit signature
172 #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
173 pub struct CommitSignature {
174 pub name: Option<String>,
175 pub email: Option<String>,
176 pub time: chrono::NaiveDateTime,
177 }
178
179 /// Converts the signature from git2 into something usable without explicit lifetimes.
180 impl From<git2::Signature<'_>> for CommitSignature {
181 fn from(signature: git2::Signature<'_>) -> Self {
182 Self {
183 name: signature.name().map(|name| name.to_string()),
184 email: signature.email().map(|email| email.to_string()),
185 time: chrono::NaiveDateTime::from_timestamp_opt(signature.when().seconds(), 0).unwrap(),
186 }
187 }
188 }
189
190 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
191 pub struct RepositorySummary {
192 pub repository: Repository,
193 pub owner: User,
194 pub visibility: RepositoryVisibility,
195 pub description: Option<String>,
196 pub last_commit: Option<Commit>,
197 }
198