Introduce client and server feature flags for *-api crates

This commit is contained in:
Akshay 2021-03-05 20:30:35 +05:30 committed by GitHub
parent c27e66741a
commit 4d51e98707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 59 additions and 5 deletions

View File

@ -267,12 +267,14 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
quote! {
#( #attrs )*
#[automatically_derived]
#[cfg(feature = "client")]
impl #request_lifetimes #ruma_api::OutgoingNonAuthRequest
for Request #request_lifetimes
{}
#( #attrs )*
#[automatically_derived]
#[cfg(feature = "server")]
impl #ruma_api::IncomingNonAuthRequest for #incoming_request_type {}
}
}
@ -287,6 +289,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
#response_type
#[automatically_derived]
#[cfg(feature = "server")]
impl ::std::convert::TryFrom<Response> for #http::Response<Vec<u8>> {
type Error = #ruma_api::error::IntoHttpError;
@ -308,6 +311,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
}
#[automatically_derived]
#[cfg(feature = "client")]
impl ::std::convert::TryFrom<#http::Response<Vec<u8>>> for Response {
type Error = #ruma_api::error::FromHttpResponseError<#error>;
@ -344,6 +348,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
};
#[automatically_derived]
#[cfg(feature = "client")]
impl #request_lifetimes #ruma_api::OutgoingRequest for Request #request_lifetimes {
type EndpointError = #error;
type IncomingResponse = <Response as #ruma_serde::Outgoing>::Incoming;
@ -386,6 +391,7 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
}
#[automatically_derived]
#[cfg(feature = "server")]
impl #ruma_api::IncomingRequest for #incoming_request_type {
type EndpointError = #error;
type OutgoingResponse = Response;

View File

@ -31,3 +31,9 @@ thiserror = "1.0.23"
[dev-dependencies]
ruma-events = { version = "=0.22.0-alpha.2", path = "../ruma-events" }
trybuild = "1.0.38"
# These feature gates exist as a workaround to pass tests.
# Any crate that invokes the `ruma-api!` macro should include these features.
[features]
client = []
server = []

View File

