reach_attestant/
storage.rs

1// SPDX-FileCopyrightText: 2023-2025 eaon <eaon@posteo.net>
2// SPDX-License-Identifier: EUPL-1.2
3
4use std::{io, path::PathBuf};
5
6use reach_core::storage::create_parents;
7
8fn path(base: Option<PathBuf>, storing: bool) -> Result<PathBuf, io::Error> {
9    let path = base
10        .ok_or(io::Error::new(
11            io::ErrorKind::NotFound,
12            "This application expects to be run by a user with a home directory.",
13        ))?
14        .join("reach-attestant");
15
16    if storing {
17        create_parents(&path.join("dummy"))?
18    }
19
20    Ok(path)
21}
22
23pub fn data_path(storing: bool) -> Result<PathBuf, io::Error> {
24    path(dirs::data_dir(), storing)
25}
26
27pub fn config_path(storing: bool) -> Result<PathBuf, io::Error> {
28    path(dirs::config_dir(), storing)
29}
30
31pub fn file_path(
32    profile_name: &str,
33    alias: Option<&str>,
34    extension: &str,
35    storing: bool,
36) -> Result<PathBuf, io::Error> {
37    Ok(match alias {
38        Some(alias) => data_path(storing)?.join(format!("{profile_name}/{alias}.{extension}")),
39        None => config_path(storing)?.join(format!("{profile_name}.{extension}")),
40    })
41}