openapi / actix-swagger

Swagger code generator for actix-web framework
https://crates.io/crates/cargo-swagg
MIT License
111 stars 8 forks source link

Convert generated response to `actix_swagger::Answer` through `Into` #18

Open sergeysova opened 4 years ago

sergeysova commented 4 years ago
    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 Into<Answer> for Response {
            #[inline]
            fn into(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)
            }
        }

rel #17