Skip to main content

grp_core/common/repos/
create.rs

1use crate::animation::Animation;
2use crate::common::structs::{Context, Repo, RequestType};
3use crate::config::Config;
4use crate::error::structs::Error;
5use crate::platform::Platform;
6
7
8impl Platform {
9    /// Creates a repository for the given owner in the selected platform.
10    /// 
11    /// - `owner`: the name or path of the **user** or **org** that _owns_ the repo.
12    /// - `repo`: a `grp_core::Repo` with the metadata for the new repo.
13    /// - `config`: a `grp_core::Config`
14    /// - `animation`: a struct wich implements the trait `grp_core::animation::Animation`
15    /// 
16    /// # Retuns
17    /// a `grp_core::Repo` with the confirmation from the platform.
18    /// 
19    /// #Error
20    /// a `grp_core::Error` containing the detail of the error. 
21    pub async fn create_repo<T: Into<String>, A: Animation + ?Sized>(&self,
22        owner: Option<T>, 
23        repo: Repo,
24        config: &Config,
25        animation: &Box<A>
26    ) -> Result<Repo, Error> {
27        let owner = owner.map(|o| o.into());
28        let owner = owner.unwrap_or(config.user.clone());
29        
30        animation.change_message("getting user type");
31        let user_type = self.get_user_type(&owner, &config).await?;
32        
33        animation.change_message("Serializing repository...");
34        let json = repo.as_json(&self);
35        
36        animation.change_message("creating repository...");
37        let url = self.url_create_repo(&user_type, &config.endpoint).await;
38        let result = self.post(url, true, config, &json).await?;
39        
40        let context = Context {
41            request_type: RequestType::Create,
42            owner: Some(user_type.get_user().name),
43            repo: Some(repo.name), additional: None,
44        };
45        
46        let base_message = "Failed to create repository";
47        let reponse = self.unwrap(result, base_message, &config, context).await?;
48        
49        Repo::from_text(&reponse, &self)
50    }
51}