Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve validation errors #295

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 174 additions & 173 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
46 changes: 27 additions & 19 deletions src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use std::{

use blue_build_process_management::ASYNC_RUNTIME;
use blue_build_recipe::{FromFileList, ModuleExt, Recipe, StagesExt};
use blue_build_utils::constants::{
MODULE_STAGE_LIST_V1_SCHEMA_URL, MODULE_V1_SCHEMA_URL, RECIPE_V1_SCHEMA_URL,
STAGE_V1_SCHEMA_URL,
};
use bon::Builder;
use clap::Args;
use colored::Colorize;
use log::{debug, info, trace};
use miette::{bail, miette, Context, IntoDiagnostic, Report};
use rayon::prelude::*;
use schema_validator::{
SchemaValidator, MODULE_STAGE_LIST_V1_SCHEMA_URL, MODULE_V1_SCHEMA_URL, RECIPE_V1_SCHEMA_URL,
STAGE_V1_SCHEMA_URL,
};
use schema_validator::SchemaValidator;
use serde::de::DeserializeOwned;
use serde_json::Value;

Expand Down Expand Up @@ -97,11 +98,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 +160,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 +203,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 +229,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