1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Procedural macros for Renox. Use them through the `renox` crate:
//! `#[derive(Model, FromRow, DbEnum, Validate)]`, `renox::migrations!()`,
//! `renox::embedded!()` and `#[renox::test]`.
use TokenStream;
use ;
/// Implements `renox::db::Model` for a struct with named fields.
///
/// ```
/// # use renox::prelude::*;
/// # use serde::Serialize;
/// #[derive(Model, Serialize, Default)]
/// #[model(table = "products", soft_deletes)]
/// struct Product {
/// id: i64,
/// name: String,
/// #[model(skip)]
/// label: String, // not a column; filled with Default when loading
/// created_at: Option<DateTime>,
/// updated_at: Option<DateTime>,
/// deleted_at: Option<DateTime>,
/// }
/// ```
///
/// - `table` defaults to the struct name in snake_case (no pluralisation).
/// - An `id` field is required; its type is the key (`i64`, `Ulid`, `Uuid` or
/// `String`, see `renox::db::ModelKey`).
/// - `created_at` / `updated_at` fields are filled on save.
/// - `soft_deletes` needs a `deleted_at: Option<DateTime>` field.
/// - `search = "title, body"` names the text columns full-text search looks
/// in (`Model::search`, `renox::db::search`), most important first;
/// `search_language = "simple"` changes the language from `english`.
/// - `default_scope = "path::to::fn"` and `hooks`: see `renox::db::Model`.
/// Implements `renox::Validate` from `#[validate(…)]` attributes on the
/// fields, for forms that need only rules:
///
/// ```
/// # use renox::prelude::*;
/// #[derive(serde::Deserialize, Validate)]
/// struct Signup {
/// #[validate(required, max = 100, label = "Full name")]
/// name: String,
/// #[validate(required, email, unique("users", "email"))]
/// email: String,
/// #[validate(required, min = 8, confirmed(&self.password_confirmation))]
/// password: String,
/// password_confirmation: String,
/// #[validate(max = 5, each(required, max = 20), distinct)]
/// tags: Vec<String>,
/// #[validate(rename = "t-shirt", one_of(&["S", "M", "L"]))]
/// size: String,
/// }
/// ```
///
/// Each item is a call on the field's rules, in order: `required` is
/// `.required()`, `max = 100` is `.max(100)`, `unique("users", "email")` is
/// `.unique("users", "email")`, so every rule of `renox::validation::Field`
/// works, and arguments may use `self`. Three are special: `each(…)` applies
/// rules to each item of a list (errors `tags.0`, …), `distinct` refuses
/// repeated items, and `rename = "…"` names the field as the form does. For
/// `prepare`, `authorize` and `after`, add `#[validate(hooks)]` on the struct
/// and `impl renox::validation::ValidateHooks`. Anything else: implement
/// `Validate` by hand.
/// Implements `renox::db::FromRow`, so `sql(…).fetch_as::<T>()` can read
/// rows of any query (joins, aggregates, a few columns) into the struct.
///
/// ```
/// # use renox::prelude::*;
/// # use serde::Serialize;
/// #[derive(FromRow, Serialize)]
/// struct ProductRow {
/// id: i64,
/// name: String,
/// #[row(rename = "category_name")]
/// category: Option<String>,
/// #[row(skip)]
/// note: String, // Default
/// }
/// ```
///
/// `derive(Model)` implements `FromRow` too.
/// A fieldless enum stored as text: in the database (a `TEXT` column), in
/// forms (`<select>`), in JSON and in templates. Variants are stored in
/// snake_case (`OnHold` → `on_hold`) unless renamed with `#[db(rename = "…")]`.
///
/// Generates `as_str()`, `ALL` (every variant, e.g. for a `<select>`),
/// `Display`, `FromStr`, `Serialize`, `Deserialize`, `ToDbValue`, and
/// decoding on every database, so the enum can be a model field.
///
/// ```
/// # use renox::prelude::*;
/// #[derive(DbEnum, Debug, Clone, Copy, PartialEq, Default)]
/// enum Status {
/// #[default]
/// Draft,
/// Published,
/// #[db(rename = "hidden")]
/// Archived,
/// }
///
/// assert_eq!(Status::Archived.as_str(), "hidden");
/// assert_eq!("published".parse::<Status>().unwrap(), Status::Published);
/// assert_eq!(Status::ALL.len(), 3);
/// ```
/// Embeds the SQL migrations of a directory (default `migrations`, relative
/// to the crate root) as `&'static [renox::db::Migration]`.
///
/// Files are `<timestamp>_<name>.up.sql` with an optional matching
/// `.down.sql`, or a plain `<timestamp>_<name>.sql` that can't be rolled back.
/// Add a `build.rs` with `println!("cargo:rerun-if-changed=migrations");` so
/// new files are picked up (`rnx new` creates it).
/// Marks an async test, like `#[tokio::test]`, using the Tokio that Renox
/// re-exports, so apps don't need `tokio` as a dependency.
///
/// ```
/// # use renox::prelude::*;
/// # use renox::testing::TestApp;
/// #[renox::test]
/// async fn home_page() {
/// let app = TestApp::new(App::new()).await;
/// app.get("/health").await.assert_ok();
/// }
/// ```
/// Embeds `resources/views`, `resources/lang` and `public` in the binary, so
/// a release build runs from a single file:
///
/// ```
/// # use renox::prelude::*;
/// # let _ =
/// App::new().embed(renox::embedded!())
/// # ;
/// ```
///
/// Files are read from disk while `APP_DEBUG` is on (templates reload), and
/// from the binary otherwise. Add `cargo:rerun-if-changed=resources` and
/// `=public` to `build.rs` so new files are picked up (`rnx new` does).