#[response]api only.Expand description
Generates OutgoingResponse and IncomingResponse implementations.
The OutgoingResponse impl is feature-gated behind cfg(feature = "server").
The IncomingResponse impl is feature-gated behind cfg(feature = "client").
The Content-Type header of the OutgoingResponse defaults to application/json, except
if the raw_body attribute is set on a field, in which case it defaults to
application/octet-stream.
By default, the type this macro is used on gets a #[non_exhaustive] attribute. This
behavior can be controlled by setting the ruma_unstable_exhaustive_types compile-time
cfg setting as --cfg=ruma_unstable_exhaustive_types using RUSTFLAGS or
.cargo/config.toml (under [build] -> rustflags = ["..."]). When that setting is
activated, the attribute is not applied so the type is exhaustive.
§Container Attributes
#[response(error = ERROR_TYPE)]: Override theEndpointErrorassociated type of theIncomingResponseimplementation. The default error type isMatrixError.#[response(status = HTTP_STATUS)]: Override the status code ofOutgoingResponse.HTTP_STATUSmust be a status code constant fromhttp::StatusCode, e.g.IM_A_TEAPOT. The default status code is200 OK;
§Field Attributes
To declare which part of the response a field belongs to:
#[ruma_api(header = HEADER_NAME)]: Fields with this attribute will be treated as HTTP headers on the response.HEADER_NAMEmust implementTryInto<http::header::HeaderName>, this is usually a constant fromhttp::header. The value of the field must implementToStringandFromStr, this is usually aString. During deserialization of the response, if the field is anOptionand parsing the header fails, the error will be ignored and the value will beNone.- No attribute: Fields without an attribute are part of the body. They can use
#[serde]attributes to customize (de)serialization. #[ruma_api(body)]: Use this if multiple endpoints should share a response body type, or the response body is better expressed as anenumrather than astruct. The value of the field will be used as the JSON body (rather than being a field in the response body object).#[ruma_api(raw_body)]: Likebodyin that the field annotated with it represents the entire response body, but this attribute is for endpoints where the body can be anything, not just JSON. The field type must beVec<u8>.
§Examples
pub mod do_a_thing {
use ruma_common::{api::response, OwnedRoomId};
// metadata! { ... };
// #[request]
// pub struct Request { ... }
#[response(status = IM_A_TEAPOT)]
pub struct Response {
#[serde(skip_serializing_if = "Option::is_none")]
pub foo: Option<String>,
}
}
pub mod download_file {
use http::header::CONTENT_TYPE;
use ruma_common::api::response;
// metadata! { ... };
// #[request]
// pub struct Request { ... }
#[response]
pub struct Response {
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: String,
#[ruma_api(raw_body)]
pub file: Vec<u8>,
}
}⚠ If this is the only documentation you see, please navigate to the docs for
ruma_common::api::response, where actual documentation can be found.