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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/// Derive macro for the [`Model`] trait.
///
/// # Requirements
///
/// [`Model`](crate::Model) can *only* be derived for non-generic `struct`s with suitable named fields.
///
/// | Type | Example | Supported? |
/// | ---- | ------- | ---------- |
/// | Standard `struct` | `struct Person { name: String }` | ✔ |
/// | Generic `struct` | `struct Person<'a, T, const N: usize> { ... }` | ✘ |
/// | Tuple struct | `struct Point(i64, i64)` | ✘ |
/// | Unit/ZST struct | `struct Unit;` or `struct Unit {}` | ✘ |
/// | `enum`s | `enum Direction { Up, Down }` | ✘ |
/// | `union`s | `union Number { i: i32, f: f32 }` | ✘ |
///
/// (Note, however, that any non-supported type can be *used* in a [`Model`](crate::Model) assuming it meets the below requirements.)
///
/// All fields in a [`Model`](crate::Model) derivee must either:
/// - Implement [`ToSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.ToSql.html) and [`FromSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.FromSql.html). Most common types will meet this requirement.
/// - Have `#[bind]` and `#[extr]` [attributes](Model#attributes) on fields that do not meet the first requirement.
/// - This escape hatch is designed to enable compatibility with certain `std` types like [`PathBuf`](std::path::PathBuf) and third-party crate types.
///
/// # Usage
/// Most of the time, deriving [`Model`](crate::Model) is easy. The only thing you need to specify is the table name:
/// ```rust
/// # use exemplar::Model;
/// #[derive(Model)]
/// #[table("people")] // <-- Required
/// pub struct Person {
/// pub name: String,
/// pub age: u16,
/// }
/// ```
///
/// For more complicated types and schemas, you may need to make use of some of the [attributes](Model#attributes) recognized by the macro:
/// ```rust
/// # use std::path::{PathBuf, Path};
/// # use exemplar::Model;
/// #[derive(Model)]
/// #[table("users")]
/// #[check("schema.sql")]
/// struct User {
/// username: String,
/// #[bind(bind_path)]
/// #[extr(extr_path)]
/// home_dir: PathBuf,
/// #[column("pwd")]
/// password: Vec<u8>,
/// }
/// # fn bind_path(value: &Path) -> exemplar::BindResult { panic!() }
/// # fn extr_path(value: &rusqlite::types::ValueRef) -> exemplar::ExtrResult<PathBuf> { panic!() }
/// ```
///
/// # Attributes
/// The [`Model`](crate::Model) derive macro recognizes several attributes.
///
/// ### `#[check]`
/// Usage:
/// ```ignore
/// #[check("path_to_schema")]
/// pub struct T { ... }
/// ```
///
/// The `check` attribute automatically generates a test that checks the derived [`Model`](crate::Model) implementation against a provided schema.
///
/// More specifically, the generated test verifies that:
/// - The specified table exists.
/// - The columns in the schema match up with those specified by the model type. If the model has fields not present in the schema (or vice versa) the test will fail.
///
/// It does *not* verify the validity of column types, nor does it test actual insertion/retrieval.
///
/// ### `#[bind]`/`#[extr]`
/// Usage:
/// ```ignore
/// #[bind(path::to::fn)]
/// #[extr(path::to::fn)]
/// field: T,
/// ```
///
/// The `bind` and `extr` attributes specify functions used to convert the annotated field to and from an SQL-friendly representation.
/// This is primarily intended as an escape hatch for when you can't implement [`ToSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.ToSql.html) and [`FromSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.FromSql.html)
/// yourself.
///
/// Both attributes take as their argument a path to a free function used to do the conversion.
/// - For `bind`, the signature should be [`fn(&T) -> BindResult`](crate::BindResult).
/// - For `extr`, the signature should be [`fn(&ValueRef) -> ExtrResult<T>`](crate::ExtrResult).
///
/// In both cases `T` is the type of the field being annotated. For some types (e.g. `PathBuf`) you may also be able to use a type it derefs to, like `Path`.
///
/// Example implementations for `PathBuf`:
/// ```rust
/// # use exemplar::*;
/// # use std::path::{Path, PathBuf};
/// # use rusqlite::types::ValueRef;
/// pub fn bind_path(value: &Path) -> BindResult {
/// use rusqlite::types::Value;
/// use rusqlite::types::ToSqlOutput;
///
/// // Depending on your program, it may make more sense
/// // to error if a lossless conversion isn't possible.
/// let str = value.to_string_lossy().into_owned();
///
/// Ok(ToSqlOutput::Owned(
/// Value::Text(str)
/// ))
/// }
///
/// pub fn extr_path(value: &ValueRef) -> ExtrResult<PathBuf> {
/// let path = value.as_str()?;
/// let path = PathBuf::from(path);
///
/// Ok(path)
/// }
/// ```
///
/// ### `#[column]`
/// Usage:
/// ```ignore
/// #[column("name")]
/// field: T,
/// ```
///
/// The `column` attribute overrides the column name Exemplar maps the annotated field to. By default, the field name is assumed to directly map to the underlying schema - `#[column]` is how you alter this behavior.
///
/// # Notes
/// Any type that derives [`Model`](crate::Model) also has an implementation of [`TryFrom<Row>`] derived, making models usable in some generic contexts.
pub use Model;
/// Generate an "anonymous" record `struct` that implements `from_row`.
///
/// This is best used for deserializing rows from an ad-hoc query in a strongly typed manner.
///
/// Note that the generated struct does *not* implement [`Model`](crate::Model), as it can't be associated with any specific table.
/// This means that tools like `#[bind]`/`#[extr]` and the like are not available for records.
///
/// However, `TryFrom<Row>` is still implemented, making records usable in some generic contexts.
///
/// # Example
/// The example assumes this database schema:
/// ```sql
/// CREATE TABLE people (name, age, alive);
/// ```
///
/// ```ignore
/// record! {
/// // The provided field name is assumed to map directly to a column in a query's output.
/// name => String,
/// age => u16,
/// }
///
/// record! {
/// // By default, the generated struct is called `Record.`
/// // This can be overridden with the `Name` parameter, should the need arise.
/// Name => Age,
/// age => u16,
/// }
///
/// let mut get_people = conn.prepare("SELECT name, age FROM people")?;
/// let mut get_ages = conn.prepare("SELECT age FROM people")?;
///
/// get_people
/// .query_and_then([], Record::from_row)?
/// .map(|record| ...);
///
/// get_ages
/// .query_and_then([], Age::from_row)?
/// .map(|age| ...);
/// ```
///
/// # Notes
///
/// Doc comments (and other attributes) are supported:
/// ```rust
/// # use exemplar::*;
/// record! {
/// /// A person's name.
/// name => String,
/// /// A person's age.
/// age => u16,
/// }
/// ```
/// Additionally, you can apply type-level attributes like derives on the `Name` argument.
/// ```rust
/// # use exemplar::*;
/// record! {
/// #[derive(Debug, Clone)]
/// Name => Age,
/// age => u16,
/// }
/// ```
/// (`record!` does not apply any derives automatically.)
///
/// This does *not* work without the `Name` argument, due to macro limitations - Rust can't
/// disambiguate between "attributes for the struct" and "attributes for the field."
///
/// ```compile_fail
/// # use exemplar::*;
/// record! {
/// #[derive(Debug, Clone)]
/// /// A person's name.
/// name => String,
/// /// A person's age.
/// age => u16,
/// }
/// ```
};
=> ;
}
/// Generate an SQL-compatible field-less `enum`.
///
/// SQL compatible means:
/// - `#[repr(i*/u*)]`
/// - Implements [`TryFrom<i64>`].
/// - Implements [`ToSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.ToSql.html) and [`FromSql`](https://docs.rs/rusqlite/latest/rusqlite/types/trait.FromSql.html).
///
/// (Additionally, the standard constellation of `Debug`/`Clone`/`Copy`/`Eq`/`Hash` are derived.)
///
/// An enum generated by this macro can be used in any [`Model`](crate::Model) implementor.
///
/// # Usage
/// ```rust
/// # use exemplar::sql_enum;
/// sql_enum! {
/// Name => Color,
/// Red,
/// Green,
/// Blue,
/// };
/// ```
///
/// By default, `#[repr(i64)]` is used. The optional `Type` parameter can override this:
///
/// ```rust
/// # use exemplar::sql_enum;
/// sql_enum! {
/// Name => Color,
/// Type => u8,
/// Red,
/// Green,
/// Blue,
/// };
/// ```
///
/// # Notes
/// Explicit discriminants are *not* supported. Variants will always be implicitly numbered, in order of definition, from zero.
///
/// Concretely, this means that:
/// ```compile_fail
/// # use exemplar::*;
/// sql_enum! {
/// Name => Color,
/// Red = 1,
/// Green = 2,
/// Blue = 3
/// }
/// ```
/// ...will not compile.
///
/// <hr>
///
/// Doc comments (and other attributes, like derives) *are* supported:
/// ```rust
/// # use exemplar::sql_enum;
/// sql_enum! {
/// #[derive(Default)]
/// /// An RGB color tag.
/// Name => Color,
/// /// Red
/// #[default]
/// Red,
/// /// Green
/// Green,
/// /// Blue
/// Blue,
/// }
/// ```
};
=>
}