diff --git a/apb/src/node.rs b/apb/src/node.rs index b79bd79f..53b967ff 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -3,7 +3,7 @@ pub enum Node { Array(std::collections::VecDeque), // TODO would be cool to make it Box<[T]> so that Node is just a ptr Object(Box), - Link(Box), // TODO feature flag to toggle these maybe? + Link(Box), // TODO feature flag to toggle these maybe? Empty, } diff --git a/apb/src/types/object/actor.rs b/apb/src/types/object/actor.rs index 45cf711f..ea8f37de 100644 --- a/apb/src/types/object/actor.rs +++ b/apb/src/types/object/actor.rs @@ -1,6 +1,4 @@ -use crate::Node; - -use crate::{Object, ObjectMut, PublicKey}; +use crate::{Node, Object, ObjectMut}; crate::strenum! { pub enum ActorType { @@ -13,7 +11,7 @@ crate::strenum! { } pub trait Actor : Object { - type PublicKey : PublicKey; + type PublicKey : crate::PublicKey; fn actor_type(&self) -> Option { None } fn preferred_username(&self) -> Option<&str> { None } @@ -30,7 +28,7 @@ pub trait Actor : Object { } pub trait ActorMut : ObjectMut { - type PublicKey : PublicKey; + type PublicKey : crate::PublicKey; fn set_actor_type(self, val: Option) -> Self; fn set_preferred_username(self, val: Option<&str>) -> Self; diff --git a/apb/src/types/object/mod.rs b/apb/src/types/object/mod.rs index 7abdeaa8..7241d343 100644 --- a/apb/src/types/object/mod.rs +++ b/apb/src/types/object/mod.rs @@ -7,12 +7,12 @@ pub mod place; pub mod profile; pub mod relationship; -use crate::{Base, BaseMut, Link, Node}; +use crate::{Base, BaseMut, Node}; -use actor::{Actor, ActorType}; -use document::{Document, DocumentType}; +use actor::ActorType; +use document::DocumentType; use activity::ActivityType; -use collection::{Collection, CollectionType}; +use collection::CollectionType; crate::strenum! { pub enum ObjectType { @@ -33,11 +33,12 @@ crate::strenum! { } pub trait Object : Base { - type Link : Link; - type Actor : Actor; + type Link : crate::Link; + type Actor : crate::Actor; type Object : Object; - type Collection : Collection; - type Document : Document; + type Collection : crate::Collection; + type Document : crate::Document; + type Activity : crate::Activity; fn object_type(&self) -> Option { None } fn attachment(&self) -> Node { Node::Empty } @@ -69,14 +70,19 @@ pub trait Object : Base { // TODO i really need this but it isn't part of AP! fn sensitive(&self) -> Option { None } + + fn as_activity(&self) -> Option<&Self::Activity> { None } + fn as_actor(&self) -> Option<&Self::Actor> { None } + fn as_collection(&self) -> Option<&Self::Collection> { None } + fn as_document(&self) -> Option<&Self::Document> { None } } pub trait ObjectMut : BaseMut { - type Link : Link; - type Actor : Actor; + type Link : crate::Link; + type Actor : crate::Actor; type Object : Object; - type Collection : Collection; - type Document : Document; + type Collection : crate::Collection; + type Document : crate::Document; fn set_object_type(self, val: Option) -> Self; fn set_attachment(self, val: Node) -> Self; @@ -116,7 +122,8 @@ impl Object for serde_json::Value { type Object = serde_json::Value; type Document = serde_json::Value; type Collection = serde_json::Value; - + type Activity = serde_json::Value; + crate::getter! { object_type -> type ObjectType } crate::getter! { attachment -> node ::Object } crate::getter! { attributed_to::attributedTo -> node Self::Actor } @@ -156,6 +163,34 @@ impl Object for serde_json::Value { } } } + + fn as_activity(&self) -> Option<&Self::Activity> { + match self.object_type() { + Some(ObjectType::Activity(_)) => Some(self), + _ => None, + } + } + + fn as_actor(&self) -> Option<&Self::Actor> { + match self.object_type() { + Some(ObjectType::Actor(_)) => Some(self), + _ => None, + } + } + + fn as_collection(&self) -> Option<&Self::Collection> { + match self.object_type() { + Some(ObjectType::Collection(_)) => Some(self), + _ => None, + } + } + + fn as_document(&self) -> Option<&Self::Document> { + match self.object_type() { + Some(ObjectType::Document(_)) => Some(self), + _ => None, + } + } } #[cfg(feature = "unstructured")]