#[restcrab]
Expand description
The restcrab
attribute macro can be used on traits.
It generates a trait <TaritName>Crab
for the original trait and a struct <TraitName>Client
which implements the trait <TaritName>Crab
.
The restcrab
attribute macro takes a parameter crab
which defined the backend to use and an optional parameter
attributes
which contains attributes to add to the generated trait and struct.
Add attributes
Attributes can be added to the generated trait like this.
#[restcrab(crab = "Reqwest", attributes(crab(cfg(not(feature = "some-feature")))))]
trait Service {}
This adds the attribute #[cfg(not(feature = "some-feature"))]
to the generated trait.
Attributes can be added to the generated struct like this.
#[restcrab(
crab = "Reqwest",
attributes(client(cfg(not(feature = "some-feature"))))
)]
trait Service {}
This adds the attribute #[cfg(not(feature = "some-feature"))]
to the generated struct.
Select http method
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET")]
fn method();
}
Select uri
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", uri = "/url/path/to/call")]
fn method();
}
Without the uri
parameter the method name is used as uri
.
Add parameters to request url
#[derive(Serialize)]
struct Request {}
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", uri = "/url/{name}")]
fn method(#[parameter] name: &str);
}
Add return type
#[derive(Deserialize)]
struct Response {}
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET")]
fn method() -> Response;
}
Add static headers to request
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", header("key", "value"), header("key2", "value2"))]
fn method();
}
The header
field can be added multiple times to the attribute.
Add static query parameters to request
#[derive(Serialize)]
struct Request {}
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", query("key", "value"))]
fn method();
}
Add static body to request
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", body = "body")]
fn method();
}
Add dynamic headers to request
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", header("key", "value"))]
fn method(#[headers] headers: HashMap<String, String>);
}
Can be combined with static headers.
Add dynamic query parameters to request
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET", query("key", "value"))]
fn method(#[queries] headers: HashMap<String, String>);
}
Can be combined with static query parameters.
Add dynamic body to request
#[derive(Serialize)]
struct Request {}
#[restcrab(crab = "Reqwest")]
trait Service {
#[restcrab(method = "GET")]
fn method(#[body] body: Request);
}