aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/stream.rs106
-rw-r--r--src/stream/streamitem.rs46
-rw-r--r--src/stream/verb.rs80
3 files changed, 136 insertions, 96 deletions
diff --git a/src/stream.rs b/src/stream.rs
index 475c886..8f543cc 100644
--- a/src/stream.rs
+++ b/src/stream.rs
@@ -11,7 +11,16 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
-use serde::Deserialize;
+mod streamitem;
+mod verb;
+
+pub use streamitem::{
+ StreamItem,
+ StreamItemEncoding,
+ StreamItemType,
+};
+pub use verb::Verb;
+
use std::{
error::Error,
result::Result
@@ -22,79 +31,6 @@ pub struct Stream {
pub items: Vec<StreamItem>,
}
-#[derive(Debug, Deserialize, PartialEq)]
-enum StreamItemType {
- #[serde(rename="activity")]
- Activity,
-}
-
-#[derive(Debug, Deserialize, PartialEq)]
-enum StreamItemEncoding {
- #[serde(rename="zot")]
- Zot,
-}
-
-#[derive(Debug, PartialEq)]
-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")))
- }
- }
-}
-
-#[derive(Debug, Deserialize, PartialEq)]
-pub struct StreamItem {
- #[serde(rename="type")]
- item_type: StreamItemType,
- encoding: StreamItemEncoding,
-
- verb: Verb,
-
- pub title: String,
- pub summary: String,
- pub body: String,
-}
-
-impl StreamItem {
- pub fn is_post(&self) -> bool {
- self.verb == Verb::Post
- }
-}
-
impl Stream {
pub fn from_json(json: &str) -> Result<Self, Box<dyn Error + 'static>> {
let items: Vec<StreamItem> = serde_json::from_str(&json)?;
@@ -206,26 +142,4 @@ mod test {
assert_eq!("The summary of the post", &item.summary);
assert_eq!("The body of the post", &item.body);
}
-
- #[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);
- }
-
- }
}
diff --git a/src/stream/streamitem.rs b/src/stream/streamitem.rs
new file mode 100644
index 0000000..343ed28
--- /dev/null
+++ b/src/stream/streamitem.rs
@@ -0,0 +1,46 @@
+/**
+ * Representation of a stream item, as received from the zot api streams.
+ *
+ * SPDX-FileCopyrightText: 2023 Eilertsens Kodeknekkeri
+ * SPDX-FileCopyrightText: 2023 Harald Eilertsen <haraldei@anduin.net>
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+use super::verb::Verb;
+
+use serde::Deserialize;
+
+#[derive(Debug, Deserialize, PartialEq)]
+pub enum StreamItemType {
+ #[serde(rename="activity")]
+ Activity,
+}
+
+#[derive(Debug, Deserialize, PartialEq)]
+pub enum StreamItemEncoding {
+ #[serde(rename="zot")]
+ Zot,
+}
+
+/**
+ * Represents an item as returned by the stream API's.
+ */
+#[derive(Debug, Deserialize, PartialEq)]
+pub struct StreamItem {
+ #[serde(rename="type")]
+ pub item_type: StreamItemType,
+ pub encoding: StreamItemEncoding,
+
+ pub verb: Verb,
+
+ pub title: String,
+ pub summary: String,
+ pub body: String,
+}
+
+impl StreamItem {
+ pub fn is_post(&self) -> bool {
+ self.verb == Verb::Post
+ }
+}
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);
+ }
+
+ }
+}