fix(apb): correct node order when iterating

use a VecDeque under the hood to be able to pop_front()
This commit is contained in:
əlemi 2024-04-23 20:58:37 +02:00
parent 4b4d52ef6f
commit ba07b2cb9d
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 8 additions and 8 deletions

View file

@ -1,7 +1,7 @@
/// ActivityPub object node, representing either nothing, something, a link to something or /// ActivityPub object node, representing either nothing, something, a link to something or
/// multiple things /// multiple things
pub enum Node<T : super::Base> { pub enum Node<T : super::Base> {
Array(Vec<T>), // TODO would be cool to make it Box<[T]> so that Node is just a ptr Array(std::collections::VecDeque<T>), // TODO would be cool to make it Box<[T]> so that Node is just a ptr
Object(Box<T>), Object(Box<T>),
Link(Box<dyn super::Link + Sync + Send>), // TODO feature flag to toggle these maybe? Link(Box<dyn super::Link + Sync + Send>), // TODO feature flag to toggle these maybe?
Empty, Empty,
@ -23,7 +23,7 @@ impl<T : super::Base + Clone> Iterator for Node<T> {
let x = match self { let x = match self {
Self::Empty => return None, Self::Empty => return None,
Self::Link(_) => return None, Self::Link(_) => return None,
Self::Array(arr) => return arr.pop(), // TODO weird that we iter in reverse Self::Array(arr) => return arr.pop_front(), // TODO weird that we iter in reverse
Self::Object(x) => *x.clone(), // TODO needed because next() on object can't get value without owning Self::Object(x) => *x.clone(), // TODO needed because next() on object can't get value without owning
}; };
*self = Self::Empty; *self = Self::Empty;
@ -37,7 +37,7 @@ impl<T : super::Base> Node<T> {
match self { match self {
Node::Empty | Node::Link(_) => None, Node::Empty | Node::Link(_) => None,
Node::Object(x) => Some(x), Node::Object(x) => Some(x),
Node::Array(v) => v.last(), // TODO so it's coherent with next(), still weird tho! Node::Array(v) => v.get(0),
} }
} }
@ -46,7 +46,7 @@ impl<T : super::Base> Node<T> {
match self { match self {
Node::Empty | Node::Link(_) => None, Node::Empty | Node::Link(_) => None,
Node::Object(x) => Some(*x), Node::Object(x) => Some(*x),
Node::Array(mut v) => v.pop(), // TODO so it's coherent with next(), still weird tho! Node::Array(mut v) => v.pop_front(),
} }
} }
@ -124,7 +124,7 @@ impl Node<serde_json::Value> {
} }
pub fn array(values: Vec<serde_json::Value>) -> Self { pub fn array(values: Vec<serde_json::Value>) -> Self {
Node::Array(values) Node::Array(values.into())
} }
#[cfg(feature = "fetch")] #[cfg(feature = "fetch")]
@ -165,7 +165,7 @@ impl From<serde_json::Value> for Node<serde_json::Value> {
fn from(value: serde_json::Value) -> Self { fn from(value: serde_json::Value) -> Self {
match value { match value {
serde_json::Value::String(uri) => Node::Link(Box::new(uri)), serde_json::Value::String(uri) => Node::Link(Box::new(uri)),
serde_json::Value::Array(arr) => Node::Array(arr), serde_json::Value::Array(arr) => Node::Array(arr.into()),
serde_json::Value::Object(_) => match value.get("href") { serde_json::Value::Object(_) => match value.get("href") {
None => Node::Object(Box::new(value)), None => Node::Object(Box::new(value)),
Some(_) => Node::Link(Box::new(value)), Some(_) => Node::Link(Box::new(value)),

View file

@ -50,7 +50,7 @@ pub async fn view(
Ok(JsonLD( Ok(JsonLD(
object.object.ap() object.object.ap()
.set_replies(apb::Node::object(replies)) .set_replies(apb::Node::object(replies))
.set_attachment(apb::Node::Array(attachments)) .set_attachment(apb::Node::array(attachments))
.ld_context() .ld_context()
)) ))
} }

View file

@ -237,7 +237,7 @@ impl Context {
.set_collection_type(Some(apb::CollectionType::OrderedCollectionPage)) .set_collection_type(Some(apb::CollectionType::OrderedCollectionPage))
.set_part_of(apb::Node::link(id.replace("/page", ""))) .set_part_of(apb::Node::link(id.replace("/page", "")))
.set_next(apb::Node::link(format!("{id}?offset={}", offset+limit))) .set_next(apb::Node::link(format!("{id}?offset={}", offset+limit)))
.set_ordered_items(apb::Node::Array(items)) .set_ordered_items(apb::Node::array(items))
} }
pub async fn dispatch(&self, uid: &str, activity_targets: Vec<String>, aid: &str, oid: Option<&str>) -> crate::Result<()> { pub async fn dispatch(&self, uid: &str, activity_targets: Vec<String>, aid: &str, oid: Option<&str>) -> crate::Result<()> {