### Serializing RDF/XML with TokioAsyncWriterRdfXmlSerializer Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.TokioAsyncWriterRdfXmlSerializer.html An example demonstrating how to initialize the serializer, add triples, and finish the serialization process asynchronously. ```rust use oxrdf::{NamedNodeRef, TripleRef, LiteralRef}; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new().with_prefix("schema", "http://schema.org/")?.for_tokio_async_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, rdf::TYPE, NamedNodeRef::new("http://schema.org/Person")?, )).await?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://schema.org/name")?, LiteralRef::new_language_tagged_literal_unchecked("Foo Bar", "en"), )).await?; assert_eq!( b"\n\n\t\n\t\tFoo Bar\n\t\n", serializer.finish().await?.as_slice() ); ``` -------------------------------- ### Serialize RDF/XML with WriterRdfXmlSerializer Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.WriterRdfXmlSerializer.html Shows how to create a serializer with a prefix, serialize two triples, and finish to get the XML output. The output is asserted to match the expected RDF/XML string. ```rust use oxrdf::{LiteralRef, NamedNodeRef, TripleRef}; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new().with_prefix("schema", "http://schema.org/")?.for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, rdf::TYPE, NamedNodeRef::new("http://schema.org/Person")?, ))?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://schema.org/name")?, LiteralRef::new_language_tagged_literal_unchecked("Foo Bar", "en"), ))?; assert_eq!( b"\n\n\t\n\t\tFoo Bar\n\t\n", serializer.finish()?.as_slice() ); ``` -------------------------------- ### Inspect prefixes with TokioAsyncReaderRdfXmlParser Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.TokioAsyncReaderRdfXmlParser.html Shows how to use the `prefixes` method to get the current IRI prefixes during parsing. The prefix list is empty at the beginning and becomes populated after reading the first triple. ```rust use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_tokio_async_reader(file.as_bytes()); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().await.unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes ``` -------------------------------- ### Count people using RdfXmlParser::for_reader Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlParser.html Example demonstrating how to count the number of people in an RDF/XML file using the for_reader method. ```rust use oxrdf::NamedNodeRef; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let schema_person = NamedNodeRef::new("http://schema.org/Person")?; let mut count = 0; for triple in RdfXmlParser::new().for_reader(file.as_bytes()) { let triple = triple?; if triple.predicate == rdf::TYPE && triple.object == schema_person.into() { count += 1; } } assert_eq!(2, count); ``` -------------------------------- ### Count people using RdfXmlParser::for_slice Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlParser.html Example demonstrating how to count the number of people in an RDF/XML file using the for_slice method. ```rust use oxrdf::NamedNodeRef; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let schema_person = NamedNodeRef::new("http://schema.org/Person")?; let mut count = 0; for triple in RdfXmlParser::new().for_slice(file) { let triple = triple?; if triple.predicate == rdf::TYPE && triple.object == schema_person.into() { count += 1; } } assert_eq!(2, count); ``` -------------------------------- ### Access prefixes with SliceRdfXmlParser::prefixes Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Shows how to inspect the IRI prefixes known at the current parsing step. The prefix list is empty at the beginning and becomes populated after reading the first triple. The example asserts the collected prefixes match the expected tuples. ```rust use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_slice(file); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes ``` -------------------------------- ### Serialize to a Write implementation Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Demonstrates the initialization of the serializer for a writer. ```rust use oxrdf::{LiteralRef, NamedNodeRef, TripleRef}; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new().with_prefix("schema", "http://schema.org/")?.for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, rdf::TYPE, NamedNodeRef::new("http://schema.org/Person")?, ))?; ``` -------------------------------- ### ReaderRdfXmlParser::prefixes Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.ReaderRdfXmlParser.html Returns an iterator over the IRI prefixes currently defined during parsing. The iterator yields (prefix_name, prefix_value) tuples. It is empty at the start and updates as prefixes are encountered. ```APIDOC ## ReaderRdfXmlParser::prefixes ### Description Returns an iterator over the IRI prefixes currently defined during parsing. The iterator yields (prefix_name, prefix_value) tuples. It is empty at the start and updates as prefixes are encountered. ### Method prefixes ### Signature pub fn prefixes(&self) -> RdfXmlPrefixesIter<'_> ⓘ ### Parameters None ### Returns RdfXmlPrefixesIter<'_> - An iterator over (prefix_name, prefix_value) tuples. ### Example ```rust use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_reader(file.as_bytes()); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes ``` ``` -------------------------------- ### Test custom RDF namespace prefix Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Demonstrates configuring a custom prefix for the RDF namespace using the RdfXmlSerializer. ```rust #[test] fn test_custom_rdf_ns() -> Result<(), Box> { let output = RdfXmlSerializer::new() .with_prefix("rdf", "http://example.com/")? .for_writer(Vec::new()) .finish()?; assert_eq!(output, b"\n\n"); Ok(()) } ``` -------------------------------- ### Parsing RDF/XML with TokioAsyncReaderRdfXmlParser Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Demonstrates how to initialize the parser and iterate over triples asynchronously to count specific RDF types. ```rust let mut count = 0; let mut parser = RdfXmlParser::new().for_tokio_async_reader(file.as_bytes()); while let Some(triple) = parser.next().await { let triple = triple?; if triple.predicate == rdf::TYPE && triple.object == schema_person.into() { count += 1; } } assert_eq!(2, count); # Ok(()) # } ``` -------------------------------- ### Serialize with base IRI and prefixes Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Shows how to configure the serializer with a base IRI and multiple prefixes to produce relative IRIs in the output. ```rust use oxrdf::{NamedNodeRef, TripleRef}; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new() .with_base_iri("http://example.com")? .with_prefix("ex", "http://example.com/ns#")? .for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?, NamedNodeRef::new("http://example.com/ns#Person")?, ))?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://example.com/ns#parent")?, NamedNodeRef::new("http://example.com#other")?, ))?; assert_eq!( b"\n\n\t\n\t\t\n\t\n", serializer.finish()?.as_slice() ); # Result::<_,Box>::Ok(()) ``` -------------------------------- ### RdfXmlParser::new Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlParser.html Builds a new RdfXmlParser instance. ```APIDOC ## RdfXmlParser::new ### Description Builds a new `RdfXmlParser` instance. ### Method `new` ### Signature `pub fn new() -> Self` ### Parameters None. ### Returns A new `RdfXmlParser` instance. ``` -------------------------------- ### Test custom empty namespace prefix Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Shows how to set an empty prefix for a namespace and serialize triples using that configuration. ```rust #[test] fn test_custom_empty_ns() -> Result<(), Box> { let mut serializer = RdfXmlSerializer::new() .with_prefix("", "http://example.com/")? .for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com/s")?, rdf::TYPE, NamedNodeRef::new("http://example.org/o")?, ))?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com/s")?, NamedNodeRef::new("http://example.com/p")?, NamedNodeRef::new("http://example.com/o2")?, ))?; let output = serializer.finish()?; assert_eq!( String::from_utf8_lossy(&output), "\n\n\t\n\t\t

