summary refs log tree commit diff stats
path: root/pkgs/by-name/ba/back/src/config/mod.rs
blob: a680b90d0a01467db14b6565ac7f33f6463c3c44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Back - An extremely simple git issue tracking system. Inspired by tvix's
// panettone
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of Back.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use std::{
    fs,
    path::{Path, PathBuf},
};

use gix::ThreadSafeRepository;
use serde::Deserialize;
use url::Url;

use crate::error::{self, Error};

pub struct BackConfig {
    // NOTE(@bpeetz): We do not need to html escape this, as the value must be a valid url. As such
    // `<tags>` of all kinds _should_ be invalid.  <2024-12-26>
    pub source_code_repository_url: Url,
    pub repository: ThreadSafeRepository,
}

#[derive(Deserialize)]
struct RawBackConfig {
    source_code_repository_url: Url,
    repository_path: PathBuf,
}

impl BackConfig {
    pub fn from_config_file(path: &Path) -> error::Result<Self> {
        let value = fs::read_to_string(path).map_err(|err| Error::ConfigRead {
            file: path.to_owned(),
            error: err,
        })?;

        let raw: RawBackConfig =
            serde_json::from_str(&value).map_err(|err| Error::ConfigParse {
                file: path.to_owned(),
                error: err,
            })?;

        Self::try_from(raw)
    }
}

impl TryFrom<RawBackConfig> for BackConfig {
    type Error = error::Error;

    fn try_from(value: RawBackConfig) -> Result<Self, Self::Error> {
        let repository = {
            ThreadSafeRepository::open(&value.repository_path).map_err(|err| Error::RepoOpen {
                repository_path: value.repository_path,
                error: Box::new(err),
            })
        }?;

        Ok(Self {
            repository,
            source_code_repository_url: value.source_code_repository_url,
        })
    }
}