Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: safe Reaction construction
Fintan Halpenny committed 3 years ago
commit de8e92d48cb4810ca7323f4468504d7968dd1a02
parent 0337574750db4437cfc024b06d73864a4147e6e6
1 file changed +42 -2
modified radicle/src/cob/common.rs
@@ -36,10 +36,10 @@ pub enum ReactionError {
    InvalidReaction,
}

-
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Serialize, Deserialize)]
+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Serialize)]
#[serde(transparent)]
pub struct Reaction {
-
    pub emoji: char,
+
    emoji: char,
}

impl Reaction {
@@ -51,6 +51,46 @@ impl Reaction {
    }
}

+
impl<'de> Deserialize<'de> for Reaction {
+
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+
    where
+
        D: serde::Deserializer<'de>,
+
    {
+
        struct ReactionVisitor;
+

+
        impl<'de> serde::de::Visitor<'de> for ReactionVisitor {
+
            type Value = Reaction;
+

+
            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+
                formatter.write_str("a reaction emoji")
+
            }
+

+
            fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
+
            where
+
                E: serde::de::Error,
+
            {
+
                Reaction::new(v).map_err(|e| E::custom(e.to_string()))
+
            }
+

+
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+
            where
+
                E: serde::de::Error,
+
            {
+
                Reaction::from_str(v).map_err(|e| E::custom(e.to_string()))
+
            }
+

+
            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+
            where
+
                E: serde::de::Error,
+
            {
+
                Reaction::from_str(&v).map_err(|e| E::custom(e.to_string()))
+
            }
+
        }
+

+
        deserializer.deserialize_char(ReactionVisitor)
+
    }
+
}
+

impl FromStr for Reaction {
    type Err = ReactionError;