astreams: continuing progress on the cursed api

This commit is contained in:
Steffo 2025-01-22 17:50:08 +01:00
parent 2bae6e0025
commit 60af998994
Signed by: steffo
GPG key ID: 6B8E18743E7E1F86
4 changed files with 154 additions and 68 deletions

View file

@ -10,9 +10,9 @@ use json_ld::object::{Any};
use json_ld::object::node::Multiset;
use json_ld::syntax::LangTagBuf;
use mediatype::MediaType;
use super::{LangDir, LangString, LinkedData, ResultGetMany, ResultGetOne, ResultSetMany, ResultSetOne};
use super::{LangDir, LangString, HasLinkedDataInterface, ResultGetMany, ResultGetOne, ResultSetMany, ResultSetOne};
impl LinkedData for Node {
impl HasLinkedDataInterface for Node {
fn ld_has_type(&self, id: &Id) -> bool {
self.has_type(id)
}

View file

@ -11,7 +11,7 @@ macro_rules! iri_id {
($iri:literal) => {
json_ld::Id::Valid(
json_ld::ValidId::Iri(
iri!($iri).to_owned()
static_iref::iri!($iri).to_owned()
)
)
};
@ -30,9 +30,7 @@ pub type ResultGetMany<T> = Vec<AResult<T>>;
pub type ResultSetOne = AResult<()>;
pub type ResultSetMany = AResult<()>;
/// Something that has IRI-indexed properties.
pub trait LinkedData: Sized
{
pub trait HasLinkedDataInterface: Sized {
fn ld_has_type(&self, id: &Id) -> bool;
fn ld_get_string(&self, id: &Id) -> ResultGetOne<String>;
@ -59,9 +57,78 @@ pub trait LinkedData: Sized
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_set_child(&mut 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;
}
pub type ResultConvert<W> = Option<W>;
pub trait IsLinkedDataReadWrapper<'b, B>
where
Self: Sized,
B: HasLinkedDataInterface + 'b,
{
fn ld_type() -> &'static Id;
fn from_ref(backend: &'b B) -> Self;
fn into_ref(self) -> &'b B;
fn try_into_read_wrapper<Other>(self) -> ResultConvert<Other>
where Other: IsLinkedDataReadWrapper<'b, B>
{
let backend = self.into_ref();
let ld_type = Self::ld_type();
if !backend.ld_has_type(ld_type) {
return None;
}
let wrapper = Other::from_ref(backend);
Some(wrapper)
}
}
pub trait IsLinkedDataWriteWrapper<'b, B>
where
Self: Sized,
B: HasLinkedDataInterface + 'b,
{
fn ld_type() -> &'static Id;
fn from_mut(backend: &'b mut B) -> Self;
fn into_mut(self) -> &'b mut B;
fn try_into_write_wrapper<Other>(self) -> ResultConvert<Other>
where Other: IsLinkedDataWriteWrapper<'b, B>
{
let backend = self.into_mut();
let ld_type = Self::ld_type();
if !backend.ld_has_type(ld_type) {
return None;
}
let wrapper = Other::from_mut(backend);
Some(wrapper)
}
fn try_into_read_wrapper<Other>(self) -> ResultConvert<Other>
where Other: IsLinkedDataReadWrapper<'b, B>
{
let backend = self.into_mut();
let ld_type = Self::ld_type();
if !backend.ld_has_type(ld_type) {
return None;
}
let wrapper = Other::from_ref(backend);
Some(wrapper)
}
}

View file

@ -11,8 +11,7 @@
//! - <https://www.w3.org/TR/activitystreams-vocabulary/>
//!
use static_iref::iri;
use crate::{iri_id, vocab};
use crate::vocab;
use crate::linkeddata::LangString;
use chrono::TimeDelta;
@ -21,12 +20,6 @@ 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,
@ -35,7 +28,12 @@ vocab!(
}
],
many [
{
"https://www.w3.org/ns/activitystreams#name",
-> LangString,
get_name @ ld_get_langstrings,
set_name @ ld_set_langstrings,
}
],
child [

View file

@ -94,7 +94,7 @@ macro_rules! vocab {
$( #[ $vocab_ref_meta ] )*
#[repr(transparent)]
pub struct $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
backend: &'b B
}
@ -102,101 +102,122 @@ macro_rules! vocab {
$( #[ $vocab_mut_meta ] )*
#[repr(transparent)]
pub struct $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
backend: &'b mut B
}
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
impl<'b, B> $crate::linkeddata::IsLinkedDataReadWrapper<'b, B> for $vocab_ref<'b, B>
where B: $crate::linkeddata::HasLinkedDataInterface,
{
pub fn new(backend: &'b B) -> Self {
#[inline(always)]
fn ld_type() -> &'static json_ld::Id {
static id: json_ld::Id = $crate::iri_id! ( $vocab_iri );
&id
}
fn from_ref(backend: &'b B) -> Self {
Self {
backend
}
}
fn into_ref(self) -> &'b B {
self.backend
}
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
impl<'b, B> $crate::linkeddata::IsLinkedDataWriteWrapper<'b, B> for $vocab_mut<'b, B>
where B: $crate::linkeddata::HasLinkedDataInterface,
{
pub fn new(backend: &'b mut B) -> Self {
fn ld_type() -> &'static json_ld::Id {
static id: json_ld::Id = $crate::iri_id! ( $vocab_iri );
&id
}
fn from_mut(backend: &'b mut B) -> Self {
Self {
backend
}
}
fn into_mut(self) -> &'b mut B {
self.backend
}
}
$(
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $one_get_meta ] )*
pub fn $one_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<$one_type> {
self.backend.$one_get_via(
&iri_id!( $one_iri )
& $crate::iri_id!( $one_iri )
)
}
),*
)*
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $one_get_meta ] )*
pub fn $one_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<$one_type> {
self.backend.$one_get_via(
&iri_id!( $one_iri )
& $crate::iri_id!( $one_iri )
)
}
),*
)*
$(
$( #[ $one_set_meta ] )*
pub fn $one_set_name(&'b mut self, value: $one_type) -> $crate::linkeddata::ResultSetOne {
self.backend.$one_set_via(
iri_id!( $one_iri ),
$crate::iri_id!( $one_iri ),
value
)
}
),*
)*
}
)?
$(
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $many_get_meta ] )*
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<&'b $many_type> {
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<$many_type> {
self.backend.$many_get_via(
&iri_id!( $many_iri )
& $crate::iri_id!( $many_iri )
)
}
),*
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $many_get_meta ] )*
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<&'b $many_type> {
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<$many_type> {
self.backend.$many_get_via(
&iri_id!( $many_iri )
& $crate::iri_id!( $many_iri )
)
}
),*
$(
$( #[ $many_set_meta ] )*
pub fn $many_set_name(&'b self, values: Vec<$many_type>) -> $crate::linkeddata::ResultSetMany {
pub fn $many_set_name(&'b mut self, values: Vec<$many_type>) -> $crate::linkeddata::ResultSetMany {
self.backend.$many_set_via(
iri_id!( $many_iri ),
$crate::iri_id!( $many_iri ),
values
)
}
@ -206,26 +227,26 @@ 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
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $child_get_meta ] )*
pub fn $child_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<&'b B> {
self.backend.ld_get_child(
&iri_id!( $child_iri )
& $crate::iri_id!( $child_iri )
)
}
)*
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $child_get_meta ] )*
pub fn $child_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<&'b B> {
self.backend.ld_get_child(
& $crate::iri_id!( $child_iri )
)
}
),*
@ -234,7 +255,7 @@ macro_rules! vocab {
$( #[ $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 )
& $crate::iri_id!( $child_iri )
)
}
),*
@ -243,7 +264,7 @@ macro_rules! vocab {
$( #[ $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 ),
$crate::iri_id!( $child_iri ),
values
)
}
@ -253,48 +274,48 @@ macro_rules! vocab {
$(
impl<'b, B> $vocab_ref<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $children_get_meta ] )*
pub fn $children_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<&'b B> {
self.backend.ld_get_children(
&iri_id!( $children_iri )
& $crate::iri_id!( $children_iri )
)
}
),*
)*
}
impl<'b, B> $vocab_mut<'b, B>
where B: $crate::linkeddata::LinkedData
where B: $crate::linkeddata::HasLinkedDataInterface
{
$(
$( #[ $children_get_meta ] )*
pub fn $children_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<&'b B> {
self.backend.ld_get_children(
&iri_id!( $children_iri )
& $crate::iri_id!( $children_iri )
)
}
),*
)*
$(
$( #[ $children_into_meta ] )*
pub fn $children_into_name(self) -> $crate::linkeddata::ResultGetMany<&'b mut B> {
self.backend.ld_into_children_mut(
&iri_id!( $children_iri )
& $crate::iri_id!( $children_iri )
)
}
),*
)*
$(
$( #[ $children_into_meta ] )*
pub fn $children_set_name(&'b mut self, values: Vec<B>) -> $crate::linkeddata::ResultSetMany {
self.backend.ld_set_children(
iri_id!( $children_iri ),
$crate::iri_id!( $children_iri ),
values
)
}
),*
)*
}
)?
};