astreams
: Fix some remaining issues
This commit is contained in:
parent
302241e03a
commit
fe8d345eff
5 changed files with 72 additions and 55 deletions
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{anyhow, Context, Error};
|
||||
use anyhow::{anyhow, bail, Context, Error};
|
||||
use chrono::{DateTime, FixedOffset, Local, TimeDelta};
|
||||
use iref::IriBuf;
|
||||
use json_ld::{Id, Indexed, Node, Object, ValidId};
|
||||
|
@ -12,8 +12,7 @@ use mediatype::MediaType;
|
|||
use super::{LangDir, LangString, HasLinkedDataInterface, ResultGetMany, ResultGetOne, ResultSetMany, ResultSetOne};
|
||||
|
||||
impl HasLinkedDataInterface for Node {
|
||||
fn ld_has_type(&self, id: &str) -> bool
|
||||
{
|
||||
fn ld_has_type(&self, id: &str) -> bool {
|
||||
self.has_type(
|
||||
&Id::from_string(
|
||||
id.to_string()
|
||||
|
@ -21,6 +20,12 @@ impl HasLinkedDataInterface for Node {
|
|||
)
|
||||
}
|
||||
|
||||
fn ld_set_type(&mut self, id: &str) {
|
||||
let id = Id::from_string(id.to_string());
|
||||
let types = self.types_mut_or_insert(Vec::new());
|
||||
types.push(id);
|
||||
}
|
||||
|
||||
fn ld_get_string(&self, id: &str) -> ResultGetOne<String> {
|
||||
let id = Id::from_string(id.to_string());
|
||||
|
||||
|
@ -170,8 +175,8 @@ impl HasLinkedDataInterface for Node {
|
|||
Some(number) => number
|
||||
};
|
||||
|
||||
let r#f32 = match number.as_f32_lossy() {
|
||||
None => return Some(Err(anyhow!("Couldn't losslessly convert JSON-LD number to u64"))),
|
||||
let r#f32 = match number.as_f32_lossless() {
|
||||
None => return Some(Err(anyhow!("Couldn't losslessly convert JSON-LD number to f32"))),
|
||||
Some(r#f32) => r#f32,
|
||||
};
|
||||
|
||||
|
@ -439,7 +444,10 @@ impl HasLinkedDataInterface for Node {
|
|||
fn ld_set_f32(&mut self, id: &str, value: f32) -> ResultSetOne {
|
||||
let id = Id::from_string(id.to_string());
|
||||
|
||||
let number = SynNumber::from(value);
|
||||
let number = match SynNumber::try_from(value) {
|
||||
Ok(number) => number,
|
||||
Err(_) => bail!("Couldn't process f32 into a JSON number, probably because the number is NaN or Infinite"),
|
||||
};
|
||||
|
||||
let literal = Literal::Number(number);
|
||||
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use chrono::{DateTime, Local, TimeDelta};
|
||||
use json_ld::syntax::{LangTagBuf, NumberBuf};
|
||||
use json_ld::syntax::{LangTagBuf};
|
||||
use mediatype::MediaType;
|
||||
use anyhow::{anyhow, Result as AResult};
|
||||
use json_ld::{Direction, Id, Indexed, Object, Value};
|
||||
use json_ld::object::Literal;
|
||||
use json_ld::object::node::Multiset;
|
||||
use anyhow::{Result as AResult};
|
||||
use json_ld::Direction;
|
||||
|
||||
mod jsonld;
|
||||
|
||||
|
@ -25,6 +23,7 @@ pub type ResultSetMany = AResult<()>;
|
|||
|
||||
pub trait HasLinkedDataInterface: Sized {
|
||||
fn ld_has_type(&self, id: &str) -> bool;
|
||||
fn ld_set_type(&mut self, id: &str);
|
||||
|
||||
fn ld_get_string(&self, id: &str) -> ResultGetOne<String>;
|
||||
fn ld_get_string_id(&self, id: &str) -> ResultGetOne<String>;
|
||||
|
@ -73,21 +72,15 @@ where
|
|||
Self: IsLinkedDataWrapper,
|
||||
B: HasLinkedDataInterface + 'b,
|
||||
{
|
||||
fn from_ref(backend: &'b B) -> Self;
|
||||
fn from_ref_unchecked(backend: &'b B) -> Self;
|
||||
fn from_ref_checked(backend: &'b B) -> Option<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();
|
||||
|
||||
if !backend.ld_has_type(Self::ld_type()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let wrapper = Other::from_ref(backend);
|
||||
|
||||
Some(wrapper)
|
||||
Other::from_ref_checked(self.into_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,35 +89,21 @@ where
|
|||
Self: IsLinkedDataWrapper,
|
||||
B: HasLinkedDataInterface + 'b,
|
||||
{
|
||||
fn from_mut(backend: &'b mut B) -> Self;
|
||||
fn from_mut_unchecked(backend: &'b mut B) -> Self;
|
||||
fn from_mut_create(backend: &'b mut B) -> Self;
|
||||
fn from_mut_checked(backend: &'b mut B) -> Option<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();
|
||||
|
||||
if !backend.ld_has_type(Self::ld_type()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let wrapper = Other::from_mut(backend);
|
||||
|
||||
Some(wrapper)
|
||||
Other::from_mut_checked(self.into_mut())
|
||||
}
|
||||
|
||||
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)
|
||||
Other::from_ref_checked(self.into_mut())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -373,8 +373,8 @@ vocab! {
|
|||
{
|
||||
"https://www.w3.org/ns/activitystreams#deleted",
|
||||
-> DateTime<Local>,
|
||||
get_deleted @ ld_get_datetimelocal,
|
||||
set_deleted @ ld_set_datetimelocal,
|
||||
get_deleted @ ld_get_datetime_local,
|
||||
set_deleted @ ld_set_datetime_local,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
pub mod activitystreams_core;
|
||||
pub mod activitystreams_extended;
|
||||
pub mod mastodon;
|
||||
pub mod miajetzt;
|
||||
pub mod litepub;
|
||||
pub mod lemmy;
|
||||
mod activitystreams_extended;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! vocab {
|
||||
|
@ -129,12 +129,23 @@ macro_rules! vocab {
|
|||
impl<'b, B> $crate::linkeddata::IsLinkedDataReadWrapper<'b, B> for $vocab_ref<'b, B>
|
||||
where B: $crate::linkeddata::HasLinkedDataInterface,
|
||||
{
|
||||
fn from_ref(backend: &'b B) -> Self {
|
||||
fn from_ref_unchecked(backend: &'b B) -> Self {
|
||||
Self {
|
||||
backend
|
||||
}
|
||||
}
|
||||
|
||||
fn from_ref_checked(backend: &'b B) -> Option<Self> {
|
||||
use $crate::linkeddata::IsLinkedDataWrapper;
|
||||
|
||||
if !backend.ld_has_type(Self::ld_type()) {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Self::from_ref_unchecked(backend))
|
||||
}
|
||||
}
|
||||
|
||||
fn into_ref(self) -> &'b B {
|
||||
self.backend
|
||||
}
|
||||
|
@ -143,12 +154,31 @@ macro_rules! vocab {
|
|||
impl<'b, B> $crate::linkeddata::IsLinkedDataWriteWrapper<'b, B> for $vocab_mut<'b, B>
|
||||
where B: $crate::linkeddata::HasLinkedDataInterface,
|
||||
{
|
||||
fn from_mut(backend: &'b mut B) -> Self {
|
||||
fn from_mut_unchecked(backend: &'b mut B) -> Self {
|
||||
Self {
|
||||
backend
|
||||
}
|
||||
}
|
||||
|
||||
fn from_mut_checked(backend: &'b mut B) -> Option<Self> {
|
||||
use $crate::linkeddata::IsLinkedDataWrapper;
|
||||
|
||||
if !backend.ld_has_type(Self::ld_type()) {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Self::from_mut_unchecked(backend))
|
||||
}
|
||||
}
|
||||
|
||||
fn from_mut_create(backend: &'b mut B) -> Self {
|
||||
use $crate::linkeddata::IsLinkedDataWrapper;
|
||||
|
||||
backend.ld_set_type(Self::ld_type());
|
||||
|
||||
Self::from_mut_unchecked(backend)
|
||||
}
|
||||
|
||||
fn into_mut(self) -> &'b mut B {
|
||||
self.backend
|
||||
}
|
||||
|
@ -194,7 +224,7 @@ macro_rules! vocab {
|
|||
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<$many_type> {
|
||||
self.backend.$many_get_via($many_iri)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
}
|
||||
|
||||
impl<'b, B> $vocab_mut<'b, B>
|
||||
|
@ -205,14 +235,14 @@ macro_rules! vocab {
|
|||
pub fn $many_get_name(&'b self) -> $crate::linkeddata::ResultGetMany<$many_type> {
|
||||
self.backend.$many_get_via($many_iri)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
|
||||
$(
|
||||
$( #[ $many_set_meta ] )*
|
||||
pub fn $many_set_name(&'b mut self, values: Vec<$many_type>) -> $crate::linkeddata::ResultSetMany {
|
||||
self.backend.$many_set_via($many_iri, values)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
}
|
||||
)?
|
||||
|
||||
|
@ -236,21 +266,21 @@ macro_rules! vocab {
|
|||
pub fn $child_get_name(&'b self) -> $crate::linkeddata::ResultGetOne<&'b B> {
|
||||
self.backend.ld_get_child($child_iri)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
|
||||
$(
|
||||
$( #[ $child_into_meta ] )*
|
||||
pub fn $child_into_name(self) -> $crate::linkeddata::ResultGetOne<&'b mut B> {
|
||||
self.backend.ld_into_child_mut($child_iri)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
|
||||
$(
|
||||
$( #[ $child_into_meta ] )*
|
||||
pub fn $child_set_name(&'b mut self, values: Vec<B>) -> $crate::linkeddata::ResultSetOne {
|
||||
self.backend.ld_set_child($child_iri, values)
|
||||
pub fn $child_set_name(&'b mut self, value: B) -> $crate::linkeddata::ResultSetOne {
|
||||
self.backend.ld_set_child($child_iri, value)
|
||||
}
|
||||
),*
|
||||
)*
|
||||
}
|
||||
)?
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use json_ld::context_processing::{Process};
|
|||
use static_iref::{iri, iri_ref};
|
||||
use acrate_astreams::linkeddata::LangDir;
|
||||
use acrate_astreams::linkeddata::IsLinkedDataWriteWrapper;
|
||||
use acrate_astreams::vocabulary::activitystreams::ObjectMut;
|
||||
use acrate_astreams::vocabulary::activitystreams_core::ObjectMut;
|
||||
|
||||
macro_rules! jsonld_document {
|
||||
($modname:ident: $filename:literal) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue