Skip to content

Commit

Permalink
fix: Improve validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Jan 26, 2025
1 parent 7afd0b1 commit 87e8e14
Show file tree
Hide file tree
Showing 76 changed files with 2,668 additions and 391 deletions.
336 changes: 165 additions & 171 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ tempfile.workspace = true
tokio = { workspace = true, optional = true }
bon.workspace = true
users.workspace = true
thiserror = "2.0.7"

[features]
# Top level features
Expand Down
13 changes: 12 additions & 1 deletion recipe/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{borrow::Cow, path::PathBuf};

use blue_build_utils::syntax_highlighting::highlight_ser;
use blue_build_utils::{
constants::BLUE_BUILD_MODULE_IMAGE_REF, syntax_highlighting::highlight_ser,
};
use bon::Builder;
use colored::Colorize;
use indexmap::IndexMap;
Expand Down Expand Up @@ -95,6 +97,15 @@ impl<'a> ModuleRequiredFields<'a> {
}
}

#[must_use]
pub fn get_module_image(&self) -> String {
format!(
"{BLUE_BUILD_MODULE_IMAGE_REF}/{}:{}",
self.module_type.typ(),
self.module_type.version().unwrap_or("latest")
)
}

#[must_use]
pub fn is_local_source(&self) -> bool {
self.source
Expand Down
23 changes: 15 additions & 8 deletions recipe/src/module/type_ver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Deserializer, Serialize};
#[derive(Debug, Clone)]
pub struct ModuleTypeVersion<'scope> {
typ: Cow<'scope, str>,
version: Cow<'scope, str>,
version: Option<Cow<'scope, str>>,
}

impl ModuleTypeVersion<'_> {
Expand All @@ -15,14 +15,21 @@ impl ModuleTypeVersion<'_> {
}

#[must_use]
pub fn version(&self) -> &str {
&self.version
pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}
}

impl std::fmt::Display for ModuleTypeVersion<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}", &self.typ, &self.version)
match self.version.as_deref() {
Some(version) => {
write!(f, "{}@{version}", &self.typ)
}
None => {
write!(f, "{}", &self.typ)
}
}
}
}

Expand All @@ -31,12 +38,12 @@ impl<'scope> From<&'scope str> for ModuleTypeVersion<'scope> {
if let Some((typ, version)) = s.split_once('@') {
Self {
typ: Cow::Borrowed(typ),
version: Cow::Borrowed(version),
version: Some(Cow::Borrowed(version)),
}
} else {
Self {
typ: Cow::Borrowed(s),
version: Cow::Owned("latest".into()),
version: None,
}
}
}
Expand All @@ -47,12 +54,12 @@ impl From<String> for ModuleTypeVersion<'_> {
if let Some((typ, version)) = s.split_once('@') {
Self {
typ: Cow::Owned(typ.to_owned()),
version: Cow::Owned(version.to_owned()),
version: Some(Cow::Owned(version.to_owned())),
}
} else {
Self {
typ: Cow::Owned(s),
version: Cow::Owned("latest".into()),
version: None,
}
}
}
Expand Down
37 changes: 22 additions & 15 deletions src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,21 @@ impl BlueBuildCommand for ValidateCommand {
impl ValidateCommand {
async fn setup_validators(&mut self) -> Result<(), Report> {
let (rv, sv, mv, mslv) = tokio::try_join!(
SchemaValidator::builder().url(RECIPE_V1_SCHEMA_URL).build(),
SchemaValidator::builder().url(STAGE_V1_SCHEMA_URL).build(),
SchemaValidator::builder().url(MODULE_V1_SCHEMA_URL).build(),
SchemaValidator::builder()
.url(RECIPE_V1_SCHEMA_URL)
.all_errors(self.all_errors)
.build(),
SchemaValidator::builder()
.url(STAGE_V1_SCHEMA_URL)
.all_errors(self.all_errors)
.build(),
SchemaValidator::builder()
.url(MODULE_V1_SCHEMA_URL)
.all_errors(self.all_errors)
.build(),
SchemaValidator::builder()
.url(MODULE_STAGE_LIST_V1_SCHEMA_URL)
.all_errors(self.all_errors)
.build(),
)?;
self.recipe_validator = Some(rv);
Expand Down Expand Up @@ -149,15 +159,12 @@ impl ValidateCommand {

if instance.get(DF::LIST_KEY).is_some() {
debug!("{path_display} is a list file");
let err = match self
let err = self
.module_stage_list_validator
.as_ref()
.unwrap()
.process_validation(path, file_str.clone(), self.all_errors)
{
Err(e) => return vec![e],
Ok(e) => e,
};
.process_validation(path, file_str.clone())
.err();

err.map_or_else(
|| {
Expand Down Expand Up @@ -195,13 +202,13 @@ impl ValidateCommand {
},
)
},
|err| vec![err],
|err| vec![err.into()],
)
} else {
debug!("{path_display} is a single file file");
single_validator
.process_validation(path, file_str, self.all_errors)
.map_or_else(|e| vec![e], |e| e.map_or_else(Vec::new, |e| vec![e]))
.process_validation(path, file_str)
.map_or_else(|e| vec![e.into()], |()| Vec::new())
}
}
Err(e) => vec![e],
Expand All @@ -221,11 +228,11 @@ impl ValidateCommand {

let schema_validator = self.recipe_validator.as_ref().unwrap();
let err = schema_validator
.process_validation(&self.recipe, recipe_str.clone(), self.all_errors)
.map_err(err_vec)?;
.process_validation(&self.recipe, recipe_str.clone())
.err();

if let Some(err) = err {
Err(vec![err])
Err(vec![err.into()])
} else {
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
.into_diagnostic()
Expand Down
6 changes: 6 additions & 0 deletions src/commands/validate/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ impl From<&JsonLocation> for Location {
}
}

impl std::fmt::Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl TryFrom<&str> for Location {
type Error = miette::Report;

Expand Down
Loading

0 comments on commit 87e8e14

Please sign in to comment.