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

Val err details #12

Merged
merged 19 commits into from
Jul 9, 2021
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let mut u = Username::new(" valid name \n\n").unwrap();
assert_eq!(u.get(), "valid name"); // now we're talking!

// This also works for mutations:
assert!(matches!(u.try_mutate(|u| *u = " ".to_owned()), Err(prae::ValidationError)));
assert!(matches!(u.try_mutate(|u| *u = " ".to_owned()), Err(prae::ValidationError { .. })))
```

Now our `Username` trims provided value automatically.
Expand Down
1 change: 1 addition & 0 deletions prae/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ prae_macro = { version = "0.2", path = "../prae_macro" }
serde = { version = "1.0", optional = true }

[dev-dependencies]
assert_matches = "1.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
33 changes: 21 additions & 12 deletions prae/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
use core::hash::Hash;
use std::fmt;
use std::ops::{Deref, Index};
use std::{error::Error, fmt};

/// Used for [`define!`](prae_macro::define) macro with `ensure` keyword.
#[derive(Clone, Debug)]
pub struct ValidationError {
/// The name of the type where this error originated.
type_name: &'static str,
/// Stringified value that caused the error.
value: String,
}

/// Default validation error. It is used for [`define!`](prae_macro::define) macro with `ensure`
/// keyword.
#[derive(PartialEq)]
pub struct ValidationError;

impl std::error::Error for ValidationError {}

impl fmt::Debug for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "provided value is not valid")
impl ValidationError {
/// Create a new error with the input value that failed.
pub fn new(type_name: &'static str, value: String) -> Self {
ValidationError { type_name, value }
}
}

impl Error for ValidationError {}

impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "provided value is not valid")
write!(
f,
"failed to create {} from value {}: provided value is invalid",
self.type_name, self.value,
)
}
}

Expand Down
5 changes: 4 additions & 1 deletion prae/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//! assert_eq!(u.get(), "valid name"); // now we're talking!
//!
//! // This also works for mutations:
//! assert!(matches!(u.try_mutate(|u| *u = " ".to_owned()), Err(prae::ValidationError)));
//! assert!(matches!(u.try_mutate(|u| *u = " ".to_owned()), Err(prae::ValidationError { .. })));
//! ```
//! Now our `Username` trims provided value automatically.
//!
Expand Down Expand Up @@ -77,8 +77,11 @@ mod core;
pub use crate::core::*;
pub use prae_macro::define;

// We need this to silince the unused_crate_dependencies warning.
// See: https://github.com/rust-lang/rust/issues/57274
#[cfg(test)]
mod test_deps {
use assert_matches as _;
use serde as _;
use serde_json as _;
}
Comment on lines 82 to 87
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, why is this necessary?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the whole test_deps business

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the lint to warn about unused crate dependencies.

#![warn(unused_crate_dependencies)]

But it give's false positives when buliding the separate integration test binaries.

❯ cargo test
   Compiling prae_macro v0.2.1 (/home/brianh/Projects/third_party/prae/prae_macro)
   Compiling prae v0.4.5 (/home/brianh/Projects/third_party/prae/prae)
warning: external crate `assert_matches` unused in `prae`: remove the dependency or add `use assert_matches as _;`
   |
note: the lint level is defined here
  --> prae/src/lib.rs:71:9
   |
71 | #![warn(unused_crate_dependencies)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: remove unnecessary dependency `assert_matches`

warning: external crate `serde` unused in `prae`: remove the dependency or add `use serde as _;`
  |
  = help: remove unnecessary dependency `serde`

warning: external crate `serde_json` unused in `prae`: remove the dependency or add `use serde_json as _;`
  |
  = help: remove unnecessary dependency `serde_json`

warning: 3 warnings emitted

    Finished test [unoptimized + debuginfo] target(s) in 0.73s
     Running unittests (target/debug/deps/prae-dc495198ddde61f3)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I get it. I'll add a comment referencing the related issue: rust-lang/rust#57274

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 😉

15 changes: 10 additions & 5 deletions prae/tests/adjust_ensure.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use assert_matches::assert_matches;

use prae;

