Skip to content

Commit

Permalink
Polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jul 28, 2024
1 parent f4a0034 commit 26986dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
9 changes: 4 additions & 5 deletions crates/macros/src/config/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ impl<'l> Field<'l> {

stmts.push(quote! {
if let Err(mut error) = #func(setting, self, context, finalize) {
error.prepend_path(path.join_key(#key_quoted));
errors.push(error);
errors.push(error.prepend_path(path.join_key(#key_quoted)));
}
});
}
Expand All @@ -155,9 +154,9 @@ impl<'l> Field<'l> {
let second = if self.is_required() {
quote! {
if finalize && self.#key.is_none() {
let mut error = schematic::ValidateError::required();
error.prepend_path(path.join_key(#key_quoted));
errors.push(error);
errors.push(schematic::ValidateError::required().prepend_path(
path.join_key(#key_quoted)
));
}
}
} else {
Expand Down
13 changes: 6 additions & 7 deletions crates/macros/src/config/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl<'l> Variant<'l> {

Some(self.map_unnamed_match(self.name, fields, |outer_names, _| {
let mut stmts = vec![];
let name = self.get_name(Some(&self.casing_format));

if let Some(expr) = self.args.validate.as_ref() {
let func = match expr {
Expand All @@ -168,20 +169,18 @@ impl<'l> Variant<'l> {
};

stmts.push(quote! {
if let Err(error) = #func(#value, self, context, finalize) {
errors.push(error);
if let Err(mut error) = #func(#value, self, context, finalize) {
errors.push(error.prepend_path(path.join_key(#name)));
}
});
}

if self.is_required() {
let name = self.get_name(Some(&self.casing_format));

stmts.push(quote! {
if finalize && [#(#outer_names),*].iter().any(|v| v.is_none()) {
let mut error = schematic::ValidateError::required();
error.prepend_path(path.join_key(#name));
errors.push(error);
errors.push(schematic::ValidateError::required().prepend_path(
path.join_key(#name)
));
}
});
}
Expand Down
8 changes: 6 additions & 2 deletions crates/schematic/src/config/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ impl ValidateError {
}
}

pub fn prepend_path(&mut self, path: Path) {
self.path = path.join_path(&self.path);
#[doc(hidden)]
pub fn prepend_path(self, path: Path) -> Self {
Self {
message: self.message,
path: path.join_path(&self.path),
}
}
}

Expand Down

0 comments on commit 26986dc

Please sign in to comment.