This commit is contained in:
Michael Zhang 2019-02-01 04:22:11 -06:00
parent 4fe3e9b18d
commit 216d9f7ccd
No known key found for this signature in database
GPG key ID: 5BAEFE5D04F0CE6C
9 changed files with 87 additions and 10 deletions

View file

@ -1,6 +1,5 @@
+++
title = "my new life stack"
template = "post.html"
date = 2018-02-01
tags = ["life", "technology"]

View file

@ -1,6 +1,5 @@
+++
title = "cleaning up your shell"
template = "post.html"
date = 2018-02-25
tags = ["technology", "shell"]

View file

@ -1,6 +1,5 @@
+++
title = "fixing tmux colors"
template = "post.html"
date = 2018-04-23
tags = ["technology", "shell"]

View file

@ -1,6 +1,5 @@
+++
title = "web apps"
template = "post.html"
date = 2018-05-28
tags = ["technology", "opinion"]

View file

@ -1,6 +1,5 @@
+++
title = "setting up irc with weechat"
template = "post.html"
date = 2018-10-18
tags = ["technology", "irc"]

View file

@ -1,6 +1,5 @@
+++
title = "twenty years of attacks on rsa.. with examples!"
template = "post.html"
date = 2018-10-26
tags = ["rsa", "cryptography"]

View file

@ -0,0 +1,84 @@
+++
title = "magic forms with proc macros: ideas"
date = 2019-02-01
tags = ["rust"]
+++
Procedural macros (proc macros for short) in Rust are incredible because they allow pre-compile source transformation. Many of the greatest abstractions in Rust take advantage of this feature. For example, you can
```rs
#[derive(Serialize)]
struct Foo {
bar: String,
}
```
It occurred to me that this feature can also be useful for generating code for rendering and validating forms (as in a place where you fill out info). **wtforms** is one of the nicest Python packages for handling form behavior in web applications, and with the power of proc macros, this functionality can be easily achieved in Rust as well.
In this post I'm going to outline some of the ideas I have for a wtforms-ish library for handling forms in Rust.
## code generation
Ideally, we should be able to use this library like this:
```rs
#[derive(Form)]
struct RegisterForm {
#[validators(email, custom("not_taken"))]
id: Email,
#[validators(required, length(4, 12))]
name: String,
#[validators(required, length(8, 128))]
pass: Password,
}
```
What this would do is add a couple more functions to our form class. Firstly, I'd like to render an HTML version of the above form. Calling something like `RegisterForm::html()` should produce the following HTML (prettified here for convenience):
```html
<form>
<input type="email" name="id" />
<input type="text" name="name" />
<input type="password" name="pass" />
<input type="submit" />
</form>
```
If we were to want to customize our form in any way, for example, adding more attributes to the elements, we would just attach that as a separate attribute onto the field:
```rs
#[validators(required, length(4, 12))]
#[attrs = "autocomplete=off"]
name: String,
```
This should generate the following HTML:
```html
<input type="text" name="name" autocomplete=off />
```
I realize this is probably not very flexible, since you'd really only be able to use this form in a specific context. But in reality, how much do you really lose by redefining that form?
## validation
You've already seen the `validators` attribute used above. This defines a set of validators that we'd like to verify the form against. Suppose you receive an instance of the form that looks like (in pseudo-y Rust):
```rs
let instance = RegisterForm {
id: Email("michael@example.com"),
name: "michael",
pass: Password("pass"),
}
```
then calling something like `instance.verify()` should run all those validators we've defined on the fields and return a list of errors that go along with each of the fields. For this instance, for example, we should at least get an error that states that the password provided was way too short.
## other interesting features
- If a form fails during validation, the user is presented with the errors and a chance to retry the form. At this point, the HTML generated should fill in the values for the fields that passed the validation so the user doesn't have to fill it out again. You see this behavior on web forms sometimes.
## conclusion
This project is a work in progress! You can see how far I am [on Github](https://github.com/iptq/wtforms).

View file

@ -1,8 +1,7 @@
+++
title = "michael's blog"
title = "home"
template = "blog.html"
page_template = "post.html"
sort_by = "date"
paginate_by = 10
paginate_path = "page"
+++

View file

@ -18,7 +18,7 @@
</div>
<div style="text-align: center; font-weight: 200; margin-bottom: 40px;">
written by Michael Zhang.
written by michael zhang.
<a href="https://git.mzhang.me/michael/blog" class="colorlink" target="_blank">source</a>
</div>
</body>