@ -34,6 +34,7 @@ ruma_api! {
}
}
#[cfg(all(feature = "client", feature = "server"))]
#[test]
fn request_serde() -> Result<(), Box<dyn std::error::Error + 'static>> {
let req = Request {

View File

@ -27,6 +27,7 @@ ruma_api! {
}
}
#[cfg(all(feature = "client", feature = "server"))]
#[test]
fn response_content_type_override() {
let res = Response { stuff: "magic".into() };
@ -44,6 +45,7 @@ fn response_content_type_override() {
assert_eq!(http_res.headers().get("content-type").unwrap(), "magic");
}
#[cfg(feature = "client")]
#[test]
fn request_content_type_override() {
let req = Request { location: None, stuff: "magic".into() };

View File

@ -16,6 +16,7 @@ ruma_api! {
response: {}
}
#[cfg(feature = "client")]
#[test]
fn empty_request_http_repr() {
let req = Request {};
@ -24,6 +25,7 @@ fn empty_request_http_repr() {
assert!(http_req.body().is_empty());
}
#[cfg(feature = "server")]
#[test]
fn empty_response_http_repr() {
let res = Response {};

View File

@ -22,6 +22,8 @@ serde_json = "1.0.61"
[features]
unstable-exhaustive-types = []
client = []
server = []
[dev-dependencies]
matches = "0.1.8"

View File

@ -43,6 +43,7 @@ impl Response {
}
}
#[cfg(feature = "server")]
#[cfg(test)]
mod tests {
use ruma_api::{exports::http, OutgoingRequest};

View File

@ -38,3 +38,5 @@ compat = []
unstable-exhaustive-types = []
unstable-pre-spec = []
unstable-synapse-quirks = []
client = []
server = []

View File

@ -75,6 +75,7 @@ struct ResponseBody {
event_id: EventId,
}
#[cfg(feature = "server")]
impl TryFrom<Response> for http::Response<Vec<u8>> {
type Error = IntoHttpError;
@ -88,6 +89,7 @@ impl TryFrom<Response> for http::Response<Vec<u8>> {
}
}
#[cfg(feature = "client")]
impl TryFrom<http::Response<Vec<u8>>> for Response {
type Error = FromHttpResponseError<crate::Error>;
@ -109,6 +111,7 @@ impl TryFrom<http::Response<Vec<u8>>> for Response {
}
}
#[cfg(feature = "client")]
impl<'a> ruma_api::OutgoingRequest for Request<'a> {
type EndpointError = crate::Error;
type IncomingResponse = Response;
@ -152,6 +155,7 @@ impl<'a> ruma_api::OutgoingRequest for Request<'a> {
}
}
#[cfg(feature = "server")]
impl ruma_api::IncomingRequest for IncomingRequest {
type EndpointError = crate::Error;
type OutgoingResponse = Response;

View File

@ -68,6 +68,7 @@ struct ResponseBody {
event_id: EventId,
}
#[cfg(feature = "server")]
impl TryFrom<Response> for http::Response<Vec<u8>> {
type Error = IntoHttpError;
@ -81,6 +82,7 @@ impl TryFrom<Response> for http::Response<Vec<u8>> {
}
}
#[cfg(feature = "client")]
impl TryFrom<http::Response<Vec<u8>>> for Response {
type Error = FromHttpResponseError<crate::Error>;
@ -102,6 +104,7 @@ impl TryFrom<http::Response<Vec<u8>>> for Response {
}
}
#[cfg(feature = "client")]
impl<'a> ruma_api::OutgoingRequest for Request<'a> {
type EndpointError = crate::Error;
type IncomingResponse = Response;
@ -144,6 +147,7 @@ impl<'a> ruma_api::OutgoingRequest for Request<'a> {
}
}
#[cfg(feature = "server")]
impl ruma_api::IncomingRequest for IncomingRequest {
type EndpointError = crate::Error;
type OutgoingResponse = Response;

View File

@ -71,6 +71,7 @@ struct ResponseBody {
event_id: EventId,
}
#[cfg(feature = "server")]
impl TryFrom<Response> for http::Response<Vec<u8>> {
type Error = IntoHttpError;
@ -84,6 +85,7 @@ impl TryFrom<Response> for http::Response<Vec<u8>> {
}
}
#[cfg(feature = "client")]
impl TryFrom<http::Response<Vec<u8>>> for Response {
type Error = FromHttpResponseError<crate::Error>;
@ -105,6 +107,7 @@ impl TryFrom<http::Response<Vec<u8>>> for Response {
}
}
#[cfg(feature = "client")]
impl<'a> ruma_api::OutgoingRequest for Request<'a> {
type EndpointError = crate::Error;
type IncomingResponse = Response;
@ -148,6 +151,7 @@ impl<'a> ruma_api::OutgoingRequest for Request<'a> {
}
}
#[cfg(feature = "server")]
impl ruma_api::IncomingRequest for IncomingRequest {
type EndpointError = crate::Error;
type OutgoingResponse = Response;

View File

@ -30,3 +30,5 @@ matches = "0.1.8"
[features]
unstable-exhaustive-types = []
unstable-pre-spec = []
client = []
server = []

View File

@ -20,3 +20,5 @@ serde_json = "1.0.61"
[features]
unstable-exhaustive-types = []
client = []
server = []

View File

@ -22,3 +22,5 @@ serde_json = "1.0.61"
[features]
unstable-exhaustive-types = []
client = []
server = []

View File

@ -41,11 +41,25 @@ api = ["ruma-api"]
events = ["ruma-events"]
signatures = ["ruma-signatures"]
appservice-api = ["api", "events", "ruma-appservice-api"]
client-api = ["api", "events", "ruma-client-api"]
federation-api = ["api", "signatures", "ruma-federation-api"]
identity-service-api = ["api", "ruma-identity-service-api"]
push-gateway-api = ["api", "ruma-push-gateway-api"]
appservice-api-c = ["api", "events", "ruma-appservice-api/client"]
appservice-api-s = ["api", "events", "ruma-appservice-api/server"]
appservice-api = ["appservice-api-c", "appservice-api-s"]
client-api-c = ["api", "events", "ruma-client-api/client"]
client-api-s = ["api", "events", "ruma-client-api/server"]
client-api = ["client-api-c", "client-api-s"]
federation-api-c = ["api", "signatures", "ruma-federation-api/client"]
federation-api-s = ["api", "signatures", "ruma-federation-api/server"]
federation-api = ["federation-api-c", "federation-api-s"]
identity-service-api-c = ["api", "ruma-identity-service-api/client"]
identity-service-api-s = ["api", "ruma-identity-service-api/server"]
identity-service-api = ["identity-service-api-c", "identity-service-api-s"]
push-gateway-api-c = ["api", "ruma-push-gateway-api/client"]
push-gateway-api-s = ["api", "ruma-push-gateway-api/server"]
push-gateway-api = ["push-gateway-api-c", "push-gateway-api-s"]
# Convenience features
either = ["ruma-identifiers/either"]