1
use crate::server::{
2
    dataset::{Dataset, PredicateObject},
3
    DatasetSummary, DatasetVersionSummary,
4
};
5
use askama::Template;
6
use sophia::api::{term::IriRef, MownStr};
7
use std::collections::BTreeSet;
8

            
9
2
#[derive(Template)]
10
2
#[template(path = "index.html")]
11
pub(crate) struct IndexTemplate<'a> {
12
    pub datasets: &'a [DatasetSummary<'a>],
13
}
14

            
15
1
#[derive(Template)]
16
1
#[template(path = "dataset.html")]
17
pub(crate) struct DatasetTemplate<'a> {
18
    pub dataset: &'a Dataset,
19
    pub versions: &'a [DatasetVersionSummary],
20
}
21

            
22
1
#[derive(Template)]
23
1
#[template(path = "version.html")]
24
pub(crate) struct VersionTemplate<'a> {
25
    pub dataset: &'a DatasetSummary<'a>,
26
    pub version: &'a DatasetVersionSummary,
27
    pub subjects: BTreeSet<&'a IriRef<MownStr<'a>>>,
28
}
29

            
30
6
#[derive(Template)]
31
1
#[template(path = "subject.html")]
32
pub(crate) struct SubjectTemplate<'a> {
33
    pub dataset: &'a DatasetSummary<'a>,
34
    pub subject: String,
35
    pub version: &'a DatasetVersionSummary,
36
    pub pos: Vec<PredicateObject<'a>>,
37
}
38

            
39
mod filters {
40
    use std::fmt;
41

            
42
    use askama::Error::Fmt;
43
    use chrono::{DateTime, Utc};
44
    use lazy_static::lazy_static;
45
    use sophia::{
46
        api::prefix::{PrefixMap, PrefixMapPair},
47
        iri::Iri,
48
    };
49

            
50
    use crate::server::{
51
        dataset::{load_datasets, load_prefixes, Dataset},
52
        get_dataset_version,
53
    };
54

            
55
    static LABEL_RESOURCE: &str = "http://www.w3.org/2000/01/rdf-schema#label";
56

            
57
    lazy_static! {
58
        static ref DATASETS: Vec<Dataset> = load_datasets("datasets").unwrap_or_default();
59
    }
60

            
61
    lazy_static! {
62
        static ref PREFIXES: Vec<PrefixMapPair> = load_prefixes("datasets").unwrap_or_default();
63
    }
64

            
65
124
    pub fn replace_prefix<T: std::fmt::Display>(s: T) -> ::askama::Result<String> {
66
124
        let prefix_found = PREFIXES.get_prefixed_pair(Iri::new_unchecked(s.to_string()));
67

            
68
124
        let out = if let Some(ps) = prefix_found {
69
122
            format!("{}:{}", ps.0.as_str(), ps.1)
70
        } else {
71
2
            s.to_string()
72
        };
73

            
74
124
        Ok(out)
75
124
    }
76

            
77
    // Would be nice if this filter returned the found prefix in the original URL format
78
    // pub fn prefix<T: std::fmt::Display>(s: T) -> ::askama::Result<String> {
79
    //     let prefix_found = PREFIXES.get_prefixed_pair(Iri::new_unchecked(s.to_string()));
80

            
81
    //     let out = if let Some(ps) = prefix_found {
82
    //         format!("{}", ps.0.)
83
    //     } else {
84
    //         s.to_string()
85
    //     };
86

            
87
    //     Ok(out)
88
    // }
89

            
90
123
    pub fn rdfslabel<T: std::fmt::Display>(
91
123
        s: T,
92
123
        id: &&str,
93
123
        version: &DateTime<Utc>,
94
123
    ) -> ::askama::Result<String> {
95
123
        let subject = Iri::new_unchecked(s.to_string());
96
123
        let Ok((_, dataset_version)) = get_dataset_version(&DATASETS, id, *version) else {
97
            return Err(Fmt(fmt::Error));
98
        };
99
123
        let pos = dataset_version.pos(&subject);
100
123

            
101
123
        let find_target_po = pos
102
123
            .into_iter()
103
379
            .find(|po| po.predicate.as_str() == LABEL_RESOURCE);
104
123
        let Some(target_po) = find_target_po else {
105
7
            return Ok("".to_string());
106
        };
107
116
        let Some(target_object) = target_po.object_literal else {
108
            return Ok("".to_string());
109
        };
110

            
111
116
        Ok(target_object.to_string())
112
123
    }
113
}