Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Generate path with Answer #17

Open
sergeysova opened this issue Aug 5, 2020 · 1 comment
Open

Generate path with Answer #17

sergeysova opened this issue Aug 5, 2020 · 1 comment

Comments

@sergeysova
Copy link
Member

    pub mod session_get {
        use super::responses;
        use actix_swagger::ContentType;
        use actix_web::http::StatusCode;
        use serde::Serialize;

        pub type Answer = actix_swagger::Answer<'static, Response>;

        #[derive(Debug, Serialize)]
        #[serde(untagged)]
        pub enum Response {
            Ok(responses::SessionGetSuccess),
            Unauthorized,
            Unexpected,
        }

        impl Response {
            #[inline]
            pub fn into_answer<'a>(self) -> Answer {
                let status = match self {
                    Self::Ok(_) => StatusCode::OK,
                    Self::Unauthorized => StatusCode::UNAUTHORIZED,
                    Self::Unexpected => StatusCode::INTERNAL_SERVER_ERROR,
                };

                let content_type = match self {
                    Self::Ok(_) => Some(ContentType::Json),
                    Self::Unauthorized => None,
                    Self::Unexpected => None,
                };

                Answer::new(self).status(status).content_type(content_type)
            }
        }
    }
@sergeysova
Copy link
Member Author

maybe remove 'static from actix_swagger::Answer ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant