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

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

            
69
pub fn read_entries(path: &Path) -> Result<Vec<IndexEntry>, Box<dyn Error>> {
70
    let index_path = IndexPath::try_from(path)?;
71
    let mut names = Vec::new();
72
    for name in index_path.files()? {
73
        names.push(IndexEntry {
74
            name,
75
            base_uri: None,
76
        });
77
    }
78
    Ok(names)
79
}
80

            
81
pub fn check_index(path: &Path) -> Result<(), Box<dyn Error>> {
82
    let index_path = IndexPath::try_from(path)?;
83
    read_index(&index_path)?;
84
    Ok(())
85
}