Skip to main content

grp_core/common/users/
users.rs

1use serde_json::Value;
2
3use crate::common::users::structs::User;
4use crate::error::structs::Error;
5use crate::platform::Platform;
6use crate::json::JSON;
7
8
9impl User {
10    /// # Return
11    /// Generates an instance of a user if the information of 
12    /// the text is a valid json and the platform matches that content.
13    /// 
14    /// # Error
15    /// a `grp_core::Error` of type `grp_core::ErrorType::ResponseParsing`.
16    pub fn from_text(text: &String, platform: &Platform) -> Result<Self, Error> {
17        let json: Value = JSON::from_str(text)?;
18        
19        let name: String;
20        let id: String;
21        let path: Option<String>;
22        
23        match platform {
24            Platform::Github => {
25                name = json["login"].as_str().unwrap().to_string();
26                id = name.clone();
27                path = None
28            },
29            Platform::Codeberg |
30            Platform::Forgejo |
31            Platform::Gitea => {
32                name = json["name"].as_str().unwrap().to_string();
33                id = name.clone();
34                path = None
35            },
36            Platform::Gitlab => {
37                id = json["id"].as_u64().unwrap().to_string();
38                name = json["name"].as_str().unwrap().to_string();
39                path = Some(json["full_path"].as_str().unwrap().to_string());
40            },
41        };
42        Ok(User { id: id, name: name, path: path })
43    }
44    
45    /// # Return
46    /// 
47    /// Generates a list of User if the information of the text is a valid list of json 
48    /// and the platform matches that content.
49    /// 
50    /// # Error
51    /// a `grp_core::Error` of type `grp_core::ErrorType::ResponseParsing`.
52    pub fn from_text_array(text: &String, platform: &Platform) -> Result<Vec<Self>, Error> {
53        let json: Vec<Value> = JSON::from_str(&text)?;
54        
55        let orgs: Vec<User> = match platform {
56            Platform::Github => json.iter()
57                .map(|org| {
58                    let name = org["login"].as_str().unwrap().to_string();
59                    User { id: name.clone(), name: name.clone(), path: None }
60                }).collect(),
61            Platform::Codeberg |
62            Platform::Forgejo |
63            Platform::Gitea => json.iter()
64                .map(|org| {
65                    let name = org["name"].as_str().unwrap().to_string();
66                    User { id: name.clone(), name: name.clone(), path: None }
67                }).collect(),
68            Platform::Gitlab => json.iter()
69                .map(|org| {
70                    let id = org["id"].as_u64().unwrap();
71                    let name = org["name"].as_str().unwrap().to_string();
72                    let path = org["full_path"].as_str().unwrap().to_string();
73                    User { id: id.to_string(), name: name.clone(), path: Some(path) }
74                }).collect(),
75        };
76        
77        Ok(orgs)
78    }
79}
80