Crates.io | preprocess |
lib.rs | preprocess |
version | 0.5.7 |
source | src |
created_at | 2022-07-25 07:45:11.802321 |
updated_at | 2024-08-02 05:22:52.807197 |
description | Preprocesses a struct with built-in preprocessors |
homepage | https://docs.rs/preprocess |
repository | https://github.com/patr-cloud/preprocess |
max_upload_size | |
id | 632418 |
size | 54,533 |
A crate to help you preprocess your structs and enums. Can be used to validate data, or to transform it.
There are two kinds of preprocessors:
use preprocess::prelude::*;
#[preprocess::sync]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UserSignUpRequest {
// First trims the email, then converts it to lowercase, then validates it as an email address.
#[preprocess(trim, lowercase, email)]
pub email: String,
// First trims the password, then checks if it's at least 8 characters long.
#[preprocess(trim, length(min = 8))]
pub password: String,
}
let processed_value = raw_value.preprocess()?;
Since the crate uses an attribute macro, it must always be the first attribute on the struct or enum. A new struct / enum will be generated with the name {original_name}Processed
. The derive macro will inherit all the derive attributes from the original struct / enum. For example:
use preprocess::prelude::*;
#[preprocess::sync]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserSignUpRequest {
#[preprocess(trim, lowercase, email)]
#[serde(default)]
pub email: String,
#[serde(alias = "pass")]
#[preprocess(trim, length(min = 8))]
pub password: String,
}
The above code will generate:
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserSignUpRequestProcessed {
#[serde(default)]
pub email: String,
#[serde(alias = "pass")]
pub password: String,
}
This way, any custom derive attributes you use (like Serde) will be inherited by the generated struct / enum. This also ensures that you can preprocess your struct / enum and send the preprocessed version to the client, without having to write any extra code.
More details about the crate can be found in the documentation.
There is no MSRV as such, and to be honest, I don't see the point of an MSRV, with how easy rust is to upgrade. I just use the latest version of rust on my machine. That being said, I don't think I've used any new rust features. So it should work on older versions of rust as well. Please open an issue if you're facing any, well, issues.
This crate is largely inspired by the validator
crate. A huge thanks to Keats for creating it.
This project is licensed under the MIT license.