Skip to content

Commit

Permalink
refactor(model): simplify activity button text (de)serialization (twi…
Browse files Browse the repository at this point in the history
…light-rs#2087)

Simplify the (de)serialization of
`gateway::presence::activity_button::ActivityButtonText`. We can remove
the custom Deserialize and Serialize implementations by taking advantage
of the fact that it has only one field, and instead using
`#[serde(transparent)]`.

Part of twilight-rs#1364.
  • Loading branch information
zeylahellyer authored Jan 28, 2023
1 parent 1754351 commit a76334a
Showing 1 changed file with 2 additions and 38 deletions.
40 changes: 2 additions & 38 deletions twilight-model/src/gateway/presence/activity_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,48 +226,12 @@ pub struct ActivityButtonLink {
/// );
/// # Ok(()) }
/// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]
pub struct ActivityButtonText {
/// Text shown on the button.
pub label: String,
}
#[derive(Debug, Deserialize)]
#[serde(field_identifier, rename_all = "snake_case")]
enum ActivityButtonTextField {
Label,
}

struct ActivityButtonTextVisitor;

impl<'de> Visitor<'de> for ActivityButtonTextVisitor {
type Value = ActivityButtonText;

fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("activity button string")
}

fn visit_string<E: DeError>(self, v: String) -> Result<Self::Value, E> {
Ok(ActivityButtonText { label: v })
}

fn visit_str<E: DeError>(self, v: &str) -> Result<Self::Value, E> {
Ok(ActivityButtonText {
label: v.to_owned(),
})
}
}

impl<'de> Deserialize<'de> for ActivityButtonText {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(ActivityButtonTextVisitor)
}
}

impl Serialize for ActivityButtonText {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.label)
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit a76334a

Please sign in to comment.