25 releases

0.2.3 Nov 7, 2024
0.2.0 Sep 22, 2024
0.2.0-alpha.5 May 22, 2024
0.2.0-alpha.4 Mar 23, 2024
0.1.0 Mar 19, 2022

#1684 in Parser implementations

Download history 540/week @ 2024-08-21 468/week @ 2024-08-28 274/week @ 2024-09-04 313/week @ 2024-09-11 448/week @ 2024-09-18 368/week @ 2024-09-25 465/week @ 2024-10-02 564/week @ 2024-10-09 1143/week @ 2024-10-16 1367/week @ 2024-10-23 1299/week @ 2024-10-30 481/week @ 2024-11-06 633/week @ 2024-11-13 484/week @ 2024-11-20 764/week @ 2024-11-27 1259/week @ 2024-12-04

3,195 downloads per month
Used in 32 crates (8 directly)

MIT/Apache

405KB
9K SLoC

Sparesults

Latest Version Released API docs Crates.io downloads actions status Gitter

Sparesults is a set of parsers and serializers for SPARQL query results formats.

It supports SPARQL Query Results XML Format (Second Edition), SPARQL 1.1 Query Results JSON Format and SPARQL 1.1 Query Results CSV and TSV Formats.

Support for SPARQL-star is also available behind the rdf-star feature.

This crate is intended to be a building piece for SPARQL client and server implementations in Rust like Oxigraph.

The entry points of this library are the two QueryResultsParser and QueryResultsSerializer structs.

Usage example converting a JSON result file into a TSV result file:

use sparesults::{QueryResultsFormat, QueryResultsParser, ReaderQueryResultsParserOutput, QueryResultsSerializer};
use std::io::Result;

fn convert_json_to_tsv(json_file: &[u8]) -> Result<Vec<u8>> {
    let json_parser = QueryResultsParser::from_format(QueryResultsFormat::Json);
    let tsv_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
    // We start to read the JSON file and see which kind of results it is
    match json_parser.for_reader(json_file)? {
      ReaderQueryResultsParserOutput::Boolean(value) => {
            // it's a boolean result, we copy it in TSV to the output buffer
            tsv_serializer.serialize_boolean_to_writer(Vec::new(), value)
        },
      ReaderQueryResultsParserOutput::Solutions(solutions_reader) => {
            // it's a set of solutions, we create a writer and we write to it while reading in streaming from the JSON file
            let mut solutions_serializer = tsv_serializer.serialize_solutions_to_writer(Vec::new(), solutions_reader.variables().to_vec())?;
            for solution in solutions_reader {
              solutions_serializer.serialize(&solution?)?;
            }
          solutions_serializer.finish()
        }
    }
}

// Let's test with a boolean
assert_eq!(
    convert_json_to_tsv(b"{\"boolean\":true}".as_slice()).unwrap(),
    b"true"
);

// And with a set of solutions
assert_eq!(
    convert_json_to_tsv(b"{\"head\":{\"vars\":[\"foo\",\"bar\"]},\"results\":{\"bindings\":[{\"foo\":{\"type\":\"literal\",\"value\":\"test\"}}]}}".as_slice()).unwrap(),
    b"?foo\t?bar\n\"test\"\t\n"
);

License

This project is licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
  • MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Oxigraph by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.9–8.5MB
~65K SLoC