1use crate::error::structs::Error;
2use crate::json::JSON;
3use crate::platform::Platform;
4use crate::common::structs::Repo;
5use crate::specific::{gitea, github, gitlab};
6
7
8impl Repo {
9 pub(crate) fn as_json(&self, platform: &Platform) -> serde_json::Value {
14 match platform {
15 Platform::Github |
16 Platform::Codeberg |
17 Platform::Forgejo |
18 Platform::Gitea => {
19 serde_json::json!({
20 "name": self.name,
21 "description": self.description,
22 "private": self.private.unwrap_or(false),
23 })
24 }
25 Platform::Gitlab => {
26 serde_json::json!({
27 "path": self.name,
28 "description": self.description,
29 "visibility": if self.private.unwrap_or(false) { "private" } else { "public" },
30 })
31 }
32 }
33 }
34
35 pub fn from_text(text: &String, platform: &Platform) -> Result<Self, Error> {
42 let repo = match platform {
43 Platform::Github => {
44 let repo: github::parser::Repository = JSON::from_str(text)?;
45
46 Repo {
47 name: repo.name.clone(),
48 path: repo.full_name.clone(),
49 private: Some(repo.private),
50 url: repo.html_url.clone(),
51 git: repo.clone_url.clone(),
52 description: repo.description.clone(),
53 }
54 },
55 Platform::Codeberg |
56 Platform::Forgejo |
57 Platform::Gitea => {
58 let repo: gitea::parser::Repository = JSON::from_str(text)?;
59
60 Repo {
61 name: repo.name.clone(),
62 path: repo.full_name.clone(),
63 private: Some(repo.private),
64 url: repo.html_url.clone(),
65 git: repo.clone_url.clone(),
66 description: repo.description.clone(),
67 }
68 },
69 Platform::Gitlab => {
70 let repo: gitlab::parser::Repository = JSON::from_str(text)?;
71
72 Repo {
73 name: repo.path.clone(),
74 path: repo.path_with_namespace.clone(),
75 private: Some(repo.visibility == "private"),
76 url: repo.web_url.clone(),
77 git: repo.http_url_to_repo.clone(),
78 description: repo.description.clone(),
79 }
80 },
81 };
82
83 Ok(repo)
84 }
85
86 pub fn from_text_array(text: &String, platform: &Platform) -> Result<Vec<Self>, Error> {
94 let repos = match platform {
95 Platform::Github => {
96 let tmp: Vec<github::parser::Repository> = JSON::from_str(text)?;
97
98 let repos = tmp.iter().map(|repo| {
99 Repo {
100 name: repo.name.clone(),
101 path: repo.full_name.clone(),
102 private: Some(repo.private),
103 url: repo.html_url.clone(),
104 git: repo.clone_url.clone(),
105 description: repo.description.clone(),
106 }
107 }).collect();
108
109 repos
110 },
111 Platform::Codeberg |
112 Platform::Forgejo |
113 Platform::Gitea => {
114 let tmp: Vec<gitea::parser::Repository> = JSON::from_str(text)?;
115
116 let repos = tmp.iter().map(|repo| {
117 Repo {
118 name: repo.name.clone(),
119 path: repo.full_name.clone(),
120 private: Some(repo.private),
121 url: repo.html_url.clone(),
122 git: repo.clone_url.clone(),
123 description: repo.description.clone(),
124 }
125 }).collect();
126
127 repos
128 },
129 Platform::Gitlab => {
130 let tmp: Vec<gitlab::parser::Repository> = JSON::from_str(text)?;
131
132 let repos = tmp.iter().map(|repo| {
133 Repo {
134 name: repo.path.clone(),
135 path: repo.path_with_namespace.clone(),
136 private: Some(repo.visibility == "private"),
137 url: repo.web_url.clone(),
138 git: repo.http_url_to_repo.clone(),
139 description: repo.description.clone(),
140 }
141 }).collect();
142
143 repos
144 }
145 };
146
147 Ok(repos)
148 }
149}