Skip to main content

waratah_api/
repos.rs

1//! Multi-repository catalog (MVP: a single `"main"` repository).
2
3use std::collections::HashMap;
4
5use crate::MAIN_REPOSITORY_ID;
6
7/// Per-repository metadata held at the HTTP edge.
8#[derive(Debug, Clone, Default)]
9pub struct RepositoryState {
10    /// In-memory prefix → namespace IRI map (not persisted).
11    pub namespaces: HashMap<String, String>,
12}
13
14impl RepositoryState {
15    /// Seed common RDF prefixes used by RDF4J clients.
16    pub fn with_default_namespaces() -> Self {
17        let mut namespaces = HashMap::new();
18        namespaces.insert(
19            "rdf".to_owned(),
20            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_owned(),
21        );
22        namespaces.insert(
23            "rdfs".to_owned(),
24            "http://www.w3.org/2000/01/rdf-schema#".to_owned(),
25        );
26        namespaces.insert(
27            "xsd".to_owned(),
28            "http://www.w3.org/2001/XMLSchema#".to_owned(),
29        );
30        Self { namespaces }
31    }
32}
33
34/// Catalog of repositories exposed by this RDF4J server.
35#[derive(Debug, Clone)]
36pub struct RepositoryCatalog {
37    repos: HashMap<String, RepositoryState>,
38}
39
40impl RepositoryCatalog {
41    /// MVP catalog containing only repository `"main"`.
42    pub fn main_only() -> Self {
43        let mut repos = HashMap::new();
44        repos.insert(
45            MAIN_REPOSITORY_ID.to_owned(),
46            RepositoryState::with_default_namespaces(),
47        );
48        Self { repos }
49    }
50
51    /// Look up a repository by id.
52    pub fn get(&self, id: &str) -> Option<&RepositoryState> {
53        self.repos.get(id)
54    }
55
56    /// Sorted repository ids.
57    pub fn ids(&self) -> Vec<String> {
58        let mut ids: Vec<String> = self.repos.keys().cloned().collect();
59        ids.sort();
60        ids
61    }
62
63    /// Whether `id` is a known repository.
64    pub fn contains(&self, id: &str) -> bool {
65        self.repos.contains_key(id)
66    }
67}
68
69impl Default for RepositoryCatalog {
70    fn default() -> Self {
71        Self::main_only()
72    }
73}