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
 */
56
10
pub fn write_index(path: &Path, entries: &[IndexEntry]) -> Result<(), Box<dyn Error>> {
57
10
    let index_path = IndexPath::try_from(path)?;
58
10
    let mut graph = LightGraph::new();
59
10
    let local = Namespace::new("")?;
60
10
    let ldp = Namespace::new("http://www.w3.org/ns/ldp#")?;
61
10
    let ldp_container = ldp.get("Container")?;
62
10
    let ldpc = local.get("")?;
63
10
    let ldp_contains = ldp.get("contains")?;
64
10
    graph.insert(ldpc, type_, ldp_container)?;
65
10
    for entry in entries {
66
        graph.insert(ldpc, ldp_contains, local.get(&entry.name)?)?;
67
    }
68
10
    let mut stringifier = TurtleSerializer::new_stringifier();
69
10
    let rdf = stringifier.serialize_graph(&graph)?.as_str();
70
10
    std::fs::write(index_path.as_ref(), rdf)?;
71
10
    println!("wrote {}", index_path.as_ref().display());
72
10
    Ok(())
73
10
}
74

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

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