aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ba/back/src/config
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-26 10:00:45 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-26 10:01:56 +0100
commita3111a5d214db66b7d676940b8f8bfda5074bd45 (patch)
treea68cc1458386052a7c97a07b69bd161866ad7046 /pkgs/by-name/ba/back/src/config
parentfix(hosts/server2): Use correct path to `vhack.eu/nixos-server` repo (diff)
downloadnixos-server-a3111a5d214db66b7d676940b8f8bfda5074bd45.zip
fix(pkgs/back): Use rocket to manage the configuration values
This reduces the amount of needed `unwraps`/`expects` and allows us to streamline the parsing processes.
Diffstat (limited to 'pkgs/by-name/ba/back/src/config')
-rw-r--r--pkgs/by-name/ba/back/src/config/mod.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/pkgs/by-name/ba/back/src/config/mod.rs b/pkgs/by-name/ba/back/src/config/mod.rs
new file mode 100644
index 0000000..4986a41
--- /dev/null
+++ b/pkgs/by-name/ba/back/src/config/mod.rs
@@ -0,0 +1,58 @@
+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,
+ })
+ }
+}