From af28e0d9165bce227722ad56cd364738af341c7c Mon Sep 17 00:00:00 2001 From: msrd0 <1182023-msrd0@users.noreply.gitlab.com> Date: Wed, 3 Feb 2021 21:22:46 +0000 Subject: [PATCH] Reexports --- README.md | 1 + derive/src/endpoint.rs | 8 ++++---- derive/src/openapi_type.rs | 4 ++-- derive/src/request_body.rs | 2 +- derive/src/resource_error.rs | 2 +- src/auth.rs | 8 +++++--- src/lib.rs | 12 +++++------- src/openapi/handler.rs | 8 +++++--- src/openapi/operation.rs | 2 +- src/result/mod.rs | 9 ++++++--- src/result/no_content.rs | 9 ++++++--- src/result/raw.rs | 8 ++++++-- src/result/result.rs | 4 +++- src/routing.rs | 4 ++-- tests/async_methods.rs | 1 + tests/ui/endpoint/async_state.rs | 2 +- 16 files changed, 50 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 083bf20..8298465 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,7 @@ examples is highly appreciated. [example]: https://gitlab.com/msrd0/gotham-restful/tree/master/example [gotham]: https://gotham.rs/ [serde_json]: https://github.com/serde-rs/json#serde-json---- + [`State`]: gotham::state::State ## Versioning diff --git a/derive/src/endpoint.rs b/derive/src/endpoint.rs index a363ec0..5078383 100644 --- a/derive/src/endpoint.rs +++ b/derive/src/endpoint.rs @@ -130,7 +130,7 @@ impl EndpointType { Self::ReadAll | Self::Search | Self::Create | Self::UpdateAll | Self::DeleteAll => { quote!(::gotham_restful::gotham::extractor::NoopPathExtractor) }, - Self::Read | Self::Update | Self::Delete => quote!(::gotham_restful::export::IdPlaceholder::<#arg_ty>), + Self::Read | Self::Update | Self::Delete => quote!(::gotham_restful::private::IdPlaceholder::<#arg_ty>), Self::Custom { .. } => { if self.has_placeholders().value { arg_ty.to_token_stream() @@ -490,7 +490,7 @@ fn expand_endpoint_type(mut ty: EndpointType, attrs: AttributeArgs, fun: &ItemFn let conn_ty = arg.ty.quote_ty(); state_block = quote! { #state_block - let repo = <::gotham_restful::export::Repo<#conn_ty>>::borrow_from(state).clone(); + let repo = <::gotham_restful::private::Repo<#conn_ty>>::borrow_from(state).clone(); }; handle_content = quote! { repo.run::<_, _, ()>(move |conn| { @@ -500,7 +500,7 @@ fn expand_endpoint_type(mut ty: EndpointType, attrs: AttributeArgs, fun: &ItemFn } Ok(quote! { - use ::gotham_restful::export::FutureExt as _; + use ::gotham_restful::private::FutureExt as _; use ::gotham_restful::gotham::state::FromState as _; #state_block async move { @@ -559,7 +559,7 @@ fn expand_endpoint_type(mut ty: EndpointType, attrs: AttributeArgs, fun: &ItemFn placeholders: Self::Placeholders, params: Self::Params, body: ::std::option::Option - ) -> ::gotham_restful::export::BoxFuture<'a, Self::Output> { + ) -> ::gotham_restful::private::BoxFuture<'a, Self::Output> { #handle_content } diff --git a/derive/src/openapi_type.rs b/derive/src/openapi_type.rs index ae14678..4b4530d 100644 --- a/derive/src/openapi_type.rs +++ b/derive/src/openapi_type.rs @@ -148,7 +148,7 @@ fn expand_enum(ident: Ident, generics: Generics, attrs: Vec, input: D { fn schema() -> #krate::OpenapiSchema { - use #krate::{export::openapi::*, OpenapiSchema}; + use #krate::{private::openapi::*, OpenapiSchema}; let mut enumeration : Vec = Vec::new(); @@ -261,7 +261,7 @@ fn expand_struct(ident: Ident, generics: Generics, attrs: Vec, input: { fn schema() -> #krate::OpenapiSchema { - use #krate::{export::{openapi::*, IndexMap}, OpenapiSchema}; + use #krate::{private::{openapi::*, IndexMap}, OpenapiSchema}; let mut properties : IndexMap>> = IndexMap::new(); let mut required : Vec = Vec::new(); diff --git a/derive/src/request_body.rs b/derive/src/request_body.rs index 077d105..9657b21 100644 --- a/derive/src/request_body.rs +++ b/derive/src/request_body.rs @@ -32,7 +32,7 @@ fn impl_openapi_type(ident: &Ident, generics: &Generics) -> TokenStream { { fn schema() -> #krate::OpenapiSchema { - use #krate::{export::openapi::*, OpenapiSchema}; + use #krate::{private::openapi::*, OpenapiSchema}; OpenapiSchema::new(SchemaKind::Type(Type::String(StringType { format: VariantOrUnknownOrEmpty::Item(StringFormat::Binary), diff --git a/derive/src/resource_error.rs b/derive/src/resource_error.rs index a2b0955..9239c80 100644 --- a/derive/src/resource_error.rs +++ b/derive/src/resource_error.rs @@ -316,7 +316,7 @@ pub fn expand_resource_error(input: DeriveInput) -> Result { impl #generics #krate::IntoResponseError for #ident #generics where #( #were ),* { - type Err = #krate::export::serde_json::Error; + type Err = #krate::private::serde_json::Error; fn into_response_error(self) -> Result<#krate::Response, Self::Err> { diff --git a/src/auth.rs b/src/auth.rs index 935c7bd..fdbab63 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,4 +1,5 @@ -use crate::{AuthError, Forbidden, HeaderName}; +use crate::{AuthError, Forbidden}; + use cookie::CookieJar; use futures_util::{ future, @@ -7,7 +8,7 @@ use futures_util::{ use gotham::{ anyhow, handler::HandlerFuture, - hyper::header::{HeaderMap, AUTHORIZATION}, + hyper::header::{HeaderMap, HeaderName, AUTHORIZATION}, middleware::{cookie::CookieParser, Middleware, NewMiddleware}, state::{FromState, State} }; @@ -15,6 +16,7 @@ use jsonwebtoken::{errors::ErrorKind, DecodingKey}; use serde::de::DeserializeOwned; use std::{marker::PhantomData, panic::RefUnwindSafe, pin::Pin}; +#[doc(no_inline)] pub use jsonwebtoken::Validation as AuthValidation; /// The authentication status returned by the auth middleware for each request. @@ -77,7 +79,7 @@ This trait will help the auth middleware to determine the validity of an authent A very basic implementation could look like this: ``` -# use gotham_restful::{AuthHandler, State}; +# use gotham_restful::{AuthHandler, gotham::state::State}; # const SECRET : &'static [u8; 32] = b"zlBsA2QXnkmpe0QTh8uCvtAEa4j33YAc"; diff --git a/src/lib.rs b/src/lib.rs index 87cb87c..ac58c2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,10 @@ #![warn(missing_debug_implementations, rust_2018_idioms)] -#![deny(broken_intra_doc_links)] #![forbid(unsafe_code)] // can we have a lint for spaces in doc comments please? #![cfg_attr(feature = "cargo-clippy", allow(clippy::tabs_in_doc_comments))] +// intra-doc links only fully work when OpenAPI is enabled +#![cfg_attr(feature = "openapi", deny(broken_intra_doc_links))] +#![cfg_attr(not(feature = "openapi"), allow(broken_intra_doc_links))] /*! This crate is an extension to the popular [gotham web framework][gotham] for Rust. It allows you to create resources with assigned endpoints that aim to be a more convenient way of creating handlers @@ -402,6 +404,7 @@ examples is highly appreciated. [example]: https://gitlab.com/msrd0/gotham-restful/tree/master/example [gotham]: https://gotham.rs/ [serde_json]: https://github.com/serde-rs/json#serde-json---- + [`State`]: gotham::state::State */ #[cfg(all(feature = "openapi", feature = "without-openapi"))] @@ -425,18 +428,13 @@ extern crate serde; #[doc(no_inline)] pub use gotham; #[doc(no_inline)] -pub use gotham::{ - hyper::{header::HeaderName, StatusCode}, - state::{FromState, State} -}; -#[doc(no_inline)] pub use mime::Mime; pub use gotham_restful_derive::*; /// Not public API #[doc(hidden)] -pub mod export { +pub mod private { pub use crate::routing::PathExtractor as IdPlaceholder; pub use futures_util::future::{BoxFuture, FutureExt}; diff --git a/src/openapi/handler.rs b/src/openapi/handler.rs index 6c321b8..fc32c11 100644 --- a/src/openapi/handler.rs +++ b/src/openapi/handler.rs @@ -1,9 +1,11 @@ use super::SECURITY_NAME; + use futures_util::{future, future::FutureExt}; use gotham::{ anyhow, handler::{Handler, HandlerFuture, NewHandler}, helpers::http::response::create_response, + hyper::StatusCode, state::State }; use indexmap::IndexMap; @@ -75,7 +77,7 @@ impl Handler for OpenapiHandler { Ok(openapi) => openapi, Err(e) => { error!("Unable to acquire read lock for the OpenAPI specification: {}", e); - let res = create_response(&state, crate::StatusCode::INTERNAL_SERVER_ERROR, TEXT_PLAIN, ""); + let res = create_response(&state, StatusCode::INTERNAL_SERVER_ERROR, TEXT_PLAIN, ""); return future::ok((state, res)).boxed(); } }; @@ -88,12 +90,12 @@ impl Handler for OpenapiHandler { match serde_json::to_string(&openapi) { Ok(body) => { - let res = create_response(&state, crate::StatusCode::OK, APPLICATION_JSON, body); + let res = create_response(&state, StatusCode::OK, APPLICATION_JSON, body); future::ok((state, res)).boxed() }, Err(e) => { error!("Unable to handle OpenAPI request due to error: {}", e); - let res = create_response(&state, crate::StatusCode::INTERNAL_SERVER_ERROR, TEXT_PLAIN, ""); + let res = create_response(&state, StatusCode::INTERNAL_SERVER_ERROR, TEXT_PLAIN, ""); future::ok((state, res)).boxed() } } diff --git a/src/openapi/operation.rs b/src/openapi/operation.rs index 8c83af5..06e1ce7 100644 --- a/src/openapi/operation.rs +++ b/src/openapi/operation.rs @@ -78,7 +78,7 @@ impl OperationParams { pub struct OperationDescription { operation_id: Option, - default_status: crate::StatusCode, + default_status: gotham::hyper::StatusCode, accepted_types: Option>, schema: ReferenceOr, params: OperationParams, diff --git a/src/result/mod.rs b/src/result/mod.rs index d5895aa..deb9d12 100644 --- a/src/result/mod.rs +++ b/src/result/mod.rs @@ -1,7 +1,10 @@ #[cfg(feature = "openapi")] use crate::OpenapiSchema; use crate::Response; + use futures_util::future::FutureExt; +#[cfg(feature = "openapi")] +use gotham::hyper::StatusCode; use mime::{Mime, STAR_STAR}; use serde::Serialize; use std::{ @@ -57,8 +60,8 @@ pub trait ResourceResult { fn schema() -> OpenapiSchema; #[cfg(feature = "openapi")] - fn default_status() -> crate::StatusCode { - crate::StatusCode::OK + fn default_status() -> StatusCode { + StatusCode::OK } } @@ -141,7 +144,7 @@ where } #[cfg(feature = "openapi")] - fn default_status() -> crate::StatusCode { + fn default_status() -> StatusCode { Res::default_status() } } diff --git a/src/result/no_content.rs b/src/result/no_content.rs index 88d624e..30413fd 100644 --- a/src/result/no_content.rs +++ b/src/result/no_content.rs @@ -2,7 +2,10 @@ use super::{handle_error, ResourceResult}; use crate::{IntoResponseError, Response}; #[cfg(feature = "openapi")] use crate::{OpenapiSchema, OpenapiType}; + use futures_util::{future, future::FutureExt}; +#[cfg(feature = "openapi")] +use gotham::hyper::StatusCode; use mime::Mime; use std::{fmt::Display, future::Future, pin::Pin}; @@ -58,8 +61,8 @@ impl ResourceResult for NoContent { /// This will always be a _204 No Content_ #[cfg(feature = "openapi")] - fn default_status() -> crate::StatusCode { - crate::StatusCode::NO_CONTENT + fn default_status() -> StatusCode { + StatusCode::NO_CONTENT } } @@ -86,7 +89,7 @@ where } #[cfg(feature = "openapi")] - fn default_status() -> crate::StatusCode { + fn default_status() -> StatusCode { NoContent::default_status() } } diff --git a/src/result/raw.rs b/src/result/raw.rs index de0f82e..fe35143 100644 --- a/src/result/raw.rs +++ b/src/result/raw.rs @@ -1,10 +1,14 @@ use super::{handle_error, IntoResponseError, ResourceResult}; #[cfg(feature = "openapi")] use crate::OpenapiSchema; -use crate::{FromBody, RequestBody, ResourceType, Response, StatusCode}; +use crate::{FromBody, RequestBody, ResourceType, Response}; + use futures_core::future::Future; use futures_util::{future, future::FutureExt}; -use gotham::hyper::body::{Body, Bytes}; +use gotham::hyper::{ + body::{Body, Bytes}, + StatusCode +}; use mime::Mime; #[cfg(feature = "openapi")] use openapiv3::{SchemaKind, StringFormat, StringType, Type, VariantOrUnknownOrEmpty}; diff --git a/src/result/result.rs b/src/result/result.rs index f22d756..2b8afe6 100644 --- a/src/result/result.rs +++ b/src/result/result.rs @@ -1,8 +1,10 @@ use super::{handle_error, into_response_helper, ResourceResult}; #[cfg(feature = "openapi")] use crate::OpenapiSchema; -use crate::{result::ResourceError, Response, ResponseBody, StatusCode}; +use crate::{result::ResourceError, Response, ResponseBody}; + use futures_core::future::Future; +use gotham::hyper::StatusCode; use mime::{Mime, APPLICATION_JSON}; use std::{error::Error, fmt::Display, pin::Pin}; diff --git a/src/routing.rs b/src/routing.rs index afbd857..f20bbf7 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -5,13 +5,13 @@ use crate::openapi::{ }; use crate::{ result::{ResourceError, ResourceResult}, - Endpoint, FromBody, Resource, Response, StatusCode + Endpoint, FromBody, Resource, Response }; use gotham::{ handler::HandlerError, helpers::http::response::{create_empty_response, create_response}, - hyper::{body::to_bytes, header::CONTENT_TYPE, Body, HeaderMap, Method}, + hyper::{body::to_bytes, header::CONTENT_TYPE, Body, HeaderMap, Method, StatusCode}, pipeline::chain::PipelineHandleChain, router::{ builder::{DefineSingleRoute, DrawRoutes, RouterBuilder, ScopeBuilder}, diff --git a/tests/async_methods.rs b/tests/async_methods.rs index 21c3462..35ec42a 100644 --- a/tests/async_methods.rs +++ b/tests/async_methods.rs @@ -4,6 +4,7 @@ extern crate gotham_derive; use gotham::{ hyper::{HeaderMap, Method}, router::builder::*, + state::State, test::TestServer }; use gotham_restful::*; diff --git a/tests/ui/endpoint/async_state.rs b/tests/ui/endpoint/async_state.rs index 85a23a4..d951370 100644 --- a/tests/ui/endpoint/async_state.rs +++ b/tests/ui/endpoint/async_state.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate gotham_restful; -use gotham_restful::State; +use gotham::state::State; #[derive(Resource)] #[resource(read_all)]