Rename SendRecv to Outgoing
This commit is contained in:
parent
4c1d6b4b58
commit
d94e15e38c
@ -317,7 +317,7 @@ impl ToTokens for Api {
|
|||||||
}
|
}
|
||||||
} else if self.request.has_body_fields() {
|
} else if self.request.has_body_fields() {
|
||||||
quote! {
|
quote! {
|
||||||
let request_body: <RequestBody as ruma_api::SendRecv>::Incoming =
|
let request_body: <RequestBody as ruma_api::Outgoing>::Incoming =
|
||||||
ruma_api::exports::serde_json::from_slice(request.body().as_slice())?;
|
ruma_api::exports::serde_json::from_slice(request.body().as_slice())?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -337,7 +337,7 @@ impl ToTokens for Api {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let response_body_type_annotation = if self.response.has_body_fields() {
|
let response_body_type_annotation = if self.response.has_body_fields() {
|
||||||
quote!(: <ResponseBody as ruma_api::SendRecv>::Incoming)
|
quote!(: <ResponseBody as ruma_api::Outgoing>::Incoming)
|
||||||
} else {
|
} else {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
};
|
};
|
||||||
|
@ -293,7 +293,7 @@ impl TryFrom<RawRequest> for Request {
|
|||||||
impl ToTokens for Request {
|
impl ToTokens for Request {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
let request_struct_header = quote! {
|
let request_struct_header = quote! {
|
||||||
#[derive(Debug, Clone, ruma_api::SendRecv)]
|
#[derive(Debug, Clone, ruma_api::Outgoing)]
|
||||||
#[incoming_no_deserialize]
|
#[incoming_no_deserialize]
|
||||||
pub struct Request
|
pub struct Request
|
||||||
};
|
};
|
||||||
@ -326,7 +326,7 @@ impl ToTokens for Request {
|
|||||||
/// Data in the request body.
|
/// Data in the request body.
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug,
|
Debug,
|
||||||
ruma_api::SendRecv,
|
ruma_api::Outgoing,
|
||||||
ruma_api::exports::serde::Serialize,
|
ruma_api::exports::serde::Serialize,
|
||||||
#derive_deserialize
|
#derive_deserialize
|
||||||
)]
|
)]
|
||||||
@ -345,7 +345,7 @@ impl ToTokens for Request {
|
|||||||
/// Data in the request body.
|
/// Data in the request body.
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug,
|
Debug,
|
||||||
ruma_api::SendRecv,
|
ruma_api::Outgoing,
|
||||||
ruma_api::exports::serde::Serialize,
|
ruma_api::exports::serde::Serialize,
|
||||||
#derive_deserialize
|
#derive_deserialize
|
||||||
)]
|
)]
|
||||||
|
@ -232,7 +232,7 @@ impl TryFrom<RawResponse> for Response {
|
|||||||
impl ToTokens for Response {
|
impl ToTokens for Response {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
let response_struct_header = quote! {
|
let response_struct_header = quote! {
|
||||||
#[derive(Debug, Clone, ruma_api::SendRecv)]
|
#[derive(Debug, Clone, ruma_api::Outgoing)]
|
||||||
#[incoming_no_deserialize]
|
#[incoming_no_deserialize]
|
||||||
pub struct Response
|
pub struct Response
|
||||||
};
|
};
|
||||||
@ -265,7 +265,7 @@ impl ToTokens for Response {
|
|||||||
/// Data in the response body.
|
/// Data in the response body.
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug,
|
Debug,
|
||||||
ruma_api::SendRecv,
|
ruma_api::Outgoing,
|
||||||
ruma_api::exports::serde::Serialize,
|
ruma_api::exports::serde::Serialize,
|
||||||
#derive_deserialize
|
#derive_deserialize
|
||||||
)]
|
)]
|
||||||
@ -284,7 +284,7 @@ impl ToTokens for Response {
|
|||||||
/// Data in the response body.
|
/// Data in the response body.
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug,
|
Debug,
|
||||||
ruma_api::SendRecv,
|
ruma_api::Outgoing,
|
||||||
ruma_api::exports::serde::Serialize,
|
ruma_api::exports::serde::Serialize,
|
||||||
#derive_deserialize
|
#derive_deserialize
|
||||||
)]
|
)]
|
||||||
|
@ -11,7 +11,7 @@ mod wrap_incoming;
|
|||||||
|
|
||||||
use wrap_incoming::Meta;
|
use wrap_incoming::Meta;
|
||||||
|
|
||||||
pub fn expand_send_recv(input: DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||||
let derive_deserialize = if no_deserialize_in_attrs(&input.attrs) {
|
let derive_deserialize = if no_deserialize_in_attrs(&input.attrs) {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
} else {
|
} else {
|
||||||
@ -20,7 +20,7 @@ pub fn expand_send_recv(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
|
|
||||||
let mut fields: Vec<_> = match input.data {
|
let mut fields: Vec<_> = match input.data {
|
||||||
Data::Enum(_) | Data::Union(_) => {
|
Data::Enum(_) | Data::Union(_) => {
|
||||||
panic!("#[derive(SendRecv)] is only supported for structs")
|
panic!("#[derive(Outgoing)] is only supported for structs")
|
||||||
}
|
}
|
||||||
Data::Struct(s) => match s.fields {
|
Data::Struct(s) => match s.fields {
|
||||||
Fields::Named(fs) => fs.named.into_pairs().map(Pair::into_value).collect(),
|
Fields::Named(fs) => fs.named.into_pairs().map(Pair::into_value).collect(),
|
||||||
@ -65,7 +65,7 @@ pub fn expand_send_recv(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let vis = input.vis;
|
let vis = input.vis;
|
||||||
let doc = format!("\"Incoming\" variant of [{ty}](struct.{ty}.html).", ty = input.ident);
|
let doc = format!("'Incoming' variant of [{ty}](struct.{ty}.html).", ty = input.ident);
|
||||||
let original_ident = input.ident;
|
let original_ident = input.ident;
|
||||||
let incoming_ident = Ident::new(&format!("Incoming{}", original_ident), Span::call_site());
|
let incoming_ident = Ident::new(&format!("Incoming{}", original_ident), Span::call_site());
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ pub fn expand_send_recv(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
#(#fields,)*
|
#(#fields,)*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ruma_api::SendRecv for #original_ident {
|
impl ruma_api::Outgoing for #original_ident {
|
||||||
type Incoming = #incoming_ident;
|
type Incoming = #incoming_ident;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -99,7 +99,7 @@ fn no_deserialize_in_attrs(attrs: &[Attribute]) -> bool {
|
|||||||
|
|
||||||
fn impl_send_recv_incoming_self(ident: Ident) -> TokenStream {
|
fn impl_send_recv_incoming_self(ident: Ident) -> TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
impl ruma_api::SendRecv for #ident {
|
impl ruma_api::Outgoing for #ident {
|
||||||
type Incoming = Self;
|
type Incoming = Self;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,11 +21,11 @@ use syn::{parse_macro_input, DeriveInput};
|
|||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
api::{Api, RawApi},
|
api::{Api, RawApi},
|
||||||
send_recv::expand_send_recv,
|
derive_outgoing::expand_derive_outgoing,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod send_recv;
|
mod derive_outgoing;
|
||||||
|
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn ruma_api(input: TokenStream) -> TokenStream {
|
pub fn ruma_api(input: TokenStream) -> TokenStream {
|
||||||
@ -36,12 +36,12 @@ pub fn ruma_api(input: TokenStream) -> TokenStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derive the `SendRecv` trait, possibly generating an 'Incoming' version of the struct this
|
/// Derive the `Outgoing` trait, possibly generating an 'Incoming' version of the struct this
|
||||||
/// derive macro is used on. Specifically, if no `#[wrap_incoming]` attribute is used on any of the
|
/// derive macro is used on. Specifically, if no `#[wrap_incoming]` attribute is used on any of the
|
||||||
/// fields of the struct, this simple implementation will be generated:
|
/// fields of the struct, this simple implementation will be generated:
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// impl SendRecv for MyType {
|
/// impl Outgoing for MyType {
|
||||||
/// type Incoming = Self;
|
/// type Incoming = Self;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -51,7 +51,7 @@ pub fn ruma_api(input: TokenStream) -> TokenStream {
|
|||||||
/// is generated, with all of the fields with `#[wrap_incoming]` replaced:
|
/// is generated, with all of the fields with `#[wrap_incoming]` replaced:
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// #[derive(SendRecv)]
|
/// #[derive(Outgoing)]
|
||||||
/// struct MyType {
|
/// struct MyType {
|
||||||
/// pub foo: Foo,
|
/// pub foo: Foo,
|
||||||
/// #[wrap_incoming]
|
/// #[wrap_incoming]
|
||||||
@ -73,8 +73,8 @@ pub fn ruma_api(input: TokenStream) -> TokenStream {
|
|||||||
/// pub ys: Vec<EventResult<YEvent>>,
|
/// pub ys: Vec<EventResult<YEvent>>,
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[proc_macro_derive(SendRecv, attributes(wrap_incoming, incoming_no_deserialize))]
|
#[proc_macro_derive(Outgoing, attributes(wrap_incoming, incoming_no_deserialize))]
|
||||||
pub fn derive_send_recv(input: TokenStream) -> TokenStream {
|
pub fn derive_send_recv(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_send_recv(input).unwrap_or_else(|err| err.to_compile_error()).into()
|
expand_derive_outgoing(input).unwrap_or_else(|err| err.to_compile_error()).into()
|
||||||
}
|
}
|
||||||
|
34
src/lib.rs
34
src/lib.rs
@ -200,16 +200,16 @@ use serde_urlencoded;
|
|||||||
///
|
///
|
||||||
/// ## Fallible deserialization
|
/// ## Fallible deserialization
|
||||||
///
|
///
|
||||||
/// All request and response types also derive `ruma_api::SendRecv`. As such, to allow fallible
|
/// All request and response types also derive `ruma_api::Outgoing`. As such, to allow fallible
|
||||||
/// deserialization, you can use the `#[wrap_incoming]` attribute. For details, see the
|
/// deserialization, you can use the `#[wrap_incoming]` attribute. For details, see the
|
||||||
/// documentation for [`SendRecv`][].
|
/// documentation for [`Outgoing`][].
|
||||||
///
|
///
|
||||||
/// [`SendRecv`]: derive.SendRecv.html
|
/// [`Outgoing`]: derive.Outgoing.html
|
||||||
#[cfg(feature = "with-ruma-api-macros")]
|
#[cfg(feature = "with-ruma-api-macros")]
|
||||||
pub use ruma_api_macros::ruma_api;
|
pub use ruma_api_macros::ruma_api;
|
||||||
|
|
||||||
#[cfg(feature = "with-ruma-api-macros")]
|
#[cfg(feature = "with-ruma-api-macros")]
|
||||||
pub use ruma_api_macros::SendRecv;
|
pub use ruma_api_macros::Outgoing;
|
||||||
|
|
||||||
#[cfg(feature = "with-ruma-api-macros")]
|
#[cfg(feature = "with-ruma-api-macros")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@ -224,11 +224,13 @@ pub mod exports {
|
|||||||
pub use url;
|
pub use url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type that can be sent as well as received. Types that implement this trait have a
|
/// A type that can be sent to another party that understands the matrix protocol. If any of the
|
||||||
/// corresponding 'Incoming' type, which is either just `Self`, or another type that has the same
|
/// fields of `Self` don't implement serde's `Deserialize`, you can derive this trait to generate a
|
||||||
/// fields with some types exchanged by ones that allow fallible deserialization, e.g. `EventResult`
|
/// corresponding 'Incoming' type that supports deserialization. This is useful for things like
|
||||||
/// from ruma_events.
|
/// ruma_events' `EventResult` type. For more details, see the [derive macro's documentation][doc].
|
||||||
pub trait SendRecv {
|
///
|
||||||
|
/// [doc]: derive.Outgoing.html
|
||||||
|
pub trait Outgoing {
|
||||||
/// The 'Incoming' variant of `Self`.
|
/// The 'Incoming' variant of `Self`.
|
||||||
type Incoming;
|
type Incoming;
|
||||||
}
|
}
|
||||||
@ -236,13 +238,13 @@ pub trait SendRecv {
|
|||||||
/// A Matrix API endpoint.
|
/// A Matrix API endpoint.
|
||||||
///
|
///
|
||||||
/// The type implementing this trait contains any data needed to make a request to the endpoint.
|
/// The type implementing this trait contains any data needed to make a request to the endpoint.
|
||||||
pub trait Endpoint: SendRecv + TryInto<http::Request<Vec<u8>>, Error = Error>
|
pub trait Endpoint: Outgoing + TryInto<http::Request<Vec<u8>>, Error = Error>
|
||||||
where
|
where
|
||||||
<Self as SendRecv>::Incoming: TryFrom<http::Request<Vec<u8>>, Error = Error>,
|
<Self as Outgoing>::Incoming: TryFrom<http::Request<Vec<u8>>, Error = Error>,
|
||||||
<Self::Response as SendRecv>::Incoming: TryFrom<http::Response<Vec<u8>>, Error = Error>,
|
<Self::Response as Outgoing>::Incoming: TryFrom<http::Response<Vec<u8>>, Error = Error>,
|
||||||
{
|
{
|
||||||
/// Data returned in a successful response from the endpoint.
|
/// Data returned in a successful response from the endpoint.
|
||||||
type Response: SendRecv + TryInto<http::Response<Vec<u8>>, Error = Error>;
|
type Response: Outgoing + TryInto<http::Response<Vec<u8>>, Error = Error>;
|
||||||
|
|
||||||
/// Metadata about the endpoint.
|
/// Metadata about the endpoint.
|
||||||
const METADATA: Metadata;
|
const METADATA: Metadata;
|
||||||
@ -377,7 +379,7 @@ mod tests {
|
|||||||
use serde::{de::IntoDeserializer, Deserialize, Serialize};
|
use serde::{de::IntoDeserializer, Deserialize, Serialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
use crate::{Endpoint, Error, Metadata, SendRecv};
|
use crate::{Endpoint, Error, Metadata, Outgoing};
|
||||||
|
|
||||||
/// A request to create a new room alias.
|
/// A request to create a new room alias.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -386,7 +388,7 @@ mod tests {
|
|||||||
pub room_alias: RoomAliasId, // path
|
pub room_alias: RoomAliasId, // path
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SendRecv for Request {
|
impl Outgoing for Request {
|
||||||
type Incoming = Self;
|
type Incoming = Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -453,7 +455,7 @@ mod tests {
|
|||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct Response;
|
pub struct Response;
|
||||||
|
|
||||||
impl SendRecv for Response {
|
impl Outgoing for Response {
|
||||||
type Incoming = Self;
|
type Incoming = Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user