Lines
100 %
Functions
55.56 %
pub(crate) mod index_path;
use self::index_path::IndexPath;
use crate::server_error::MyError;
use sophia::{
api::{
graph::MutableGraph,
ns::{rdf::type_, Namespace},
parser::TripleParser,
serializer::{Stringifier, TripleSerializer},
source::TripleSource,
triple::Triple,
},
inmem::graph::LightGraph,
iri::Iri,
turtle::{parser::turtle::TurtleParser, serializer::turtle::TurtleSerializer},
};
use std::{error::Error, path::Path};
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct IndexEntry {
pub name: String,
pub base_uri: Option<String>,
}
/**
* Read an index.ttl file and return the entries that are mentioned in it.
*/
pub(crate) fn read_index(index_ttl: &index_path::IndexPath) -> Result<Vec<IndexEntry>, MyError> {
let rdf = std::fs::read_to_string(index_ttl)
.map_err(|e| MyError::io_error(index_ttl.to_path_buf(), e))?;
let base: Iri<String> = Iri::new("http://example.com/".to_string())?;
let parser = TurtleParser { base: Some(base) };
let mut source = parser.parse_str(&rdf);
let mut entries = Vec::new();
let ldp = Namespace::new("http://www.w3.org/ns/ldp#")?;
let ldp_contains = ldp.get("contains")?;
source
.for_each_triple(|t| {
let p = t.p();
if ldp_contains.eq(&p) {
entries.push(IndexEntry {
name: t.s().to_string(),
base_uri: None,
});
})
.map_err(MyError::format_error)?;
Ok(entries)
* Write index.ttl with all the given entries.
* The ttl file will contain a an ldp#Container.
* The entries are linked to the ldp#Container via ldp#contains predicates.
pub fn write_index(path: &Path, entries: &[IndexEntry]) -> Result<(), Box<dyn Error>> {
let index_path = IndexPath::try_from(path)?;
let mut graph = LightGraph::new();
let local = Namespace::new("")?;
let ldp_container = ldp.get("Container")?;
let ldpc = local.get("")?;
graph.insert(ldpc, type_, ldp_container)?;
for entry in entries {
graph.insert(ldpc, ldp_contains, local.get(&entry.name)?)?;
let mut stringifier = TurtleSerializer::new_stringifier();
let rdf = stringifier.serialize_graph(&graph)?.as_str();
std::fs::write(index_path.as_ref(), rdf)?;
println!("wrote {}", index_path.as_ref().display());
Ok(())
* List all the files that are adjacent to an index.ttl in a directory.
pub fn read_entries(path: &Path) -> Result<Vec<IndexEntry>, Box<dyn Error>> {
let mut names = Vec::new();
for name in index_path.files()? {
names.push(IndexEntry {
name,
Ok(names)
pub fn check_index(path: &Path) -> Result<(), Box<dyn Error>> {
read_index(&index_path)?;