Fork of https://github.com/oxigraph/oxigraph.git for the purpose of NextGraph project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
oxigraph/testsuite/src/files.rs

83 lines
2.7 KiB

use oxigraph::model::NamedOrBlankNode;
use oxigraph::{DatasetSyntax, Error, GraphSyntax, MemoryStore, Result};
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::PathBuf;
pub fn read_file(url: &str) -> Result<impl BufRead> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(if url.starts_with("http://w3c.github.io/rdf-tests/") {
Ok(url.replace("http://w3c.github.io/rdf-tests/", "rdf-tests/"))
} else if url.starts_with("http://www.w3.org/2013/RDFXMLTests/") {
Ok(url.replace("http://www.w3.org/2013/RDFXMLTests/", "rdf-tests/rdf-xml/"))
} else if url.starts_with("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/") {
Ok(url.replace(
"http://www.w3.org/2001/sw/DataAccess/tests/",
"rdf-tests/sparql11/",
))
} else if url.starts_with("http://www.w3.org/2009/sparql/docs/tests/data-sparql11/") {
Ok(url.replace(
"http://www.w3.org/2009/sparql/docs/tests/",
"rdf-tests/sparql11/",
))
} else if url.starts_with("https://github.com/oxigraph/oxigraph/tests/") {
Ok(url.replace(
"https://github.com/oxigraph/oxigraph/tests/",
"oxigraph-tests/",
))
} else {
Err(Error::msg(format!("Not supported url for file: {}", url)))
}?);
Ok(BufReader::new(File::open(&path)?))
}
pub fn read_file_to_string(url: &str) -> Result<String> {
let mut buf = String::new();
read_file(url)?.read_to_string(&mut buf)?;
Ok(buf)
}
pub fn load_to_store(
url: &str,
store: &MemoryStore,
to_graph_name: Option<&NamedOrBlankNode>,
) -> Result<()> {
if url.ends_with(".nt") {
store.load_graph(
read_file(url)?,
GraphSyntax::NTriples,
to_graph_name,
Some(url),
)
} else if url.ends_with(".ttl") {
store.load_graph(
read_file(url)?,
GraphSyntax::Turtle,
to_graph_name,
Some(url),
)
} else if url.ends_with(".rdf") {
store.load_graph(
read_file(url)?,
GraphSyntax::RdfXml,
to_graph_name,
Some(url),
)
} else if url.ends_with(".nq") {
store.load_dataset(read_file(url)?, DatasetSyntax::NQuads, Some(url))
} else if url.ends_with(".trig") {
store.load_dataset(read_file(url)?, DatasetSyntax::TriG, Some(url))
} else {
Err(Error::msg(format!(
"Serialization type not found for {}",
url
)))
}
}
pub fn load_store(url: &str) -> Result<MemoryStore> {
let store = MemoryStore::new();
load_to_store(url, &store, None)?;
Ok(store)
}