\n\t\n" ); Ok(()) } ``` -------------------------------- ### Serialize triples with with_base_iri and with_prefix Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlSerializer.html Shows how to build an RdfXmlSerializer with a base IRI and a custom prefix, then serialize two triples and assert the exact RDF/XML output. The expected output includes the xml:base attribute and the ex namespace declaration. ```rust use oxrdf::{NamedNodeRef, TripleRef}; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new() .with_base_iri("http://example.com")? .with_prefix("ex", "http://example.com/ns#")? .for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?, NamedNodeRef::new("http://example.com/ns#Person")?, ))?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://example.com/ns#parent")?, NamedNodeRef::new("http://example.com#other")?, ))?; assert_eq!( b"\n\n\t\n\t\t\n\t\n", serializer.finish()?.as_slice() ); ``` -------------------------------- ### RdfXmlSerializer::new Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlSerializer.html Creates a new RdfXmlSerializer instance. ```APIDOC ## RdfXmlSerializer::new ### Description Creates a new RdfXmlSerializer instance. ### Method new ### Signature pub fn new() -> Self ### Parameters None ### Returns A new RdfXmlSerializer instance. ### Example ```rust let serializer = RdfXmlSerializer::new(); ``` ``` -------------------------------- ### fn partition(self, f: F) -> (B, B) Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Consumes an iterator, creating two collections from it. ```APIDOC ## fn partition(self, f: F) -> (B, B) ### Description Consumes an iterator, creating two collections from it. ### Method `partition` ### Signature rust fn partition(self, f: F) -> (B, B) where Self: Sized, B: Default + Extend, F: FnMut(&Self::Item) -> bool, ### Parameters - **f** (F) - Required - A closure that determines which collection each item goes into. ### Returns - **(B, B)** - A tuple of two collections: one for items where `f` returns `true`, and one for items where `f` returns `false`. ``` -------------------------------- ### Inspect prefixes with RdfXmlParser::prefixes Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Shows how to inspect the prefixes collected by the parser. Initially empty, after reading the first triple the prefixes include rdf and schema. Uses RdfXmlParser::new().for_slice(file) and parser.prefixes(). ```rust use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_slice(file); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes // # Result::<_, Box>::Ok(()) ``` -------------------------------- ### RdfXmlPrefixesIter Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/index.html Iterator on the file prefixes. ```APIDOC ## RdfXmlPrefixesIter ### Description Iterator on the file prefixes. Provides access to namespace prefixes defined in the RDF/XML document. ### Usage ```rust use oxrdfxml::RdfXmlPrefixesIter; // Iterate over prefixes. ``` ``` -------------------------------- ### Read base IRI with ReaderRdfXmlParser::base_iri Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.ReaderRdfXmlParser.html Demonstrates the base_iri() method, which returns the base IRI at the current parsing step. Initially None, after reading the first triple it becomes Some("http://example.com/") because the document declares xml:base. Uses RdfXmlParser::new().for_reader() and parser.next().unwrap()?. ```rust use oxrdfxml::RdfXmlParser; let file = r#" "#; let mut parser = RdfXmlParser::new().for_reader(file.as_bytes()); assert!(parser.base_iri().is_none()); // No base at the beginning because none has been given to the parser. parser.next().unwrap()?; // We read the first triple assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI. ``` -------------------------------- ### fn map_windows(self, f: F) -> MapWindows Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Calls the given function f for each contiguous window of size N over self and returns an iterator over the outputs of f. Like slice::windows(), the windows during mapping overlap as well. ```APIDOC ## fn map_windows(self, f: F) -> MapWindows ### Description Calls the given function `f` for each contiguous window of size `N` over `self` and returns an iterator over the outputs of `f`. Like `slice::windows()`, the windows during mapping overlap as well. ### Method `map_windows` ### Signature rust fn map_windows(self, f: F) -> MapWindows where Self: Sized, F: FnMut(&[Self::Item; N]) -> R, ### Parameters - **f** (F) - Required - A closure that takes a reference to a window of `N` items and returns a value. ### Returns - **MapWindows** - An iterator adapter that yields the results of applying `f` to each window. ``` -------------------------------- ### Check base IRI with TokioAsyncReaderRdfXmlParser Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.TokioAsyncReaderRdfXmlParser.html Demonstrates the `base_iri` method. The base IRI is None at the beginning and becomes Some after reading the first triple when an xml:base is present in the RDF/XML. ```rust use oxrdfxml::RdfXmlParser; let file = r#" "#; let mut parser = RdfXmlParser::new().for_tokio_async_reader(file.as_bytes()); assert!(parser.base_iri().is_none()); // No base at the beginning because none has been given to the parser. parser.next().await.unwrap()?; // We read the first triple assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI. ``` -------------------------------- ### fn take(self, n: usize) -> Take Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Creates an iterator that yields the first n elements, or fewer if the underlying iterator ends sooner. ```APIDOC ## fn take(self, n: usize) -> Take ### Description Creates an iterator that yields the first `n` elements, or fewer if the underlying iterator ends sooner. ### Method `take` ### Signature rust fn take(self, n: usize) -> Take where Self: Sized, ### Parameters - **n** (usize) - Required - The number of elements to take. ### Returns - **Take** - An iterator adapter that yields at most `n` elements. ``` -------------------------------- ### Inspect RDF/XML prefixes with ReaderRdfXmlParser::prefixes Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.ReaderRdfXmlParser.html Shows how the prefixes() method returns an iterator of (prefix, IRI) tuples. Initially empty, after reading the first triple it contains the rdf and schema prefixes declared in the document. Uses RdfXmlParser::new().for_reader() and parser.next().unwrap()?. ```rust use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_reader(file.as_bytes()); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes ``` -------------------------------- ### Retrieving IRI Prefixes Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Shows how to inspect the IRI prefixes encountered during the parsing process. The prefix list is empty initially and updates as the parser encounters them. ```rust # #[tokio::main(flavor = "current_thread")] # async fn main() -> Result<(), Box> { use oxrdfxml::RdfXmlParser; let file = r#" Foo "#; let mut parser = RdfXmlParser::new().for_tokio_async_reader(file.as_bytes()); assert_eq!(parser.prefixes().collect::>(), []); // No prefix at the beginning parser.next().await.unwrap()?; // We read the first triple assert_eq!( parser.prefixes().collect::>(), [ ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), ("schema", "http://schema.org/") ] ); // There are now prefixes // # Ok(()) # } ``` -------------------------------- ### RdfXmlSerializer::with_prefix Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlSerializer.html Adds a namespace prefix to the serializer. ```APIDOC ## RdfXmlSerializer::with_prefix ### Description Adds a namespace prefix to the serializer. Returns a Result indicating success or failure. ### Method with_prefix ### Signature pub fn with_prefix(self, prefix: &str, namespace: &str) -> Result ### Parameters - **prefix** (string) - Required - The prefix to use. - **namespace** (string) - Required - The namespace URI. ### Returns A Result containing the serializer with the prefix added, or an error. ### Example ```rust let serializer = RdfXmlSerializer::new().with_prefix("schema", "http://schema.org/")?; ``` ``` -------------------------------- ### product Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlPrefixesIter.html Iterates over the entire iterator, multiplying all the elements. ```APIDOC ## product ### Description Iterates over the entire iterator, multiplying all the elements. ### Method fn product

