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 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
#[derive(Hash, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
32 |
pub struct Repository {
|
33 |
pub owner: User,
|
34 |
pub name: String,
|
35 |
|
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 |
|
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 |
|
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 |
|
83 |
|
84 |
|
85 |
|
86 |
pub name: String,
|
87 |
|
88 |
pub owner: User,
|
89 |
|
90 |
pub description: Option<String>,
|
91 |
|
92 |
pub visibility: RepositoryVisibility,
|
93 |
|
94 |
pub default_branch: String,
|
95 |
|
96 |
pub latest_commit: Option<Commit>,
|
97 |
|
98 |
pub tree_rev: Option<String>,
|
99 |
|
100 |
pub tree: Vec<RepositoryTreeEntry>,
|
101 |
}
|
102 |
|
103 |
#[derive(Debug, Clone, Serialize, Deserialize)]
|
104 |
pub enum RepositoryObjectType {
|
105 |
Tree,
|
106 |
Blob,
|
107 |
}
|
108 |
|
109 |
|
110 |
#[derive(Debug, Clone, Serialize, Deserialize)]
|
111 |
pub struct RepositoryTreeEntry {
|
112 |
|
113 |
pub name: String,
|
114 |
|
115 |
pub object_type: RepositoryObjectType,
|
116 |
|
117 |
pub mode: i32,
|
118 |
|
119 |
pub size: Option<usize>,
|
120 |
|
121 |
pub last_commit: Option<Commit>,
|
122 |
}
|
123 |
|
124 |
impl RepositoryTreeEntry {
|
125 |
|
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 |
|
144 |
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
|
145 |
pub struct Commit {
|
146 |
|
147 |
pub oid: String,
|
148 |
|
149 |
pub message: Option<String>,
|
150 |
|
151 |
pub author: CommitSignature,
|
152 |
|
153 |
pub committer: CommitSignature,
|
154 |
|
155 |
pub time: chrono::NaiveDateTime,
|
156 |
}
|
157 |
|
158 |
|
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 |
|
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 |
|
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 |
|