api: Change Incoming derive to use owned ID types

… except for a few ID types that don't have an owned variant yet.
This commit is contained in:
Jonas Platte 2022-04-04 15:34:13 +02:00 committed by Jonas Platte
parent 01e080d0b5
commit ab94bed1dc
5 changed files with 21 additions and 18 deletions

View File

@ -7,7 +7,9 @@ pub mod v1 {
//! //!
//! [spec]: https://spec.matrix.org/v1.2/application-service-api/#put_matrixappv1transactionstxnid //! [spec]: https://spec.matrix.org/v1.2/application-service-api/#put_matrixappv1transactionstxnid
use ruma_common::{api::ruma_api, events::AnyRoomEvent, serde::Raw, TransactionId}; use ruma_common::{
api::ruma_api, events::AnyRoomEvent, serde::Raw, OwnedTransactionId, TransactionId,
};
ruma_api! { ruma_api! {
metadata: { metadata: {
@ -44,7 +46,7 @@ pub mod v1 {
impl IncomingRequest { impl IncomingRequest {
/// Creates an `IncomingRequest` with the given transaction ID and list of events. /// Creates an `IncomingRequest` with the given transaction ID and list of events.
pub fn new(txn_id: Box<TransactionId>, events: Vec<Raw<AnyRoomEvent>>) -> IncomingRequest { pub fn new(txn_id: OwnedTransactionId, events: Vec<Raw<AnyRoomEvent>>) -> IncomingRequest {
IncomingRequest { txn_id, events } IncomingRequest { txn_id, events }
} }

View File

@ -91,7 +91,7 @@ pub mod v3 {
json!({ "user_id": "@carl:example.org" }), json!({ "user_id": "@carl:example.org" }),
) )
.unwrap(); .unwrap();
let user_id = user_id!("@carl:example.org").to_owned(); let user_id = user_id!("@carl:example.org").into();
let recipient = IncomingInvitationRecipient::UserId { user_id }; let recipient = IncomingInvitationRecipient::UserId { user_id };
assert_eq!(incoming, recipient); assert_eq!(incoming, recipient);
} }

View File

@ -9,7 +9,7 @@ pub mod v3 {
api::ruma_api, api::ruma_api,
events::{AnyStateEventContent, StateEventType}, events::{AnyStateEventContent, StateEventType},
serde::{Incoming, Raw}, serde::{Incoming, Raw},
RoomId, OwnedRoomId, RoomId,
}; };
ruma_api! { ruma_api! {
@ -144,7 +144,7 @@ pub mod v3 {
{ {
// FIXME: find a way to make this if-else collapse with serde recognizing trailing // FIXME: find a way to make this if-else collapse with serde recognizing trailing
// Option // Option
let (room_id, event_type, state_key): (Box<RoomId>, StateEventType, String) = let (room_id, event_type, state_key): (OwnedRoomId, StateEventType, String) =
if path_args.len() == 3 { if path_args.len() == 3 {
serde::Deserialize::deserialize(serde::de::value::SeqDeserializer::< serde::Deserialize::deserialize(serde::de::value::SeqDeserializer::<
_, _,

View File

@ -9,7 +9,7 @@ pub mod v3 {
api::ruma_api, api::ruma_api,
events::{AnyStateEventContent, StateEventContent, StateEventType}, events::{AnyStateEventContent, StateEventContent, StateEventType},
serde::{Incoming, Raw}, serde::{Incoming, Raw},
EventId, RoomId, EventId, OwnedRoomId, RoomId,
}; };
use serde_json::value::to_raw_value as to_raw_json_value; use serde_json::value::to_raw_value as to_raw_json_value;
@ -174,7 +174,7 @@ pub mod v3 {
{ {
// FIXME: find a way to make this if-else collapse with serde recognizing trailing // FIXME: find a way to make this if-else collapse with serde recognizing trailing
// Option // Option
let (room_id, event_type, state_key): (Box<RoomId>, StateEventType, String) = let (room_id, event_type, state_key): (OwnedRoomId, StateEventType, String) =
if path_args.len() == 3 { if path_args.len() == 3 {
serde::Deserialize::deserialize(serde::de::value::SeqDeserializer::< serde::Deserialize::deserialize(serde::de::value::SeqDeserializer::<
_, _,

View File

@ -20,7 +20,7 @@ pub fn expand_derive_incoming(mut ty_def: DeriveInput) -> syn::Result<TokenStrea
Data::Enum(e) => { Data::Enum(e) => {
for var in &mut e.variants { for var in &mut e.variants {
for field in &mut var.fields { for field in &mut var.fields {
if strip_lifetimes(&mut field.ty) { if strip_lifetimes(&mut field.ty, &ruma_common) {
found_lifetime = true; found_lifetime = true;
} }
} }
@ -31,7 +31,7 @@ pub fn expand_derive_incoming(mut ty_def: DeriveInput) -> syn::Result<TokenStrea
if !matches!(field.vis, syn::Visibility::Public(_)) { if !matches!(field.vis, syn::Visibility::Public(_)) {
return Err(syn::Error::new_spanned(field, "All fields must be marked `pub`")); return Err(syn::Error::new_spanned(field, "All fields must be marked `pub`"));
} }
if strip_lifetimes(&mut field.ty) { if strip_lifetimes(&mut field.ty, &ruma_common) {
found_lifetime = true; found_lifetime = true;
} }
} }
@ -118,7 +118,7 @@ fn clean_generics(generics: &mut Generics) {
.collect(); .collect();
} }
fn strip_lifetimes(field_type: &mut Type) -> bool { fn strip_lifetimes(field_type: &mut Type, ruma_common: &TokenStream) -> bool {
match field_type { match field_type {
// T<'a> -> IncomingT // T<'a> -> IncomingT
// The IncomingT has to be declared by the user of this derive macro. // The IncomingT has to be declared by the user of this derive macro.
@ -137,7 +137,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
.into_iter() .into_iter()
.map(|mut ty| { .map(|mut ty| {
if let GenericArgument::Type(ty) = &mut ty { if let GenericArgument::Type(ty) = &mut ty {
if strip_lifetimes(ty) { if strip_lifetimes(ty, ruma_common) {
has_lifetimes = true; has_lifetimes = true;
}; };
} }
@ -160,7 +160,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
.clone() .clone()
.into_iter() .into_iter()
.map(|mut ty| { .map(|mut ty| {
if strip_lifetimes(&mut ty) { if strip_lifetimes(&mut ty, ruma_common) {
has_lifetimes = true; has_lifetimes = true;
}; };
ty ty
@ -191,6 +191,8 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
if last_seg.ident == "str" { if last_seg.ident == "str" {
// &str -> String // &str -> String
Some(parse_quote! { ::std::string::String }) Some(parse_quote! { ::std::string::String })
} else if last_seg.ident == "RawJsonValue" {
Some(parse_quote! { ::std::boxed::Box<#path> })
} else if last_seg.ident == "ClientSecret" } else if last_seg.ident == "ClientSecret"
|| last_seg.ident == "DeviceId" || last_seg.ident == "DeviceId"
|| last_seg.ident == "DeviceKeyId" || last_seg.ident == "DeviceKeyId"
@ -200,7 +202,6 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
|| last_seg.ident == "MxcUri" || last_seg.ident == "MxcUri"
|| last_seg.ident == "ServerName" || last_seg.ident == "ServerName"
|| last_seg.ident == "SessionId" || last_seg.ident == "SessionId"
|| last_seg.ident == "RawJsonValue"
|| last_seg.ident == "RoomAliasId" || last_seg.ident == "RoomAliasId"
|| last_seg.ident == "RoomId" || last_seg.ident == "RoomId"
|| last_seg.ident == "RoomOrAliasId" || last_seg.ident == "RoomOrAliasId"
@ -210,8 +211,8 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
|| last_seg.ident == "TransactionId" || last_seg.ident == "TransactionId"
|| last_seg.ident == "UserId" || last_seg.ident == "UserId"
{ {
// The identifiers that need to be boxed `Box<T>` since they are DST's. let ident = format_ident!("Owned{}", last_seg.ident);
Some(parse_quote! { ::std::boxed::Box<#path> }) Some(parse_quote! { #ruma_common::#ident })
} else { } else {
None None
} }
@ -219,7 +220,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
// &[T] -> Vec<T> // &[T] -> Vec<T>
Type::Slice(TypeSlice { elem, .. }) => { Type::Slice(TypeSlice { elem, .. }) => {
// Recursively strip the lifetimes of the slice's elements. // Recursively strip the lifetimes of the slice's elements.
strip_lifetimes(&mut *elem); strip_lifetimes(&mut *elem, ruma_common);
Some(parse_quote! { Vec<#elem> }) Some(parse_quote! { Vec<#elem> })
} }
_ => None, _ => None,
@ -229,7 +230,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
Some(ty) => ty, Some(ty) => ty,
None => { None => {
// Strip lifetimes of `elem`. // Strip lifetimes of `elem`.
strip_lifetimes(elem); strip_lifetimes(elem, ruma_common);
// Replace reference with `elem`. // Replace reference with `elem`.
(**elem).clone() (**elem).clone()
} }
@ -240,7 +241,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
Type::Tuple(syn::TypeTuple { elems, .. }) => { Type::Tuple(syn::TypeTuple { elems, .. }) => {
let mut has_lifetime = false; let mut has_lifetime = false;
for elem in elems { for elem in elems {
if strip_lifetimes(elem) { if strip_lifetimes(elem, ruma_common) {
has_lifetime = true; has_lifetime = true;
} }
} }