grp_core/common/users/
users.rs1use 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 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 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