prae::define! {
Expand All @@ -8,7 +10,10 @@ prae::define! {

#[test]
fn construction_fails_for_invalid_data() {
assert_eq!(Username::new(" ").unwrap_err(), prae::ValidationError);
assert_matches!(
Username::new(" ").unwrap_err(),
prae::ValidationError { .. }
)
}

#[test]
Expand All @@ -20,10 +25,10 @@ fn construction_succeeds_for_valid_data() {
#[test]
fn mutation_fails_for_invalid_data() {
let mut un = Username::new("user").unwrap();
assert_eq!(
assert_matches!(
un.try_mutate(|u| *u = " ".to_owned()).unwrap_err(),
prae::ValidationError
);
prae::ValidationError { .. }
)
}

#[test]
Expand All @@ -38,4 +43,4 @@ fn mutation_succeeds_for_valid_data() {
let mut un = Username::new("user").unwrap();
assert!(un.try_mutate(|u| *u = " new user ".to_owned()).is_ok());
assert_eq!(un.get(), "new user");
}
}
10 changes: 6 additions & 4 deletions prae/tests/adjust_validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assert_matches::assert_matches;

use prae;

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub struct UsernameError;

prae::define! {
Expand All @@ -17,7 +19,7 @@ prae::define! {

#[test]
fn construction_fails_for_invalid_data() {
assert_eq!(Username::new(" ").unwrap_err(), UsernameError {});
assert_matches!(Username::new(" ").unwrap_err(), UsernameError {});
}

#[test]
Expand All @@ -29,7 +31,7 @@ fn construction_succeeds_for_valid_data() {
#[test]
fn mutation_fails_for_invalid_data() {
let mut un = Username::new("user").unwrap();
assert_eq!(
assert_matches!(
un.try_mutate(|u| *u = " ".to_owned()).unwrap_err(),
UsernameError {}
);
Expand All @@ -47,4 +49,4 @@ fn mutation_succeeds_for_valid_data() {
let mut un = Username::new("user").unwrap();
assert!(un.try_mutate(|u| *u = " new user ".to_owned()).is_ok());
assert_eq!(un.get(), "new user");
}
}
25 changes: 20 additions & 5 deletions prae/tests/ensure.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use assert_matches::assert_matches;

use prae;

prae::define! {
Expand All @@ -7,7 +9,20 @@ prae::define! {

#[test]
fn construction_fails_for_invalid_data() {
assert_eq!(Username::new("").unwrap_err(), prae::ValidationError);
assert_matches!(
Username::new("").unwrap_err(),
prae::ValidationError { .. }
)
}

#[test]
fn error_formats_correctly() {
let error = Username::new("").unwrap_err();
let message = format!("{}", error);
assert_eq!(
message,
"failed to create Username from value \"\": provided value is invalid"
);
}

#[test]
Expand All @@ -19,10 +34,10 @@ fn construction_succeeds_for_valid_data() {
#[test]
fn mutation_fails_for_invalid_data() {
let mut un = Username::new("user").unwrap();
assert_eq!(
assert_matches!(
un.try_mutate(|u| *u = "".to_owned()).unwrap_err(),
prae::ValidationError
);
prae::ValidationError { .. }
)
}

#[test]
Expand All @@ -37,4 +52,4 @@ fn mutation_succeeds_for_valid_data() {
let mut un = Username::new("user").unwrap();
assert!(un.try_mutate(|u| *u = " new user ".to_owned()).is_ok());
assert_eq!(un.get(), " new user ");
}
}
11 changes: 11 additions & 0 deletions prae/tests/non_clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Debug)]
struct User {
name: String,
}

prae::define! {
ValidUser: User
ensure |u| !u.name.is_empty()
}


3 changes: 2 additions & 1 deletion prae/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod adjust_ensure;
mod adjust_validate;
mod ensure;
mod validate;
mod non_clone;
mod validate;
10 changes: 6 additions & 4 deletions prae/tests/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assert_matches::assert_matches;

use prae;

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub struct UsernameError;

prae::define! {
Expand All @@ -16,7 +18,7 @@ prae::define! {

#[test]
fn construction_fails_for_invalid_data() {
assert_eq!(Username::new("").unwrap_err(), UsernameError {});
assert_matches!(Username::new("").unwrap_err(), UsernameError {});
}

#[test]
Expand All @@ -28,7 +30,7 @@ fn construction_succeeds_for_valid_data() {
#[test]
fn mutation_fails_for_invalid_data() {
let mut un = Username::new("user").unwrap();
assert_eq!(
assert_matches!(
un.try_mutate(|u| *u = "".to_owned()).unwrap_err(),
UsernameError {}
);
Expand All @@ -46,4 +48,4 @@ fn mutation_succeeds_for_valid_data() {
let mut un = Username::new("user").unwrap();
assert!(un.try_mutate(|u| *u = " new user ".to_owned()).is_ok());
assert_eq!(un.get(), " new user ");
}
}
2 changes: 1 addition & 1 deletion prae_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn define(input: TokenStream) -> TokenStream {
GuardClosure::Ensure(EnsureClosure(closure)) => quote! {
fn validate(v: &Self::Target) -> Option<prae::ValidationError> {
let f: fn(&Self::Target) -> bool = #closure;
if f(v) { None } else { Some(prae::ValidationError) }
if f(v) { None } else { Some(prae::ValidationError::new(stringify!(#ident), format!("{:?}", v))) }
}
},
GuardClosure::Validate(ValidateClosure(closure, err_ty)) => quote! {
Expand Down