tsk_rs/
namespace.rs

1use std::fs;
2
3use crate::settings::Settings;
4use color_eyre::eyre::{Context, Result};
5
6/// Namespace abstraction and metadata
7pub struct Namespace {
8    /// If the namespace detected is the one currently active in configuration
9    pub is_current: bool,
10    /// Name of the detected namespace
11    pub name: String,
12}
13
14/// List all namespaces available in the ecosystem
15pub fn list_namespaces(settings: &Settings) -> Result<Vec<Namespace>> {
16    let mut namespaces: Vec<Namespace> = vec![];
17
18    // search available namespaces from filesystem
19    for entry in fs::read_dir(settings.data.path.clone()).with_context(|| "error while scanning database directory for namespaces")? {
20        let entry = entry?;
21        if entry.metadata()?.is_dir() {
22            let name = entry.file_name().to_str().unwrap().to_string(); // TODO: fix unwrap
23            let is_current = if name == settings.namespace { true } else { false };
24            namespaces.push(Namespace { is_current, name });
25        }
26    }
27
28    // if no namespaces found, add the default one and set it active
29    if namespaces.len() == 0 {
30        namespaces.push(
31            Namespace { name: "default".into(), is_current: true }
32        );
33    }
34
35    Ok(namespaces)
36}
37
38// eof