(self) -> P ### Constraints - Self: Sized - P: Product ### Returns P - The product of the elements. ``` -------------------------------- ### fn fold(self, init: B, f: F) -> B Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Folds every element into an accumulator by applying an operation, returning the final result. ```APIDOC ## fn fold(self, init: B, f: F) -> B ### Description Folds every element into an accumulator by applying an operation, returning the final result. ### Method `fold` ### Signature rust fn fold(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B, ### Parameters - **init** (B) - Required - The initial accumulator value. - **f** (F) - Required - A closure that takes the accumulator and an item, and returns the new accumulator. ### Returns - **B** - The final accumulator value. ``` -------------------------------- ### fn by_ref(&mut self) -> &mut Self Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Creates a “by reference” adapter for this instance of Iterator. ```APIDOC ## fn by_ref(&mut self) -> &mut Self ### Description Creates a “by reference” adapter for this instance of `Iterator`. ### Method `by_ref` ### Signature rust fn by_ref(&mut self) -> &mut Self where Self: Sized, ### Returns - **&mut Self** - A mutable reference to the iterator. ``` -------------------------------- ### RdfXmlSerializer::finish Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlSerializer.html Finishes serialization and returns the output. ```APIDOC ## RdfXmlSerializer::finish ### Description Finishes serialization and returns the output as a byte slice. Returns a Result containing the output. ### Method finish ### Signature pub fn finish(self) -> Result, ...> ### Parameters None ### Returns A Result containing the serialized output as a byte vector. ### Example ```rust let output = serializer.finish()?; ``` ``` -------------------------------- ### Imports for RdfXmlParser Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Imports required for the RDF/XML parser implementation, including oxrdf types, quick_xml for XML parsing, and optional async support. ```rust use crate::error::{RdfXmlParseError, RdfXmlSyntaxError}; use crate::utils::*; use oxilangtag::LanguageTag; use oxiri::{Iri, IriParseError}; #[cfg(feature = "rdf-12")] use oxrdf::BaseDirection; use oxrdf::vocab::rdf; use oxrdf::{BlankNode, Literal, NamedNode, NamedOrBlankNode, Term, Triple}; use quick_xml::escape::{resolve_xml_entity, unescape_with}; use quick_xml::events::attributes::Attribute; use quick_xml::events::*; use quick_xml::name::{LocalName, Namespace, PrefixDeclaration, PrefixIter, ResolveResult}; use quick_xml::{Decoder, Error, NsReader, Writer}; use std::borrow::Cow; use std::collections::{HashMap, HashSet}; use std::io::{BufReader, Read}; use std::str; #[cfg(feature = "async-tokio")] use tokio::io::{AsyncRead, BufReader as AsyncBufReader}; ``` -------------------------------- ### next Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Asynchronously reads the next triple from the RDF/XML input. Returns None when the input is exhausted. ```APIDOC ## next ### Description Asynchronously reads the next triple from the RDF/XML input. Returns `None` when the input is exhausted. ### Method `pub async fn next(&mut self) -> Option>` ### Parameters None. ### Returns - `Option>` - `Some(Ok(triple))` if a triple is available, `Some(Err(e))` on parse error, `None` if the input is finished. ### Example ```rust let mut parser = RdfXmlParser::new().for_tokio_async_reader(file.as_bytes()); while let Some(triple) = parser.next().await { let triple = triple?; // process triple } ``` ``` -------------------------------- ### RdfXmlSerializer::for_tokio_async_writer Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Creates a serializer that writes RDF/XML to an asynchronous writer implementing AsyncWrite. This method is only available when the async-tokio feature is enabled. ```APIDOC ## RdfXmlSerializer::for_tokio_async_writer ### Description Creates a serializer that writes RDF/XML to an asynchronous writer implementing AsyncWrite. This method is only available when the async-tokio feature is enabled. ### Method for_tokio_async_writer ### Signature pub fn for_tokio_async_writer(self, writer: W) -> TokioAsyncWriterRdfXmlSerializer ### Parameters - **writer** (W) - Required - The asynchronous writer to write the RDF/XML output to. Must implement AsyncWrite and Unpin. ### Return Value Returns a TokioAsyncWriterRdfXmlSerializer that can be used to serialize triples and finish the output. ### Usage Example ```rust use oxrdf::{NamedNodeRef, TripleRef, LiteralRef}; use oxrdf::vocab::rdf; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new().with_prefix("schema", "http://schema.org/")?.for_tokio_async_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, rdf::TYPE, NamedNodeRef::new("http://schema.org/Person")?, )).await?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://schema.org/name")?, LiteralRef::new_language_tagged_literal_unchecked("Foo Bar", "en"), )).await?; assert_eq!( b"\n\n\t\n\t\tFoo Bar\n\t\n", serializer.finish().await?.as_slice() ); ``` ``` -------------------------------- ### Iterator for RdfXmlPrefixesIter Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlPrefixesIter.html RdfXmlPrefixesIter implements the Iterator trait, yielding items of type (&'a str, &'a str). The next method advances the iterator and returns the next prefix pair, while size_hint provides bounds on the remaining length. ```APIDOC ## Iterator for RdfXmlPrefixesIter ### Description RdfXmlPrefixesIter implements the Iterator trait, yielding items of type (&'a str, &'a str). The next method advances the iterator and returns the next prefix pair, while size_hint provides bounds on the remaining length. ### Type Item (&'a str, &'a str) ### Methods #### next - **Signature**: fn next(&mut self) -> Option - **Description**: Advances the iterator and returns the next value. #### size_hint - **Signature**: fn size_hint(&self) -> (usize, Option) - **Description**: Returns the bounds on the remaining length of the iterator. ``` -------------------------------- ### Access base IRI with SliceRdfXmlParser::base_iri Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Shows how to retrieve the current base IRI. The base IRI is `None` at the beginning if none was provided, and becomes `Some("http://example.com/")` after reading the first triple when the XML declares `xml:base`. ```rust use oxrdfxml::RdfXmlParser; let file = r#" "#; let mut parser = RdfXmlParser::new().for_slice(file); assert!(parser.base_iri().is_none()); // No base at the beginning because none has been given to the parser. parser.next().unwrap()?; // We read the first triple assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI. ``` -------------------------------- ### Check base IRI with RdfXmlParser::base_iri Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/parser.rs.html Demonstrates how the base IRI is initially None and becomes Some after reading the first triple when xml:base is present. Uses RdfXmlParser::new().for_slice(file) and parser.base_iri(). ```rust use oxrdfxml::RdfXmlParser; let file = r#" "#; let mut parser = RdfXmlParser::new().for_slice(file); assert!(parser.base_iri().is_none()); // No base at the beginning because none has been given to the parser. parser.next().unwrap()?; // We read the first triple assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI. # Result::<_, Box>::Ok(()) ``` -------------------------------- ### RdfXmlSerializer::for_writer Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlSerializer.html Creates a writer that serializes RDF/XML to a synchronous Write implementation. ```APIDOC ## RdfXmlSerializer::for_writer ### Description Serializes a RDF/XML file to a `Write` implementation. This writer does unbuffered writes. ### Method `pub fn for_writer(self, writer: W) -> WriterRdfXmlSerializer` ### Parameters - `writer` (W) - Required - The `Write` implementation to write the RDF/XML output to. ### Returns `WriterRdfXmlSerializer` - A serializer that writes to the given writer. ### Example ```rust use oxrdf::{NamedNodeRef, TripleRef}; use oxrdfxml::RdfXmlSerializer; let mut serializer = RdfXmlSerializer::new() .with_base_iri("http://example.com")? .with_prefix("ex", "http://example.com/ns#")? .for_writer(Vec::new()); serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?, NamedNodeRef::new("http://example.com/ns#Person")?, ))?; serializer.serialize_triple(TripleRef::new( NamedNodeRef::new("http://example.com#me")?, NamedNodeRef::new("http://example.com/ns#parent")?, NamedNodeRef::new("http://example.com#other")?, ))?; assert_eq!( b"\n\n\t\n\t\t\n\t\n", serializer.finish()?.as_slice() ); ``` ``` -------------------------------- ### fn scan(self, initial_state: St, f: F) -> Scan Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html An iterator adapter which, like fold, holds internal state, but unlike fold, produces a new iterator. ```APIDOC ## fn scan(self, initial_state: St, f: F) -> Scan ### Description An iterator adapter which, like `fold`, holds internal state, but unlike `fold`, produces a new iterator. ### Method `scan` ### Signature rust fn scan(self, initial_state: St, f: F) -> Scan where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option, ### Parameters - **initial_state** (St) - Required - The initial state for the scan. - **f** (F) - Required - A closure that takes the current state and an item, and returns an optional new item. ### Returns - **Scan** - An iterator adapter that yields the results of the scan. ``` -------------------------------- ### fn inspect(self, f: F) -> Inspect Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Does something with each element of an iterator, passing the value on. ```APIDOC ## fn inspect(self, f: F) -> Inspect ### Description Does something with each element of an iterator, passing the value on. ### Method `inspect` ### Signature rust fn inspect(self, f: F) -> Inspect where Self: Sized, F: FnMut(&Self::Item), ### Parameters - **f** (F) - Required - A closure that is called with a reference to each item. ### Returns - **Inspect** - An iterator adapter that yields the original items after calling `f` on each. ``` -------------------------------- ### fn reduce(self, f: F) -> Option Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.SliceRdfXmlParser.html Reduces the elements to a single one, by repeatedly applying a reducing operation. ```APIDOC ## fn reduce(self, f: F) -> Option ### Description Reduces the elements to a single one, by repeatedly applying a reducing operation. ### Method `reduce` ### Signature rust fn reduce(self, f: F) -> Option where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item, ### Parameters - **f** (F) - Required - A closure that takes two items and returns a single item. ### Returns - **Option** - The reduced value, or `None` if the iterator is empty. ``` -------------------------------- ### prefixes Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.TokioAsyncReaderRdfXmlParser.html Returns an iterator over the IRI prefixes considered at the current step of parsing. The iterator yields (prefix name, prefix value) tuples. ```APIDOC ## prefixes ### Description The list of IRI prefixes considered at the current step of the parsing. This method returns (prefix name, prefix value) tuples. It is empty at the beginning of the parsing and gets updated when prefixes are encountered. It should be full at the end of the parsing (but if a prefix is overridden, only the latest version will be returned). ### Method pub fn prefixes(&self) -> RdfXmlPrefixesIter<'_> ### Parameters - **self** (&TokioAsyncReaderRdfXmlParser) - Required - The parser instance. ### Returns - **RdfXmlPrefixesIter<'_>** - An iterator over (prefix name, prefix value) tuples. ``` -------------------------------- ### any Source: https://docs.rs/oxrdfxml/latest/oxrdfxml/struct.RdfXmlPrefixesIter.html Tests if any element of the iterator matches a predicate. ```APIDOC ## any ### Description Tests if any element of the iterator matches a predicate. ### Method fn any(&mut self, f: F) -> bool ### Parameters - **f** (F) - Required - The predicate function. ### Constraints - Self: Sized - F: FnMut(Self::Item) -> bool ### Returns bool - true if any element matches, false otherwise. ``` -------------------------------- ### Test IRI splitting logic Source: https://docs.rs/oxrdfxml/latest/src/oxrdfxml/serializer.rs.html Verifies the behavior of the split_iri function with various IRI formats including standard URLs, fragments, and URNs. ```rust #[test] fn test_split_iri() { assert_eq!( split_iri("http://schema.org/Person"), ("http://schema.org/", "Person") ); assert_eq!(split_iri("http://schema.org/"), ("http://schema.org/", "")); assert_eq!( split_iri("http://schema.org#foo"), ("http://schema.org#", "foo") ); assert_eq!(split_iri("urn:isbn:foo"), ("urn:isbn:", "foo")); } ```