Skip to content

Commit

Permalink
Merge pull request #91 from jprochazk/newtype-empty-display
Browse files Browse the repository at this point in the history
Remove empty colon from display impl of `Report`
  • Loading branch information
jprochazk authored Jan 27, 2024
2 parents 7ccf734 + 3055d4b commit 291d042
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
6 changes: 5 additions & 1 deletion garde/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ impl Report {
impl std::fmt::Display for Report {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (path, error) in self.iter() {
writeln!(f, "{path}: {error}")?;
if path.is_empty() {
writeln!(f, "{error}")?;
} else {
writeln!(f, "{path}: {error}")?;
}
}
Ok(())
}
Expand Down
24 changes: 22 additions & 2 deletions garde/tests/rules/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ struct NonEmptyStr_Struct<'a> {
v: &'a str,
}

#[test]
fn newtype_struct_valid() {
util::check_ok(&[NonEmptyStr_Struct { v: "test" }], &());
}

#[test]
fn newtype_struct_invalid() {
util::check_fail!(&[NonEmptyStr_Struct { v: "" }], &());
}

#[derive(Debug, garde::Validate)]
#[garde(transparent)]
struct NonEmptyStr_Tuple<'a>(#[garde(length(min = 1))] &'a str);

#[test]
fn newtype_tuple_valid() {
util::check_ok(&[NonEmptyStr_Tuple("test")], &());
}

#[test]
fn newtype_tuple_invalid() {
util::check_fail!(&[NonEmptyStr_Tuple("")], &());
}

#[derive(Debug, garde::Validate)]

struct Test<'a> {
Expand All @@ -23,7 +43,7 @@ struct Test<'a> {
}

#[test]
fn newtype_valid() {
fn newtype_fields_valid() {
util::check_ok(
&[Test {
a: NonEmptyStr_Struct { v: "test" },
Expand All @@ -34,7 +54,7 @@ fn newtype_valid() {
}

#[test]
fn newtype_invalid() {
fn newtype_fields_invalid() {
util::check_fail!(
&[Test {
a: NonEmptyStr_Struct { v: "" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: garde/tests/./rules/newtype.rs
expression: snapshot
---
Test {
a: NonEmptyStr_Struct {
v: "",
},
b: NonEmptyStr_Tuple(
"",
),
}
a: length is lower than 1
b: length is lower than 1


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: garde/tests/./rules/newtype.rs
expression: snapshot
---
NonEmptyStr_Struct {
v: "",
}
length is lower than 1


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: garde/tests/./rules/newtype.rs
expression: snapshot
---
NonEmptyStr_Tuple(
"",
)
length is lower than 1


8 changes: 3 additions & 5 deletions garde/tests/rules/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn check_ok<T: Validate + Debug>(cases: &[T], ctx: &T::Context) {
"{} input: {case:?}, errors: [{}]",
"FAIL".red(),
report
.iter()
.map(|(path, error)| format!("{path}: {error}"))
.to_string()
.split('\n')
.collect::<Vec<_>>()
.join("; ")
);
Expand All @@ -34,9 +34,7 @@ pub fn __check_fail<T: Validate + Debug>(cases: &[T], ctx: &T::Context) -> Strin
for case in cases {
if let Err(report) = case.validate(ctx) {
writeln!(&mut snapshot, "{case:#?}").unwrap();
for (path, error) in report.iter() {
writeln!(&mut snapshot, "{path}: {error}").unwrap();
}
write!(&mut snapshot, "{report}").unwrap();
writeln!(&mut snapshot).unwrap();
} else {
eprintln!("{} input: {case:?}", "SUCCESS".red());
Expand Down

0 comments on commit 291d042

Please sign in to comment.