json-ld 0.22.0

JSON-LD implementation
Documentation
//! JSON-LD expansion algorithms.
//!
//! See: <https://www.w3.org/TR/json-ld-api/#expansion-algorithms>
use crate::{
	JsonLdBacktraceBuilder, JsonLdDocument, JsonLdExpandedDocument, JsonLdIndexedObject,
	JsonLdLocatedError, JsonLdObject, JsonLdSourceFileRef, algorithms::AsyncJsonLdEnvironment,
	context::RawJsonLdProcessedContext,
};

mod array;
mod document;
mod element;
mod environment;
mod expanded;
mod literal;
mod node;
mod options;
mod value;

use element::*;
use environment::*;
use expanded::*;
use literal::*;
use node::node_id_of_term;
pub use options::*;

/// Low-level expansion trait.
///
/// This provides direct access to the expansion algorithm with full control
/// over the active context and options. For the high-level processor entry
/// point, use [`JsonLdProcessor::expand_with`](crate::JsonLdProcessor::expand_with).
pub trait JsonLdExpand {
	/// Expand this document with the default expansion options.
	#[allow(async_fn_in_trait)]
	async fn expand(
		&self,
		env: impl AsyncJsonLdEnvironment,
	) -> Result<JsonLdExpandedDocument, JsonLdLocatedError>;

	/// Expand this document with the given expansion options and active
	/// context.
	#[allow(async_fn_in_trait)]
	async fn expand_with(
		&self,
		env: impl AsyncJsonLdEnvironment,
		active_context: &RawJsonLdProcessedContext,
		options: JsonLdExpansionOptions,
	) -> Result<JsonLdExpandedDocument, JsonLdLocatedError>;
}

impl JsonLdExpand for JsonLdDocument {
	async fn expand(
		&self,
		env: impl AsyncJsonLdEnvironment,
	) -> Result<JsonLdExpandedDocument, JsonLdLocatedError> {
		let active_context = RawJsonLdProcessedContext::new(self.url().map(ToOwned::to_owned));
		self.expand_with(env, &active_context, JsonLdExpansionOptions::default())
			.await
	}

	async fn expand_with(
		&self,
		env: impl AsyncJsonLdEnvironment,
		active_context: &RawJsonLdProcessedContext,
		options: JsonLdExpansionOptions,
	) -> Result<JsonLdExpandedDocument, JsonLdLocatedError> {
		let loc = JsonLdBacktraceBuilder::new().file(JsonLdSourceFileRef::original(self.url()));
		Expander {
			base_url: self.url(),
			options,
			active_context,
			active_property: None,
		}
		.expand_document(&env, self.document(), loc)
		.await
	}
}

fn filter_top_level_item(item: &JsonLdIndexedObject) -> bool {
	// Remove dangling values.
	!matches!(item.inner(), JsonLdObject::Value(_))
}