aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream/verb.rs
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2024-01-12 18:59:05 +0100
committerHarald Eilertsen <haraldei@anduin.net>2024-01-12 18:59:05 +0100
commit316c62b8ddce3315cca0a9ecf35c551e8013160e (patch)
tree2a14ab0ecf8ef18c9fdc52ca9acdade298d1b88b /src/stream/verb.rs
parentbe04fb276aa4777f5477c579fc82f1cef9b18d65 (diff)
downloadrust-zotapi-316c62b8ddce3315cca0a9ecf35c551e8013160e.tar.gz
rust-zotapi-316c62b8ddce3315cca0a9ecf35c551e8013160e.tar.bz2
rust-zotapi-316c62b8ddce3315cca0a9ecf35c551e8013160e.zip
Refactor stream API, move StreamItem and Verb to modules.
Diffstat (limited to 'src/stream/verb.rs')
-rw-r--r--src/stream/verb.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/stream/verb.rs b/src/stream/verb.rs
new file mode 100644
index 0000000..75a8dac
--- /dev/null
+++ b/src/stream/verb.rs
@@ -0,0 +1,80 @@
+/**
+ * Code to represent an activitystreams verb.
+ *
+ * SPDX-FileCopyrightText: 2023 Eilertsens Kodeknekkeri
+ * SPDX-FileCopyrightText: 2023 Harald Eilertsen <haraldei@anduin.net>
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+use serde::Deserialize;
+
+#[derive(Debug, PartialEq)]
+pub enum Verb {
+ Post,
+ Like,
+ Update,
+}
+
+impl<'de> Deserialize<'de> for Verb {
+ fn deserialize<D>(deserializer: D) -> Result<Verb, D::Error>
+ where
+ D: serde::de::Deserializer<'de>,
+ {
+ deserializer.deserialize_str(VerbVisitor)
+ }
+}
+
+struct VerbVisitor;
+
+impl<'de> serde::de::Visitor<'de> for VerbVisitor {
+ type Value = Verb;
+
+ fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+ write!(formatter, "a valid activity streams verb URI")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ match s {
+ "http://activitystrea.ms/schema/1.0/post" =>
+ Ok(Verb::Post),
+ "http://activitystrea.ms/schema/1.0/like" =>
+ Ok(Verb::Like),
+ "http://activitystrea.ms/schema/1.0/update" =>
+ Ok(Verb::Update),
+ _ =>
+ Err(E::custom(format!("unknown activity streams verb")))
+ }
+ }
+}
+
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn deserialize_activitustreams_verb_from_json() {
+ #[derive(Debug, Deserialize, PartialEq)]
+ struct VerbTest {
+ verb: Verb
+ }
+
+ let verbs = vec![
+ (Verb::Post, "post"),
+ (Verb::Like, "like"),
+ (Verb::Update, "update")
+ ];
+
+ for v in verbs {
+ let verb: VerbTest = serde_json::from_str(
+ &format!(r#"{{"verb": "http://activitystrea.ms/schema/1.0/{}"}}"#, v.1)
+ ).unwrap();
+ assert_eq!(v.0, verb.verb);
+ }
+
+ }
+}