git-next/crates/server/src/config/load.rs

70 lines
1.7 KiB
Rust
Raw Normal View History

2024-04-09 10:44:01 +01:00
use terrors::OneOf;
use tracing::error;
2024-04-09 10:44:01 +01:00
2024-05-11 18:57:18 +01:00
use crate::{
config::{BranchName, RepoConfig, RepoDetails},
gitforge::{self, ForgeFileError},
};
2024-04-09 10:44:01 +01:00
pub async fn load(
details: &RepoDetails,
forge: &gitforge::Forge,
) -> Result<
RepoConfig,
OneOf<(
ForgeFileError,
2024-05-11 18:57:18 +01:00
crate::config::Error,
toml::de::Error,
RepoConfigValidationErrors,
)>,
> {
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)
2024-04-09 10:44:01 +01:00
}
#[derive(Debug)]
pub enum RepoConfigValidationErrors {
Forge(gitforge::ForgeBranchError),
BranchNotFound(BranchName),
}
pub async fn validate(
config: RepoConfig,
forge: &gitforge::Forge,
) -> Result<RepoConfig, RepoConfigValidationErrors> {
let branches = forge.branches_get_all().await.map_err(|e| {
error!(?e, "Failed to list branches");
RepoConfigValidationErrors::Forge(e)
})?;
if !branches
.iter()
.any(|branch| branch.name() == &config.branches().main())
{
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().main(),
));
}
if !branches
.iter()
.any(|branch| branch.name() == &config.branches().next())
{
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().next(),
));
}
if !branches
.iter()
.any(|branch| branch.name() == &config.branches().dev())
{
return Err(RepoConfigValidationErrors::BranchNotFound(
config.branches().dev(),
));
}
Ok(config)
}