Rust macro to transform struct string fields in place.
Inspired by conform
package for Go.
If you are looking for help with the development and optimization of your project, ShakaCode can help you to take the reliability and performance of your app to the next level.
If you are a developer interested in working on Rust / ReScript / TypeScript / Ruby on Rails projects, we're hiring!
Add to Cargo.toml
:
[dependencies]
conform = "x.x.x"
conform-derive = "x.x.x"
where x.x.x
is current version of crate.
Example:
extern crate conform;
#[macro_use]
extern crate conform_derive;
use conform::Conform;
#[derive(Conform, Debug)]
struct User {
#[conform(trim)]
name: String,
#[conform(trim, lower)]
email: Option<String>,
}
fn main() {
let mut user = User {
name: " Alex Fedoseev ".to_string(),
email: " Alex.Fedoseev@gmail.com".to_string(),
};
user.conform();
println!("{:?}", user);
// Prints: User { name: "Alex Fedoseev", email: "alex.fedoseev@gmail.com" }
}
conform
works only on fields of String
or Option<String>
type.
let
binding must be mutable.
You can use conform
in conjunction with validator
:
match user.conform().validate() {
Ok(_) => /* save `user` to DB */,
Err(err) => /* handle validation `err` */,
}
See full example.
Removes leading and trailing whitespaces.
" foo " -> "foo"
Removes leading whitespaces.
" foo " -> "foo "
Removes trailing whitespaces.
" foo " -> " foo"
Converts any case into lower case ignoring separators.
"Foo-Bar" -> "foo-bar"
Converts any case into UPPER CASE ignoring separators.
"Foo-Bar" -> "FOO-BAR"
Converts any case into traditional sentence case without capitalizing the first letter.
"Foo Bar" -> "foo bar"
Converts any case into title case where every word is capitalized.
"foo bar" -> "Foo Bar"
Converts any case into camelCase.
"foo bar" -> "fooBar"
Converts any case into PascalCase.
"foo bar" -> "FooBar"
Converts any case into kebab-case.
"Foo Bar" -> "foo-bar"
Converts any case into Train-Case.
"foo bar" -> "Foo-Bar"
Converts any case into snake_case.
"Foo Bar" -> "foo_bar"
Converts any case into CONSTANT_CASE.
"Foo Bar" -> "FOO_BAR"
MIT.