我正在try 使用我的请求 struct 设置serde,以将其转换为JSON,以便将其传递给API:
#[serde_with::skip_serializing_none]
#[derive(Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct Request {
#[serde(with = "http_serde::uri")]
url: http::Uri,
http_response_body: Option<bool>,
#[serde(with = "http_serde::method")]
http_request_method: Option<http::Method>,
}
我希望有一些字段是可选的,如果它们是None
,那么我只想从序列化中省略它们.我使用serde_with
跳过None
个字段,它适用于http_response_body: Option<bool>
,但不适用于http_request_method: Option<http::Method>
.我收到以下错误:
error[E0308]: mismatched types
--> src\zyte.rs:7:10
|
7 | #[derive(Serialize, Debug, Default)]
| ^^^^^^^^^
| |
| expected struct `Method`, found enum `Option`
| arguments to this function are incorrect
|
= note: expected reference `&Method`
found reference `&'__a std::option::Option<Method>`
note: function defined here
--> C:\Users\isaac\.cargo\registry\src\github.com-1ecc6299db9ec823\http-serde-1.1.2\src\lib.rs:237:12
|
237 | pub fn serialize<S: Serializer>(method: &Method, ser: S) -> Result<S::Ok, S::Error> {
| ^^^^^^^^^
= note: this error originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-simple-scraper` due to previous error
我能做些什么才能让这件事起作用?