gitlab 0.1900.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 std::borrow::Cow;

use derive_builder::Builder;

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

/// Get a custom attribute for a group.
#[derive(Debug, Clone, Builder)]
pub struct CustomAttribute<'a> {
    /// The ID of the group.
    #[builder(setter(into))]
    group: NameOrId<'a>,
    /// The key of the custom attribute.
    #[builder(setter(into))]
    key: Cow<'a, str>,
}

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

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

    fn endpoint(&self) -> Cow<'static, str> {
        format!("groups/{}/custom_attributes/{}", self.group, self.key).into()
    }
}

#[cfg(test)]
mod tests {
    use crate::api::groups::custom_attributes::{CustomAttribute, CustomAttributeBuilderError};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};
    use http::Method;

    #[test]
    fn group_is_needed() {
        let err = CustomAttribute::builder().key("test").build().unwrap_err();
        crate::test::assert_missing_field!(err, CustomAttributeBuilderError, "group");
    }

    #[test]
    fn key_is_needed() {
        let err = CustomAttribute::builder().group(1).build().unwrap_err();
        crate::test::assert_missing_field!(err, CustomAttributeBuilderError, "key");
    }

    #[test]
    fn group_key_are_sufficient() {
        CustomAttribute::builder()
            .group(1)
            .key("test")
            .build()
            .unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::GET)
            .endpoint("groups/group/custom_attributes/test")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = CustomAttribute::builder()
            .group("group")
            .key("test")
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}