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
    let rdf = std::fs::read_to_string(index_ttl)
31
10
        .map_err(|e| MyError::io_error(index_ttl.to_path_buf(), e))?;
32
10
    let base: Iri<String> = Iri::new("http://example.com/".to_string())?;
33
10
    let parser = TurtleParser { base: Some(base) };
34
10
    let mut source = parser.parse_str(&rdf);
35
10
    let mut entries = Vec::new();
36
10
    let ldp = Namespace::new("http://www.w3.org/ns/ldp#")?;
37
10
    let ldp_contains = ldp.get("contains")?;
38
10
    source
39
30
        .for_each_triple(|t| {
40
30
            let p = t.p();
41
30
            if ldp_contains.eq(&p) {
42
20
                entries.push(IndexEntry {
43
20
                    name: t.s().to_string(),
44
20
                    base_uri: None,
45
20
                });
46
20
            }
47
30
        })
48
10
        .map_err(MyError::format_error)?;
49
10
    Ok(entries)
50
10
}
51

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

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

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