Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
What is prae?
This crate aims to provide a better way to define types that require
validation. prae is not a validation library, but a library that
helps developers to define validation-requiring types with very little
effort.
How it works?
The main way to use prae is through define! macro.
For example, suppose you want to create a Username type. You want this
type to be a string, and you don't want it to be empty. Traditionally, would
create a wrapper struct with getter and setter functions, like this
simplified example:
;
let username = new.unwrap;
assert_eq!;
let err = new.unwrap_err;
assert_eq!;
Using prae, you will do it like this:
use define;
define!
let username = new.unwrap;
assert_eq!;
let err = new.unwrap_err;
assert_eq!;
assert_eq!;
Futhermore, prae allows you to use custom errors and extend your types.
See docs for define! for more information and examples.
Additional features
*_unprocessed functions
By default, all methods of the wrappers generated by
define! (which are just aliases of the
Bounded type) will run the adjustment/validation (if
present) routines on every construction and mutation.
If you find yourself in a situation where you know for sure that some
construction/mutation is valid, you can opt out of this using
*_unprocessed functions (e.g. foo.set_unprocessed(value) instead of
foo.set(value)) and save a bit of computations.
To be able to use these functions, just enable the unprocessed
feature of the crate.
Serde integration
You can enable serde integration with the serde feature. It will implement
Serialize and Deserialize
traits for the wrappers if their inner type implements them. The
deserialization will automatically fail if it contains invalid value. Here
is an example:
use ;
use define;
define!
// Serialization works as expected.
let u = User ;
let j = to_string.unwrap;
assert_eq!;
// Deserialization with invalid data fails.
let e = .unwrap_err;
assert_eq!;
// And here we get a nice adjusted value.
let u = .unwrap;
assert_eq!;
Drawbacks
Although proc macros are very powerful, they aren't free. In this case, you
have to pull up additional dependencies such as syn and
quote, and expect a slightly slower compile times.