1
Fork 0

astreams: more cursed

This commit is contained in:
Steffo 2025-01-16 06:39:42 +01:00
parent 516172d6f5
commit 2bae6e0025
Signed by: steffo
GPG key ID: 6B8E18743E7E1F86
4 changed files with 171 additions and 1 deletions

View file

@ -482,6 +482,48 @@ impl LinkedData for Node {
Ok(())
}
fn ld_get_child(&self, id: &Id) -> ResultGetOne<&Self> {
let property = self.properties.get_any(id)?;
let node = match property.as_node() {
None => return Some(Err(anyhow!("Couldn't process property as JSON-LD node"))),
Some(value) => value,
};
Some(Ok(node))
}
fn ld_into_child_mut(&mut self, id: &Id) -> ResultGetOne<&mut Self> {
// TODO: Replace with a get_mut or similar when available
let node = self.properties_mut()
.iter_mut()
.filter(|(cid, _)| cid == &id)
.flat_map(|(_, prop)| prop
.iter_mut()
.map(|obj| obj
.as_node_mut()
.ok_or(anyhow!("Couldn't process property as JSON-LD node"))
)
)
.next();
node
}
fn ld_set_child(&mut self, id: Id, value: Self) -> ResultSetOne {
let boxed_node = Box::new(value);
let object: Object = Object::Node(boxed_node);
let indexed_object: Indexed<Object> = Indexed::from(object);
let prop_objects: Multiset<Indexed<Object>> = Multiset::singleton(indexed_object);
self.properties.set(id, prop_objects);
Ok(())
}
fn ld_get_children(&self, id: &Id) -> ResultGetMany<&Self> {
let properties = self.properties.get(id);

View file

@ -57,6 +57,10 @@ pub trait LinkedData: Sized
fn ld_set_strings(&mut self, id: Id, values: Vec<String>) -> ResultSetMany;
fn ld_set_langstrings(&mut self, id: Id, values: Vec<LangString>) -> ResultSetMany;
fn ld_get_child(&self, id: &Id) -> ResultGetOne<&Self>;
fn ld_into_child_mut(&mut self, id: &Id) -> ResultGetOne<&mut Self>;
fn ld_set_child(&self, id: Id, value: Self) -> ResultSetOne;
fn ld_get_children(&self, id: &Id) -> ResultGetMany<&Self>;
fn ld_into_children_mut(&mut self, id: &Id) -> ResultGetMany<&mut Self>;
fn ld_set_children(&mut self, id: Id, values: Vec<Self>) -> ResultSetMany;

View file

@ -13,6 +13,7 @@
use static_iref::iri;
use crate::{iri_id, vocab};
use crate::linkeddata::LangString;
use chrono::TimeDelta;
vocab!(
@ -20,6 +21,12 @@ vocab!(
ref ObjectRef,
mut ObjectMut,
one [
{
"https://www.w3.org/ns/activitystreams#name",
-> LangString,
get_name @ ld_get_langstring,
set_name @ ld_set_langstring,
},
{
"https://www.w3.org/ns/activitystreams#duration",
-> TimeDelta,
@ -27,13 +34,60 @@ vocab!(
set_timedelta @ ld_set_timedelta,
}
],
many [],
many [
],
child [
],
children [
{
"https://www.w3.org/ns/activitystreams#attachment",
get_attachments,
into_attachments_mut,
set_attachments,
},
{
"https://www.w3.org/ns/activitystreams#attributedTo",
get_attributed_to,
into_attributed_to_mut,
set_attributed_to,
},
{
"https://www.w3.org/ns/activitystreams#audience",
get_audience,
into_audience_mut,
set_audience,
},
{
"https://www.w3.org/ns/activitystreams#content",
get_content,
into_content_mut,
set_content,
},
{
"https://www.w3.org/ns/activitystreams#context",
get_context,
into_context_mut,
set_context,
},
{
"https://www.w3.org/ns/activitystreams#bcc",
get_bcc,
into_bcc_mut,
set_bcc,
},
{
"https://www.w3.org/ns/activitystreams#bto",
get_bto,
into_bto_mut,
set_bto,
},
{
"https://www.w3.org/ns/activitystreams#cc",
get_cc,
into_cc_mut,
set_cc,
}
],
);

View file

@ -49,6 +49,29 @@ macro_rules! vocab {
}
),*
],
)?
$(
child [
$(
{
$child_iri:literal,
$( #[ $child_get_meta:meta ] )*
$child_get_name:ident,
$( #[ $child_into_meta:meta ] )*
$child_into_name:ident,
$( #[ $child_set_meta:meta ] )*
$child_set_name:ident,
}
),*
],
)?
$(
children [
$(
{
@ -181,6 +204,53 @@ macro_rules! vocab {
}
)?
$(
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
{
$(
$( #[ $children_get_meta ] )*
pub fn $children_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<&'b B> {
self.backend.ld_get_child(
&iri_id!( $children_iri )
)
}
),*
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
{
$(
$( #[ $child_get_meta ] )*
pub fn $child_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<&'b B> {
self.backend.ld_get_child(
&iri_id!( $child_iri )
)
}
),*
$(
$( #[ $child_into_meta ] )*
pub fn $child_into_name(self) -> $crate::linkeddata::ResultGetOne<&'b mut B> {
self.backend.ld_into_child_mut(
&iri_id!( $child_iri )
)
}
),*
$(
$( #[ $child_into_meta ] )*
pub fn $child_set_name(&'b mut self, values: Vec<B>) -> $crate::linkeddata::ResultSetOne {
self.backend.ld_set_child(
iri_id!( $child_iri ),
values
)
}
),*
}
)?
$(
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData