git-next/crates/server/src/config/load.rs
Paul Campbell f8375ed1fc refactor(server/config): rename RepoConfigValidationError as Error
It is never referenced directly outside of this module.
2024-05-15 20:46:29 +01:00

51 lines
1.5 KiB
Rust

use git_next_config::{BranchName, RepoConfig};
use git_next_git::RepoDetails;
use terrors::OneOf;
use tracing::error;
use crate::gitforge::{self, ForgeFileError};
pub async fn load(
details: &RepoDetails,
forge: &gitforge::Forge,
) -> Result<RepoConfig, OneOf<(ForgeFileError, crate::config::Error, toml::de::Error, Error)>> {
let contents = forge
.file_contents_get(&details.branch, ".git-next.toml")
.await
.map_err(OneOf::new)?;
let config = RepoConfig::load(&contents).map_err(OneOf::new)?;
let config = validate(config, forge).await.map_err(OneOf::new)?;
Ok(config)
}
#[derive(Debug)]
pub enum Error {
Forge(gitforge::ForgeBranchError),
BranchNotFound(BranchName),
}
pub async fn validate(config: RepoConfig, forge: &gitforge::Forge) -> Result<RepoConfig, Error> {
let branches = forge.branches_get_all().await.map_err(|e| {
error!(?e, "Failed to list branches");
Error::Forge(e)
})?;
if !branches
.iter()
.any(|branch| branch == &config.branches().main())
{
return Err(Error::BranchNotFound(config.branches().main()));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().next())
{
return Err(Error::BranchNotFound(config.branches().next()));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().dev())
{
return Err(Error::BranchNotFound(config.branches().dev()));
}
Ok(config)
}