scaleway-rs 0.2.7

A pure Rust scaleway API binding.
Documentation
use crate::ScalewayApi;
use crate::ScalewayError;
use crate::data::project::ScalewayProject;
use serde::Serialize;

#[derive(Serialize, Debug)]
struct CreateProjectConfig {
    name: String,
    organization_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
}

/// Builder for creating a new Scaleway project.
///
/// Created by [`ScalewayApi::create_project`](crate::ScalewayApi::create_project).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayCreateProjectBuilder {
    api: ScalewayApi,
    config: CreateProjectConfig,
}

impl ScalewayCreateProjectBuilder {
    pub fn new(api: ScalewayApi, name: &str, organization_id: &str) -> Self {
        ScalewayCreateProjectBuilder {
            api,
            config: CreateProjectConfig {
                name: name.to_string(),
                organization_id: organization_id.to_string(),
                description: None,
            },
        }
    }

    /// Sets an optional human-readable description for the project.
    pub fn description(mut self, description: &str) -> ScalewayCreateProjectBuilder {
        self.config.description = Some(description.to_string());
        self
    }

    /// Creates the project and returns it (blocking).
    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<ScalewayProject, ScalewayError> {
        Ok(self
            .api
            .post("https://api.scaleway.com/account/v3/projects", self.config)?
            .json::<ScalewayProject>()?)
    }

    /// Creates the project and returns it.
    pub async fn run_async(self) -> Result<ScalewayProject, ScalewayError> {
        Ok(self
            .api
            .post_async("https://api.scaleway.com/account/v3/projects", self.config)
            .await?
            .json::<ScalewayProject>()
            .await?)
    }
}