2 releases
Uses old Rust 2015
0.0.2 | Mar 29, 2018 |
---|---|
0.0.1 | Mar 29, 2018 |
#21 in #strings
13KB
214 lines
Conform
Rust macro to transform struct string fields in place.
Inspired by conform
package for Go.
ShakaCode
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!
Installation
Add to Cargo.toml
:
[dependencies]
conform = "x.x.x"
conform-derive = "x.x.x"
where x.x.x
is current version of crate.
Usage
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.
Usage with validator
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.
Transformations
trim
Removes leading and trailing whitespaces.
" foo " -> "foo"
trim_left
Removes leading whitespaces.
" foo " -> "foo "
trim_right
Removes trailing whitespaces.
" foo " -> " foo"
lower
Converts any case into lower case ignoring separators.
"Foo-Bar" -> "foo-bar"
upper
Converts any case into UPPER CASE ignoring separators.
"Foo-Bar" -> "FOO-BAR"
sentence
Converts any case into traditional sentence case without capitalizing the first letter.
"Foo Bar" -> "foo bar"
title
Converts any case into title case where every word is capitalized.
"foo bar" -> "Foo Bar"
camel
Converts any case into camelCase.
"foo bar" -> "fooBar"
pascal
Converts any case into PascalCase.
"foo bar" -> "FooBar"
kebab
Converts any case into kebab-case.
"Foo Bar" -> "foo-bar"
train
Converts any case into Train-Case.
"foo bar" -> "Foo-Bar"
snake
Converts any case into snake_case.
"Foo Bar" -> "foo_bar"
constant
Converts any case into CONSTANT_CASE.
"Foo Bar" -> "FOO_BAR"
Licence
MIT.
Dependencies
~2MB
~47K SLoC