1 |
use std::fmt::{Display, Formatter};
|
2 |
use std::str::FromStr;
|
3 |
|
4 |
use serde::{Deserialize, Serialize};
|
5 |
|
6 |
use crate::object::GiteratedObject;
|
7 |
|
8 |
use super::{instance::Instance, user::User};
|
9 |
|
10 |
mod operations;
|
11 |
mod settings;
|
12 |
mod values;
|
13 |
|
14 |
pub use operations::*;
|
15 |
pub use settings::*;
|
16 |
pub use values::*;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
#[derive(Hash, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
45 |
pub struct Repository {
|
46 |
pub owner: User,
|
47 |
pub name: String,
|
48 |
|
49 |
pub instance: Instance,
|
50 |
}
|
51 |
|
52 |
impl Display for Repository {
|
53 |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
54 |
f.write_str(&format!("{}/{}@{}", self.owner, self.name, self.instance))
|
55 |
}
|
56 |
}
|
57 |
|
58 |
impl GiteratedObject for Repository {
|
59 |
fn object_name() -> &'static str {
|
60 |
"repository"
|
61 |
}
|
62 |
|
63 |
fn from_object_str(object_str: &str) -> Result<Self, anyhow::Error> {
|
64 |
Ok(Repository::from_str(object_str)?)
|
65 |
}
|
66 |
}
|
67 |
|
68 |
impl TryFrom<String> for Repository {
|
69 |
type Error = RepositoryParseError;
|
70 |
|
71 |
fn try_from(value: String) -> Result<Self, Self::Error> {
|
72 |
Self::from_str(&value)
|
73 |
}
|
74 |
}
|
75 |
|
76 |
impl FromStr for Repository {
|
77 |
type Err = RepositoryParseError;
|
78 |
|
79 |
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
80 |
let mut by_ampersand = s.split('@');
|
81 |
let mut path_split = by_ampersand.next().ok_or(RepositoryParseError)?.split('/');
|
82 |
|
83 |
let instance = Instance::from_str(by_ampersand.next().ok_or(RepositoryParseError)?)
|
84 |
.map_err(|_| RepositoryParseError)?;
|
85 |
let owner = User::from_str(path_split.next().ok_or(RepositoryParseError)?)
|
86 |
.map_err(|_| RepositoryParseError)?;
|
87 |
let name = path_split.next().ok_or(RepositoryParseError)?.to_string();
|
88 |
|
89 |
Ok(Self {
|
90 |
instance,
|
91 |
owner,
|
92 |
name,
|
93 |
})
|
94 |
}
|
95 |
}
|
96 |
|
97 |
#[derive(Debug, thiserror::Error)]
|
98 |
#[error("no parse!")]
|
99 |
pub struct RepositoryParseError;
|
100 |
|
101 |
|
102 |
#[derive(PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)]
|
103 |
#[sqlx(type_name = "visibility", rename_all = "lowercase")]
|
104 |
pub enum RepositoryVisibility {
|
105 |
Public,
|
106 |
Unlisted,
|
107 |
Private,
|
108 |
}
|
109 |
|
110 |
|
111 |
impl Display for RepositoryVisibility {
|
112 |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
113 |
write!(f, "{:?}", self)
|
114 |
}
|
115 |
}
|
116 |
|
117 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
118 |
pub struct RepositoryView {
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
pub name: String,
|
124 |
|
125 |
pub owner: User,
|
126 |
|
127 |
pub description: Option<Description>,
|
128 |
|
129 |
pub visibility: Visibility,
|
130 |
|
131 |
pub default_branch: DefaultBranch,
|
132 |
|
133 |
pub latest_commit: Option<LatestCommit>,
|
134 |
|
135 |
pub tree_rev: Option<String>,
|
136 |
|
137 |
pub tree: Vec<RepositoryTreeEntry>,
|
138 |
}
|
139 |
|
140 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
141 |
pub struct RepositoryFile {
|
142 |
|
143 |
pub content: Vec<u8>,
|
144 |
|
145 |
pub binary: bool,
|
146 |
|
147 |
pub size: usize,
|
148 |
}
|
149 |
|
150 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
151 |
pub struct RepositoryDiff {
|
152 |
|
153 |
pub files_changed: usize,
|
154 |
|
155 |
pub insertions: usize,
|
156 |
|
157 |
pub deletions: usize,
|
158 |
|
159 |
pub patch: String,
|
160 |
}
|
161 |
|
162 |
#[derive(Debug, Clone, Serialize, Deserialize)]
|
163 |
pub enum RepositoryObjectType {
|
164 |
Tree,
|
165 |
Blob,
|
166 |
}
|
167 |
|
168 |
|
169 |
#[derive(Debug, Clone, Serialize, Deserialize)]
|
170 |
pub struct RepositoryTreeEntry {
|
171 |
|
172 |
pub id: String,
|
173 |
|
174 |
pub name: String,
|
175 |
|
176 |
pub object_type: RepositoryObjectType,
|
177 |
|
178 |
pub mode: i32,
|
179 |
|
180 |
pub size: Option<usize>,
|
181 |
|
182 |
pub last_commit: Option<Commit>,
|
183 |
}
|
184 |
|
185 |
impl RepositoryTreeEntry {
|
186 |
|
187 |
pub fn new(id: &str, name: &str, object_type: RepositoryObjectType, mode: i32) -> Self {
|
188 |
Self {
|
189 |
id: id.to_string(),
|
190 |
name: name.to_string(),
|
191 |
object_type,
|
192 |
mode,
|
193 |
size: None,
|
194 |
last_commit: None,
|
195 |
}
|
196 |
}
|
197 |
}
|
198 |
|
199 |
#[derive(Debug, Clone, Serialize, Deserialize)]
|
200 |
pub struct RepositoryTreeEntryWithCommit {
|
201 |
pub tree_entry: RepositoryTreeEntry,
|
202 |
pub commit: Commit,
|
203 |
}
|
204 |
|
205 |
|
206 |
#[derive(PartialEq, Hash, Eq, Debug, Clone, Serialize, Deserialize)]
|
207 |
pub struct Commit {
|
208 |
|
209 |
pub oid: String,
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
pub short_oid: String,
|
215 |
|
216 |
pub message: Option<String>,
|
217 |
|
218 |
pub author: CommitSignature,
|
219 |
|
220 |
pub committer: CommitSignature,
|
221 |
|
222 |
pub time: chrono::NaiveDateTime,
|
223 |
}
|
224 |
|
225 |
|
226 |
impl From<git2::Commit<'_>> for Commit {
|
227 |
fn from(commit: git2::Commit<'_>) -> Self {
|
228 |
Self {
|
229 |
oid: commit.id().to_string(),
|
230 |
|
231 |
short_oid: commit
|
232 |
.as_object()
|
233 |
.short_id()
|
234 |
.unwrap()
|
235 |
.as_str()
|
236 |
.unwrap()
|
237 |
.to_string(),
|
238 |
message: commit.message().map(|message| message.to_string()),
|
239 |
author: commit.author().into(),
|
240 |
committer: commit.committer().into(),
|
241 |
time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),
|
242 |
}
|
243 |
}
|
244 |
}
|
245 |
|
246 |
|
247 |
#[derive(PartialEq, Hash, Eq, Debug, Clone, Serialize, Deserialize)]
|
248 |
pub struct CommitSignature {
|
249 |
pub name: Option<String>,
|
250 |
pub email: Option<String>,
|
251 |
pub time: chrono::NaiveDateTime,
|
252 |
}
|
253 |
|
254 |
|
255 |
impl From<git2::Signature<'_>> for CommitSignature {
|
256 |
fn from(signature: git2::Signature<'_>) -> Self {
|
257 |
Self {
|
258 |
name: signature.name().map(|name| name.to_string()),
|
259 |
email: signature.email().map(|email| email.to_string()),
|
260 |
time: chrono::NaiveDateTime::from_timestamp_opt(signature.when().seconds(), 0).unwrap(),
|
261 |
}
|
262 |
}
|
263 |
}
|
264 |
|
265 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
266 |
pub struct RepositorySummary {
|
267 |
pub repository: Repository,
|
268 |
pub owner: User,
|
269 |
pub visibility: RepositoryVisibility,
|
270 |
pub description: Option<String>,
|
271 |
pub last_commit: Option<Commit>,
|
272 |
}
|
273 |
|
274 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
275 |
pub struct IssueLabel {
|
276 |
pub name: String,
|
277 |
pub color: String,
|
278 |
}
|
279 |
|
280 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
281 |
pub struct RepositoryIssue {
|
282 |
pub author: User,
|
283 |
pub id: u64,
|
284 |
pub title: String,
|
285 |
pub contents: String,
|
286 |
pub labels: Vec<IssueLabel>,
|
287 |
}
|
288 |
|