    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    use std::borrow::Cow;

    use super::{Bearer, Error};

    /// Builder for the [`Bearer`] challenge.
    ///
    /// It is up to implementor to fill all required fields,
    /// neither this `Builder` or [`Bearer`] does not provide any validation.
    ///
    /// [`Bearer`]: struct.Bearer.html
    #[derive(Debug, Default)]
    pub struct BearerBuilder(Bearer);

    impl BearerBuilder {
        /// Provides the `scope` attribute, as defined in [RFC6749, Section 3.3](https://tools.ietf.org/html/rfc6749#section-3.3)
        pub fn scope<T>(mut self, value: T) -> Self
        where
            T: Into<Cow<'static, str>>,
        {
            self.0.scope = Some(value.into());
            self
        }

        /// Provides the `realm` attribute, as defined in [RFC2617](https://tools.ietf.org/html/rfc2617)
        pub fn realm<T>(mut self, value: T) -> Self
        where
            T: Into<Cow<'static, str>>,
        {
            self.0.realm = Some(value.into());
            self
        }

        /// Provides the `error` attribute, as defined in [RFC6750, Section 3.1](https://tools.ietf.org/html/rfc6750#section-3.1)
        pub fn error(mut self, value: Error) -> Self {
            self.0.error = Some(value);
            self
        }

        /// Provides the `error_description` attribute, as defined in [RFC6750, Section 3](https://tools.ietf.org/html/rfc6750#section-3)
        pub fn error_description<T>(mut self, value: T) -> Self
        where
            T: Into<Cow<'static, str>>,
        {
            self.0.error_description = Some(value.into());
            self
        }

        /// Provides the `error_uri` attribute, as defined in [RFC6750, Section 3](https://tools.ietf.org/html/rfc6750#section-3)
        ///
        /// It is up to implementor to provide properly-formed absolute URI.
        pub fn error_uri<T>(mut self, value: T) -> Self
        where
            T: Into<Cow<'static, str>>,
        {
            self.0.error_uri = Some(value.into());
            self
        }

        /// Consumes the builder and returns built `Bearer` instance.
        pub fn finish(self) -> Bearer {
            self.0
        }
    }