Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure encoding/decoding function return Send futures. #3

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/codecs/iri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use did::*;
mod data;
pub use data::*;

pub trait IriCodec {
pub trait IriCodec: Send + Sync {
fn encode(&self, suffix: &str) -> Result<Vec<CborValue>, EncodeError>;

fn decode(&self, array: &[CborValue]) -> Result<String, DecodeError>;
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use xsd_date::*;
mod xsd_date_time;
pub use xsd_date_time::*;

pub trait TypeCodec {
pub trait TypeCodec: Send + Sync {
fn encode(
&self,
state: &TransformerState,
Expand Down
41 changes: 30 additions & 11 deletions src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use iref::{IriBuf, IriRef, IriRefBuf};

/// Decoding options.
#[derive(Debug, Default)]
pub struct DecodeOptions<'a> {
pub struct DecodeOptions {
// /// Map associating JSON-LD context URLs to CBOR-LD (integer) identifiers.
// pub context_map: IdMap,
/// Datatype codecs.
pub codecs: Codecs,

/// Tables.
pub default_tables: Cow<'a, Tables>,
pub default_tables: Cow<'static, Tables>,
}

/// Decodes a CBOR-LD document using the given JSON-LD context loader and the
Expand All @@ -36,7 +36,7 @@ pub async fn decode(
pub async fn decode_with(
cbor_ld_document: &CborValue,
loader: impl json_ld::Loader,
options: DecodeOptions<'_>,
options: DecodeOptions,
) -> Result<JsonValue, DecodeError> {
match cbor_ld_document {
CborValue::Tag(tag, value) => {
Expand Down Expand Up @@ -79,24 +79,24 @@ pub async fn decode_from_bytes(
pub async fn decode_from_bytes_with(
bytes: &[u8],
loader: impl json_ld::Loader,
options: DecodeOptions<'_>,
options: DecodeOptions,
) -> Result<JsonValue, DecodeError> {
let cbor_ld_document = ciborium::from_reader(bytes)?;
decode_with(&cbor_ld_document, loader, options).await
}

/// CBOR-LD decoder.
pub struct Decoder<'a, L> {
pub struct Decoder<L> {
loader: L,
state: TransformerState<'a>,
state: TransformerState,
}

impl<'a, L> Decoder<'a, L> {
impl<L> Decoder<L> {
pub fn new(
loader: L,
// application_context_map: IdMap,
codecs: Codecs,
tables: Cow<'a, Tables>,
tables: Cow<'static, Tables>,
) -> Self {
Self {
loader,
Expand All @@ -108,7 +108,7 @@ impl<'a, L> Decoder<'a, L> {
}
}

impl<'a, L> Decoder<'a, L>
impl<L> Decoder<L>
where
L: json_ld::Loader,
{
Expand All @@ -126,7 +126,7 @@ where
}
}

impl<'t, L> Transformer<'t> for Decoder<'t, L>
impl<L> Transformer for Decoder<L>
where
L: json_ld::Loader,
{
Expand Down Expand Up @@ -210,7 +210,7 @@ where
.map(|v| JsonValue::String(v.into()))
}

fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'t>, &mut Self::Loader) {
fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader) {
(&mut self.state, &mut self.loader)
}

Expand Down Expand Up @@ -270,3 +270,22 @@ where
}
}
}

#[cfg(test)]
mod tests {
use std::future::Future;

use super::{decode, decode_from_bytes};

fn assert_send(_: impl Send + Future) {}

#[test]
fn decode_is_send() {
assert_send(decode(&ciborium::Value::Null, json_ld::NoLoader))
}

#[test]
fn decode_from_bytes_is_send() {
assert_send(decode_from_bytes(b"", json_ld::NoLoader))
}
}
44 changes: 33 additions & 11 deletions src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub use error::*;

/// Encoding options.
#[derive(Debug, Default)]
pub struct EncodeOptions<'a> {
pub struct EncodeOptions {
/// Compression mode.
pub compression_mode: CompressionMode,

/// Default compression tables.
pub default_table: Cow<'a, Tables>,
pub default_table: Cow<'static, Tables>,

// /// Map associating JSON-LD context URLs to CBOR-LD (integer) identifiers.
// pub context_map: IdMap,
Expand All @@ -38,7 +38,7 @@ pub async fn encode(
pub async fn encode_with(
json_ld_document: &json_ld::syntax::Value,
loader: impl json_ld::Loader,
options: EncodeOptions<'_>,
options: EncodeOptions,
) -> Result<CborValue, EncodeError> {
let cbor_value = match options.compression_mode {
CompressionMode::Uncompressed => {
Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn encode_to_bytes(
pub async fn encode_to_bytes_with(
json_ld_document: &json_ld::syntax::Value,
loader: impl json_ld::Loader,
options: EncodeOptions<'_>,
options: EncodeOptions,
) -> Result<Vec<u8>, EncodeError> {
encode_with(json_ld_document, loader, options)
.await
Expand All @@ -90,21 +90,21 @@ pub fn cbor_into_bytes(cbor: CborValue) -> Vec<u8> {
bytes
}

pub struct Encoder<'a, L> {
pub struct Encoder<L> {
loader: L,
state: TransformerState<'a>,
state: TransformerState,
}

impl<'a, L> Encoder<'a, L> {
pub fn new(loader: L, codecs: Codecs, tables: Cow<'a, Tables>) -> Self {
impl<L> Encoder<L> {
pub fn new(loader: L, codecs: Codecs, tables: Cow<'static, Tables>) -> Self {
Self {
loader,
state: TransformerState::new(codecs, tables),
}
}
}

impl<'a, L> Encoder<'a, L>
impl<L> Encoder<L>
where
L: json_ld::Loader,
{
Expand All @@ -123,7 +123,7 @@ where
}
}

impl<'a, L> Transformer<'a> for Encoder<'a, L>
impl<L> Transformer for Encoder<L>
where
L: json_ld::Loader,
{
Expand Down Expand Up @@ -201,7 +201,7 @@ where
self.encode_vocab_term(active_context, value)
}

fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'a>, &mut Self::Loader) {
fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader) {
(&mut self.state, &mut self.loader)
}

Expand Down Expand Up @@ -257,3 +257,25 @@ where
}
}
}

#[cfg(test)]
mod tests {
use std::future::Future;

use super::{encode, encode_to_bytes};

fn assert_send(_: impl Send + Future) {}

#[test]
fn encode_is_send() {
assert_send(encode(&json_ld::syntax::Value::Null, json_ld::NoLoader))
}

#[test]
fn encode_to_bytes_is_send() {
assert_send(encode_to_bytes(
&json_ld::syntax::Value::Null,
json_ld::NoLoader,
))
}
}
12 changes: 6 additions & 6 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub struct ExpectedObject;

pub struct InvalidTypeKind;

pub trait Transformer<'t> {
pub trait Transformer {
type Input: TransformedValue<Object = Self::InputObject>;
type Output: TransformedValue<Object = Self::OutputObject>;

Expand Down Expand Up @@ -222,7 +222,7 @@ pub trait Transformer<'t> {
value: &Self::Input,
) -> Result<Self::Output, Self::Error>;

fn state_and_loader_mut(&mut self) -> (&mut TransformerState<'t>, &mut Self::Loader);
fn state_and_loader_mut(&mut self) -> (&mut TransformerState, &mut Self::Loader);

#[allow(async_fn_in_trait)]
async fn process_global_context<'c>(
Expand Down Expand Up @@ -517,18 +517,18 @@ fn is_alias_with_def(key: &str, def: Option<TermDefinitionRef>, keyword: Keyword
})
}

pub struct TransformerState<'a> {
pub struct TransformerState {
// pub context_map: IdMap,
pub allocator: IdAllocator,
pub codecs: Codecs,
pub tables: Cow<'a, Tables>,
pub tables: Cow<'static, Tables>,
}

impl<'a> TransformerState<'a> {
impl TransformerState {
pub fn new(
// context_map: IdMap,
codecs: Codecs,
tables: Cow<'a, Tables>,
tables: Cow<'static, Tables>,
) -> Self {
Self {
// context_map,
Expand Down