grp_core/common/
platform.rs1use hyper::HeaderMap;
2use reqwest::Response;
3
4use crate::error::types::ErrorType;
5use crate::error::structs::Error;
6use crate::common::structs::Context;
7use crate::config::Config;
8use crate::platform::Platform;
9use crate::specific::{gitea, github, gitlab};
10
11impl Platform {
12 pub fn matches(name: &str) -> Result<Platform, Error> {
18 let platform: Platform = match name {
19 "github" => Platform::Github,
20 "gitea" => Platform::Gitea,
21 "gitlab" => Platform::Gitlab,
22 "codeberg" => Platform::Codeberg,
23 "forgejo" => Platform::Forgejo,
24 name => {
25 return Err(
26 Error::new(
27 ErrorType::Unsupported,
28 vec![name, "Platform variant"]
29 )
30 )
31 }
32 };
33
34 Ok(platform)
35 }
36
37 pub(crate) fn get_auth_header<S: AsRef<str>>(&self, token: &S) -> HeaderMap {
38 let token = token.as_ref().to_string();
39 match self {
40 Platform::Github => { github::header::get_auth_header(token) }
41 Platform::Codeberg |
42 Platform::Forgejo |
43 Platform::Gitea => { gitea::header::get_auth_header(token) }
44 Platform::Gitlab => { gitlab::header::get_auth_header(token) }
45 }
46 }
47
48 pub(crate) fn get_base_url<S: AsRef<str>>(&self, endpoint: &S) -> String {
49 let endpoint = endpoint.as_ref();
50 match self {
51 Platform::Github => { format!("https://{}", endpoint) }
52 Platform::Codeberg |
53 Platform::Forgejo |
54 Platform::Gitea => { format!("https://{}/api/v1", endpoint) }
55 Platform::Gitlab => { format!("https://{}/api/v4", endpoint) }
56 }
57 }
58
59 pub async fn unwrap<T: Into<String>>(&self,
68 result: Response,
69 base_message: T,
70 config: &Config,
71 context: Context,
72 ) -> Result<String, Error> {
73 match self {
74 Platform::Github => { github::unwrap::unwrap(result, base_message.into(), config, context).await }
75 Platform::Codeberg |
76 Platform::Forgejo |
77 Platform::Gitea => { gitea::unwrap::unwrap(result, base_message.into(), config, context).await }
78 Platform::Gitlab => { gitlab::unwrap::unwrap(result, base_message.into(), config, context).await }
79 }
80 }
81}