Fix return type for deserialize_one (#93)

This commit is contained in:
Nutomic 2024-01-05 16:18:12 +01:00 committed by GitHub
parent 3efa99514c
commit ec97b44de4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -56,12 +56,12 @@ where
/// #[derive(serde::Deserialize)] /// #[derive(serde::Deserialize)]
/// struct Note { /// struct Note {
/// #[serde(deserialize_with = "deserialize_one")] /// #[serde(deserialize_with = "deserialize_one")]
/// to: Url /// to: [Url; 1]
/// } /// }
/// ///
/// let note = serde_json::from_str::<Note>(r#"{"to": ["https://example.com/u/alice"] }"#); /// let note = serde_json::from_str::<Note>(r#"{"to": ["https://example.com/u/alice"] }"#);
/// assert!(note.is_ok()); /// assert!(note.is_ok());
pub fn deserialize_one<'de, T, D>(deserializer: D) -> Result<T, D::Error> pub fn deserialize_one<'de, T, D>(deserializer: D) -> Result<[T; 1], D::Error>
where where
T: Deserialize<'de>, T: Deserialize<'de>,
D: Deserializer<'de>, D: Deserializer<'de>,
@ -75,8 +75,8 @@ where
let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?; let result: MaybeArray<T> = Deserialize::deserialize(deserializer)?;
Ok(match result { Ok(match result {
MaybeArray::Simple(value) => value, MaybeArray::Simple(value) => [value],
MaybeArray::Array([value]) => value, MaybeArray::Array([value]) => [value],
}) })
} }
@ -125,7 +125,7 @@ mod tests {
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
struct Note { struct Note {
#[serde(deserialize_with = "deserialize_one")] #[serde(deserialize_with = "deserialize_one")]
_to: Url, _to: [Url; 1],
} }
let note = serde_json::from_str::<Note>( let note = serde_json::from_str::<Note>(