1use {
2 crate::{Literal, Namespace},
3 std::ffi::CString,
4};
5
6#[derive(Debug, Clone)]
10pub struct Graph {
11 pub namespace: Namespace,
12 pub local_name: String,
13}
14
15impl std::fmt::Display for Graph {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 write!(
20 f,
21 "{}{}",
22 self.namespace.name.as_str(),
23 self.local_name.as_str()
24 )
25 }
26}
27
28impl Graph {
29 pub fn declare(namespace: Namespace, local_name: &str) -> Self {
30 Self { namespace, local_name: local_name.to_string() }
33 }
34
35 pub fn dataset_from_path(namespace: Namespace, path: &std::path::Path) -> Self {
36 Self::declare(
37 namespace,
38 path.file_name().unwrap().to_str().unwrap(),
39 )
40 }
41
42 pub fn test_dataset_from_path(namespace: Namespace, path: &std::path::Path) -> Self {
43 Self::declare(
44 namespace,
45 format!(
46 "test-{}",
47 path.file_name().unwrap().to_str().unwrap()
48 )
49 .as_str(),
50 )
51 }
52
53 pub fn as_iri(&self) -> Result<iri_string::types::IriReferenceString, ekg_error::Error> {
54 self.namespace
55 .with_local_name(self.local_name.as_str())
56 .map_err(ekg_error::Error::from)
57 }
58
59 pub fn as_display_iri(&self) -> GraphDisplayIRI { GraphDisplayIRI { graph: self } }
60
61 pub fn as_c_string(&self) -> Result<CString, ekg_error::Error> {
62 let literal = self.as_lexical_value()?;
65 CString::new(literal.to_string().as_str()).map_err(ekg_error::Error::from)
66 }
67
68 pub fn as_lexical_value(&self) -> Result<Literal, ekg_error::Error> {
69 Literal::from_iri(self.as_iri()?.as_ref())
70 }
71}
72
73pub struct GraphDisplayIRI<'a> {
74 graph: &'a Graph,
75}
76
77impl<'a> std::fmt::Display for GraphDisplayIRI<'a> {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(
81 f,
82 "<{:}{}>",
83 self.graph.namespace.iri,
84 self.graph.local_name.as_str()
85 )
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 #[test]
93 fn test_display_iri() {
94 let ns =
95 iri_string::types::IriReferenceString::try_from("https://whatever.kom/graph/").unwrap();
96 let graph_prefix = crate::Namespace::declare("graph:", ns.try_into().unwrap()).unwrap();
97 let graph = crate::Graph::declare(graph_prefix, "somedataset");
98
99 assert_eq!(
100 format!("{:}", graph).as_str(),
101 "graph:somedataset"
102 );
103 assert_eq!(
104 format!("{:}", graph.as_display_iri()).as_str(),
105 "<https://whatever.kom/graph/somedataset>"
106 );
107 }
108
109 #[test]
111 fn test_graph_ns() {
112 let ns =
113 iri_string::types::IriReferenceString::try_from("https://whatever.kom/graph/").unwrap();
114 let graph_prefix = crate::Namespace::declare("kggraph:", ns.try_into().unwrap()).unwrap();
115
116 let graph = crate::Graph::declare(graph_prefix, "somedataset");
117 let c_string = graph.as_c_string().unwrap().into_string().unwrap();
118
119 assert_eq!(
120 c_string,
121 "<https://whatever.kom/graph/somedataset>"
122 );
123 }
124}