diff --git a/ruma-api-macros/src/api.rs b/ruma-api-macros/src/api.rs index 326576dd..627ea442 100644 --- a/ruma-api-macros/src/api.rs +++ b/ruma-api-macros/src/api.rs @@ -317,7 +317,7 @@ impl ToTokens for Api { } } else if self.request.has_body_fields() { quote! { - let request_body: ::Incoming = + let request_body: ::Incoming = ruma_api::exports::serde_json::from_slice(request.body().as_slice())?; } } else { @@ -337,7 +337,7 @@ impl ToTokens for Api { }; let response_body_type_annotation = if self.response.has_body_fields() { - quote!(: ::Incoming) + quote!(: ::Incoming) } else { TokenStream::new() }; diff --git a/ruma-api-macros/src/api/request.rs b/ruma-api-macros/src/api/request.rs index e6261780..a6efc456 100644 --- a/ruma-api-macros/src/api/request.rs +++ b/ruma-api-macros/src/api/request.rs @@ -293,7 +293,7 @@ impl TryFrom for Request { impl ToTokens for Request { fn to_tokens(&self, tokens: &mut TokenStream) { let request_struct_header = quote! { - #[derive(Debug, Clone, ruma_api::SendRecv)] + #[derive(Debug, Clone, ruma_api::Outgoing)] #[incoming_no_deserialize] pub struct Request }; @@ -326,7 +326,7 @@ impl ToTokens for Request { /// Data in the request body. #[derive( Debug, - ruma_api::SendRecv, + ruma_api::Outgoing, ruma_api::exports::serde::Serialize, #derive_deserialize )] @@ -345,7 +345,7 @@ impl ToTokens for Request { /// Data in the request body. #[derive( Debug, - ruma_api::SendRecv, + ruma_api::Outgoing, ruma_api::exports::serde::Serialize, #derive_deserialize )] diff --git a/ruma-api-macros/src/api/response.rs b/ruma-api-macros/src/api/response.rs index eb0abe8b..29f2b4ae 100644 --- a/ruma-api-macros/src/api/response.rs +++ b/ruma-api-macros/src/api/response.rs @@ -232,7 +232,7 @@ impl TryFrom for Response { impl ToTokens for Response { fn to_tokens(&self, tokens: &mut TokenStream) { let response_struct_header = quote! { - #[derive(Debug, Clone, ruma_api::SendRecv)] + #[derive(Debug, Clone, ruma_api::Outgoing)] #[incoming_no_deserialize] pub struct Response }; @@ -265,7 +265,7 @@ impl ToTokens for Response { /// Data in the response body. #[derive( Debug, - ruma_api::SendRecv, + ruma_api::Outgoing, ruma_api::exports::serde::Serialize, #derive_deserialize )] @@ -284,7 +284,7 @@ impl ToTokens for Response { /// Data in the response body. #[derive( Debug, - ruma_api::SendRecv, + ruma_api::Outgoing, ruma_api::exports::serde::Serialize, #derive_deserialize )] diff --git a/ruma-api-macros/src/send_recv.rs b/ruma-api-macros/src/derive_outgoing.rs similarity index 94% rename from ruma-api-macros/src/send_recv.rs rename to ruma-api-macros/src/derive_outgoing.rs index 448e14e1..8d8e866b 100644 --- a/ruma-api-macros/src/send_recv.rs +++ b/ruma-api-macros/src/derive_outgoing.rs @@ -11,7 +11,7 @@ mod wrap_incoming; use wrap_incoming::Meta; -pub fn expand_send_recv(input: DeriveInput) -> syn::Result { +pub fn expand_derive_outgoing(input: DeriveInput) -> syn::Result { let derive_deserialize = if no_deserialize_in_attrs(&input.attrs) { TokenStream::new() } else { @@ -20,7 +20,7 @@ pub fn expand_send_recv(input: DeriveInput) -> syn::Result { let mut fields: Vec<_> = match input.data { 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 { 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 { } 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 incoming_ident = Ident::new(&format!("Incoming{}", original_ident), Span::call_site()); @@ -76,7 +76,7 @@ pub fn expand_send_recv(input: DeriveInput) -> syn::Result { #(#fields,)* } - impl ruma_api::SendRecv for #original_ident { + impl ruma_api::Outgoing for #original_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 { quote! { - impl ruma_api::SendRecv for #ident { + impl ruma_api::Outgoing for #ident { type Incoming = Self; } } diff --git a/ruma-api-macros/src/send_recv/wrap_incoming.rs b/ruma-api-macros/src/derive_outgoing/wrap_incoming.rs similarity index 100% rename from ruma-api-macros/src/send_recv/wrap_incoming.rs rename to ruma-api-macros/src/derive_outgoing/wrap_incoming.rs diff --git a/ruma-api-macros/src/lib.rs b/ruma-api-macros/src/lib.rs index f20378d3..d2024306 100644 --- a/ruma-api-macros/src/lib.rs +++ b/ruma-api-macros/src/lib.rs @@ -21,11 +21,11 @@ use syn::{parse_macro_input, DeriveInput}; use self::{ api::{Api, RawApi}, - send_recv::expand_send_recv, + derive_outgoing::expand_derive_outgoing, }; mod api; -mod send_recv; +mod derive_outgoing; #[proc_macro] 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 /// fields of the struct, this simple implementation will be generated: /// /// ```ignore -/// impl SendRecv for MyType { +/// impl Outgoing for MyType { /// 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: /// /// ```ignore -/// #[derive(SendRecv)] +/// #[derive(Outgoing)] /// struct MyType { /// pub foo: Foo, /// #[wrap_incoming] @@ -73,8 +73,8 @@ pub fn ruma_api(input: TokenStream) -> TokenStream { /// pub ys: Vec>, /// } /// ``` -#[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 { 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() } diff --git a/src/lib.rs b/src/lib.rs index b81410c3..94a29b54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,16 +200,16 @@ use serde_urlencoded; /// /// ## 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 -/// documentation for [`SendRecv`][]. +/// documentation for [`Outgoing`][]. /// -/// [`SendRecv`]: derive.SendRecv.html +/// [`Outgoing`]: derive.Outgoing.html #[cfg(feature = "with-ruma-api-macros")] pub use ruma_api_macros::ruma_api; #[cfg(feature = "with-ruma-api-macros")] -pub use ruma_api_macros::SendRecv; +pub use ruma_api_macros::Outgoing; #[cfg(feature = "with-ruma-api-macros")] #[doc(hidden)] @@ -224,11 +224,13 @@ pub mod exports { pub use url; } -/// A type that can be sent as well as received. Types that implement this trait have a -/// corresponding 'Incoming' type, which is either just `Self`, or another type that has the same -/// fields with some types exchanged by ones that allow fallible deserialization, e.g. `EventResult` -/// from ruma_events. -pub trait SendRecv { +/// A type that can be sent to another party that understands the matrix protocol. If any of the +/// fields of `Self` don't implement serde's `Deserialize`, you can derive this trait to generate a +/// corresponding 'Incoming' type that supports deserialization. This is useful for things like +/// ruma_events' `EventResult` type. For more details, see the [derive macro's documentation][doc]. +/// +/// [doc]: derive.Outgoing.html +pub trait Outgoing { /// The 'Incoming' variant of `Self`. type Incoming; } @@ -236,13 +238,13 @@ pub trait SendRecv { /// A Matrix API endpoint. /// /// The type implementing this trait contains any data needed to make a request to the endpoint. -pub trait Endpoint: SendRecv + TryInto>, Error = Error> +pub trait Endpoint: Outgoing + TryInto>, Error = Error> where - ::Incoming: TryFrom>, Error = Error>, - ::Incoming: TryFrom>, Error = Error>, + ::Incoming: TryFrom>, Error = Error>, + ::Incoming: TryFrom>, Error = Error>, { /// Data returned in a successful response from the endpoint. - type Response: SendRecv + TryInto>, Error = Error>; + type Response: Outgoing + TryInto>, Error = Error>; /// Metadata about the endpoint. const METADATA: Metadata; @@ -377,7 +379,7 @@ mod tests { use serde::{de::IntoDeserializer, Deserialize, Serialize}; use serde_json; - use crate::{Endpoint, Error, Metadata, SendRecv}; + use crate::{Endpoint, Error, Metadata, Outgoing}; /// A request to create a new room alias. #[derive(Debug)] @@ -386,7 +388,7 @@ mod tests { pub room_alias: RoomAliasId, // path } - impl SendRecv for Request { + impl Outgoing for Request { type Incoming = Self; } @@ -453,7 +455,7 @@ mod tests { #[derive(Clone, Copy, Debug)] pub struct Response; - impl SendRecv for Response { + impl Outgoing for Response { type Incoming = Self; }