Michael Zhang 2 weeks ago
parent
commit
216d9f7ccd
Signed by: Michael Zhang <iptq@protonmail.com> GPG Key ID: 5BAEFE5D04F0CE6C

+ 0
- 1
content/2018-02-01-my-new-life-stack.md View File

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

+ 0
- 1
content/2018-02-25-cleaning-up-your-shell.md View File

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

+ 0
- 1
content/2018-04-23-fixing-tmux-colors.md View File

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

+ 0
- 1
content/2018-05-28-web-apps.md View File

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

+ 0
- 1
content/2018-10-18-weechat-relay.md View File

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

+ 0
- 1
content/2018-10-26-twenty-years-of-rsa-attacks.md View File

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

+ 84
- 0
content/2019-02-01-magic-forms-with-proc-macros.md View File

@@ -0,0 +1,84 @@
1
++++
2
+title = "magic forms with proc macros: ideas"
3
+date = 2019-02-01
4
+
5
+tags = ["rust"]
6
++++
7
+
8
+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
9
+
10
+```rs
11
+#[derive(Serialize)]
12
+struct Foo {
13
+    bar: String,
14
+}
15
+```
16
+
17
+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.
18
+
19
+In this post I'm going to outline some of the ideas I have for a wtforms-ish library for handling forms in Rust.
20
+
21
+## code generation
22
+
23
+Ideally, we should be able to use this library like this:
24
+
25
+```rs
26
+#[derive(Form)]
27
+struct RegisterForm {
28
+    #[validators(email, custom("not_taken"))]
29
+    id: Email,
30
+    #[validators(required, length(4, 12))]
31
+    name: String,
32
+    #[validators(required, length(8, 128))]
33
+    pass: Password,
34
+}
35
+```
36
+
37
+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):
38
+
39
+```html
40
+<form>
41
+    <input type="email" name="id" />
42
+    <input type="text" name="name" />
43
+    <input type="password" name="pass" />
44
+    <input type="submit" />
45
+</form>
46
+```
47
+
48
+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:
49
+
50
+```rs
51
+#[validators(required, length(4, 12))]
52
+#[attrs = "autocomplete=off"]
53
+name: String,
54
+```
55
+
56
+This should generate the following HTML:
57
+
58
+```html
59
+<input type="text" name="name" autocomplete=off />
60
+```
61
+
62
+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?
63
+
64
+## validation
65
+
66
+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):
67
+
68
+```rs
69
+let instance = RegisterForm {
70
+    id: Email("michael@example.com"),
71
+    name: "michael",
72
+    pass: Password("pass"),
73
+}
74
+```
75
+
76
+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.
77
+
78
+## other interesting features
79
+
80
+- 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.
81
+
82
+## conclusion
83
+
84
+This project is a work in progress! You can see how far I am [on Github](https://github.com/iptq/wtforms).

+ 2
- 3
content/_index.md View File

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

+ 1
- 1
templates/layout.html View File

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

Loading…
Cancel
Save