zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
//! Zulip API response types.

mod base;
mod error_code;
mod result;

pub use base::BaseResponse;
pub use error_code::ErrorCode;
pub use result::ApiResult;

/// Represents the common functions for all response types.
pub trait Response {
    /// Gets the [Response] `msg` field.
    fn msg(&self) -> &str;

    /// Sets the [Response] `msg` field.
    fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C);

    /// Builder function that sets the [Response] `msg` field.
    fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self;

    /// Gets the [Response] `result` field.
    fn result(&self) -> ApiResult;

    /// Sets the [Response] `result` field.
    fn set_result(&mut self, result: ApiResult);

    /// Builder function that sets the [Response] `result` field.
    fn with_result(self, result: ApiResult) -> Self;

    /// Gets the [Response] `code` field.
    fn code(&self) -> Option<ErrorCode>;

    /// Sets the [Response] `code` field.
    fn set_code(&mut self, code: ErrorCode);

    /// Unsets the [Response] `code` field.
    fn unset_code(&mut self) -> Option<ErrorCode>;

    /// Builder function that sets the [Response] `code` field.
    fn with_code(self, code: ErrorCode) -> Self;

    /// Gets the [Response] ignored unsupported parameters list.
    ///
    /// An array of any parameters sent in the request that are not
    /// supported by the endpoint.
    ///
    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
    /// for details on this and its change history.
    fn ignored_parameters_unsupported(&self) -> Option<&[String]>;

    /// Sets the [Response] ignored unsupported parameters list.
    ///
    /// An array of any parameters sent in the request that are not
    /// supported by the endpoint.
    ///
    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
    /// for details on this and its change history.
    fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
        &mut self,
        val: C,
    );

    /// Unsets the [Response] ignored unsupported parameters list.
    ///
    /// An array of any parameters sent in the request that are not
    /// supported by the endpoint.
    ///
    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
    /// for details on this and its change history.
    fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>>;

    /// Builder function that sets the [Response] ignored unsupported parameters list.
    ///
    /// An array of any parameters sent in the request that are not
    /// supported by the endpoint.
    ///
    /// See [error handling](https://zulip.com//api/rest-error-handling#ignored-parameters) documentation
    /// for details on this and its change history.
    fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
        self,
        val: C,
    ) -> Self;
}

/// Implements the [Response] trait for a given type.
#[macro_export]
macro_rules! impl_response {
    ($ty:ident) => {
        impl $crate::response::Response for $ty {
            fn msg(&self) -> &str {
                &self.msg
            }

            fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C) {
                self.msg = C::into(msg).into();
            }

            fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self {
                Self {
                    msg: C::into(msg).into(),
                    ..self
                }
            }

            fn result(&self) -> $crate::response::ApiResult {
                self.result
            }

            fn set_result(&mut self, result: $crate::response::ApiResult) {
                self.result = result;
            }

            fn with_result(self, result: $crate::response::ApiResult) -> Self {
                Self { result, ..self }
            }

            fn code(&self) -> Option<$crate::response::ErrorCode> {
                self.code
            }

            fn set_code(&mut self, code: $crate::response::ErrorCode) {
                self.code = Some(code);
            }

            fn unset_code(&mut self) -> Option<$crate::response::ErrorCode> {
                self.code.take()
            }

            fn with_code(self, code: $crate::response::ErrorCode) -> Self {
                Self {
                    code: Some(code),
                    ..self
                }
            }

            fn ignored_parameters_unsupported(&self) -> Option<&[String]> {
                self.ignored_parameters_unsupported.as_deref()
            }

            fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
                &mut self,
                val: C,
            ) {
                self.ignored_parameters_unsupported = Some(C::into(val).into());
            }

            fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>> {
                self.ignored_parameters_unsupported.take()
            }

            fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
                self,
                val: C,
            ) -> Self {
                Self {
                    ignored_parameters_unsupported: Some(C::into(val).into()),
                    ..self
                }
            }
        }
    };

    ($ty:ident, $field:ident) => {
        impl $crate::response::Response for $ty {
            fn msg(&self) -> &str {
                &self.$field.msg()
            }

            fn set_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(&mut self, msg: C) {
                self.$field.set_msg(msg);
            }

            fn with_msg<'a, C: Into<std::borrow::Cow<'a, str>>>(self, msg: C) -> Self {
                Self {
                    $field: self.$field.with_msg(msg),
                    ..self
                }
            }

            fn result(&self) -> $crate::response::ApiResult {
                self.$field.result()
            }

            fn set_result(&mut self, result: $crate::response::ApiResult) {
                self.$field.set_result(result);
            }

            fn with_result(self, result: $crate::response::ApiResult) -> Self {
                Self {
                    $field: self.$field.with_result(result),
                    ..self
                }
            }

            fn code(&self) -> Option<$crate::response::ErrorCode> {
                self.$field.code()
            }

            fn set_code(&mut self, code: $crate::response::ErrorCode) {
                self.$field.set_code(code);
            }

            fn unset_code(&mut self) -> Option<$crate::response::ErrorCode> {
                self.$field.unset_code()
            }

            fn with_code(self, code: $crate::response::ErrorCode) -> Self {
                Self {
                    $field: self.$field.with_code(code),
                    ..self
                }
            }

            fn ignored_parameters_unsupported(&self) -> Option<&[String]> {
                self.$field.ignored_parameters_unsupported()
            }

            fn set_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
                &mut self,
                val: C,
            ) {
                self.$field.set_ignored_parameters_unsupported(val);
            }

            fn unset_ignored_parameters_unsupported(&mut self) -> Option<Vec<String>> {
                self.$field.unset_ignored_parameters_unsupported()
            }

            fn with_ignored_parameters_unsupported<'a, C: Into<std::borrow::Cow<'a, [String]>>>(
                self,
                val: C,
            ) -> Self {
                Self {
                    $field: self.$field.with_ignored_parameters_unsupported(val),
                    ..self
                }
            }
        }
    };
}