gitlab 0.1903.0

Gitlab API client.
Documentation
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use derive_builder::Builder;

use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;

/// Query for a specific namespace on an instance.
///
/// Unlike `groups::Group`, this resolves both group namespaces and user
/// personal namespaces.
#[derive(Debug, Builder, Clone)]
pub struct Namespace<'a> {
    /// The namespace to get.
    #[builder(setter(into))]
    namespace: NameOrId<'a>,
}

impl<'a> Namespace<'a> {
    /// Create a builder for the endpoint.
    pub fn builder() -> NamespaceBuilder<'a> {
        NamespaceBuilder::default()
    }
}

impl Endpoint for Namespace<'_> {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("namespaces/{}", self.namespace).into()
    }
}

#[cfg(test)]
mod tests {
    use crate::api::namespaces::{Namespace, NamespaceBuilderError};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    #[test]
    fn namespace_is_necessary() {
        let err = Namespace::builder().build().unwrap_err();
        crate::test::assert_missing_field!(err, NamespaceBuilderError, "namespace");
    }

    #[test]
    fn namespace_is_sufficient() {
        Namespace::builder().namespace(1).build().unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("namespaces/group%2Fsubgroup")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Namespace::builder()
            .namespace("group/subgroup")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_numeric_id() {
        let endpoint = ExpectedUrl::builder()
            .endpoint("namespaces/1")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = Namespace::builder().namespace(1).build().unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}