1
pub(crate) mod index_path;
2

            
3
use self::index_path::IndexPath;
4
use crate::server_error::MyError;
5
use sophia::{
6
    api::{
7
        graph::MutableGraph,
8
        ns::{rdf::type_, Namespace},
9
        parser::TripleParser,
10
        serializer::{Stringifier, TripleSerializer},
11
        source::TripleSource,
12
        triple::Triple,
13
    },
14
    inmem::graph::LightGraph,
15
    iri::Iri,
16
    turtle::{parser::turtle::TurtleParser, serializer::turtle::TurtleSerializer},
17
};
18
use std::{error::Error, path::Path};
19

            
20
#[derive(PartialEq, Eq, PartialOrd, Ord)]
21
pub struct IndexEntry {
22
    pub name: String,
23
    pub base_uri: Option<String>,
24
}
25

            
26
/**
27
 * Read an index.ttl file and return the entries that are mentioned in it.
28
 */
29
10
pub(crate) fn read_index(index_ttl: &index_path::IndexPath) -> Result<Vec<IndexEntry>, MyError> {
30
10
    println!("{}", index_ttl.as_ref().display());
31
10
    let rdf = std::fs::read_to_string(index_ttl)
32
10
        .map_err(|e| MyError::io_error(index_ttl.to_path_buf(), e))?;
33
10
    let base: Iri<String> = Iri::new("http://example.com/".to_string())?;
34
10
    let parser = TurtleParser { base: Some(base) };
35
10
    let mut source = parser.parse_str(&rdf);
36
10
    let mut entries = Vec::new();
37
10
    let ldp = Namespace::new("http://www.w3.org/ns/ldp#")?;
38
10
    let ldp_contains = ldp.get("contains")?;
39
10
    source
40
10
        .for_each_triple(|t| {
41
10
            let p = t.p();
42
10
            if ldp_contains.eq(&p) {
43
                entries.push(IndexEntry {
44
                    name: t.s().to_string(),
45
                    base_uri: None,
46
                });
47
10
            }
48
10
        })
49
10
        .map_err(MyError::format_error)?;
50
10
    Ok(entries)
51
10
}
52

            
53
/**
54
 * Write index.ttl with all the given entries.
55
 * The ttl file will contain a an ldp#Container.
56
 * The entries are linked to the ldp#Container via ldp#contains predicates.
57
 */
58
10
pub fn write_index(path: &Path, entries: &[IndexEntry]) -> Result<(), Box<dyn Error>> {
59
10
    let index_path = IndexPath::try_from(path)?;
60
10
    let mut graph = LightGraph::new();
61
10
    let local = Namespace::new("")?;
62
10
    let ldp = Namespace::new("http://www.w3.org/ns/ldp#")?;
63
10
    let ldp_container = ldp.get("Container")?;
64
10
    let ldpc = local.get("")?;
65
10
    let ldp_contains = ldp.get("contains")?;
66
10
    graph.insert(ldpc, type_, ldp_container)?;
67
10
    for entry in entries {
68
        graph.insert(ldpc, ldp_contains, local.get(&entry.name)?)?;
69
    }
70
10
    let mut stringifier = TurtleSerializer::new_stringifier();
71
10
    let rdf = stringifier.serialize_graph(&graph)?.as_str();
72
10
    std::fs::write(index_path.as_ref(), rdf)?;
73
10
    println!("wrote {}", index_path.as_ref().display());
74
10
    Ok(())
75
10
}
76

            
77
/**
78
 * List all the files that are adjacent to an index.ttl in a directory.
79
 */
80
50
pub fn read_entries(path: &Path) -> Result<Vec<IndexEntry>, Box<dyn Error>> {
81
50
    let index_path = IndexPath::try_from(path)?;
82
10
    let mut names = Vec::new();
83
10
    for name in index_path.files()? {
84
        names.push(IndexEntry {
85
            name,
86
            base_uri: None,
87
        });
88
    }
89
10
    Ok(names)
90
50
}
91

            
92
10
pub fn check_index(path: &Path) -> Result<(), Box<dyn Error>> {
93
10
    let index_path = IndexPath::try_from(path)?;
94
10
    read_index(&index_path)?;
95
10
    Ok(())
96
10
}