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 // `` 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 { 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 for BackConfig { type Error = error::Error; fn try_from(value: RawBackConfig) -> Result { 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, }) } }