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
//! Derive macros for [fastauth](https://github.com/lassejlv/fastauth). Use them through
//! `fastauth` or `fastauth_core` (feature `macros`), which re-export each derive next to the
//! trait it implements.
//!
//! The generated code names `fastauth_core` when the crate depends on it directly, and
//! `fastauth::__private::core` when it only depends on the `fastauth` facade.
use TokenStream;
use ;
use TokenStream as TokenStream2;
use ;
use ;
/// Implements `Fields` and `OnTable` for a struct of columns added to an existing table,
/// plus a typed `Col` constant per field (`two_factor_enabled` becomes
/// `TWO_FACTOR_ENABLED`).
///
/// ```
/// use fastauth_core::plugin_prelude::*;
///
/// #[derive(Fields, Clone)]
/// #[fields(table = User)]
/// pub struct TwoFactorUser {
/// #[field(default = false)]
/// pub two_factor_enabled: bool,
/// }
///
/// let _: Where<User> = TwoFactorUser::TWO_FACTOR_ENABLED.eq(true);
/// ```
///
/// `#[fields(table = ..)]` names the model the columns live on, `User` when omitted.
/// Field attributes, all inside `#[field(..)]`:
///
/// - `default = ..`: a bool, integer or string literal, or `now` for timestamps.
/// - `input`: sign-up and update-user bodies may set it. Without it, clients never can.
/// `input = false` spells out the default.
/// - `returned = false`: never serialized to clients.
/// - `unique`, `index`.
/// - `rename = ".."`: the column name, when it differs from the field name.
///
/// `Option<T>` fields are nullable; the column type is `<T as ColumnValue>::TYPE`.
/// Implements `Model` and `OnTable<Table = Self>` for a table, plus a typed `Col` constant
/// per field.
///
/// ```
/// use fastauth_core::plugin_prelude::*;
///
/// #[derive(Model, Clone)]
/// #[model(table = "api_key")]
/// pub struct ApiKey {
/// #[model(id)]
/// pub id: Id,
/// #[model(references = User, on_delete = Cascade, index)]
/// pub user_id: Id,
/// #[model(unique, hidden)]
/// pub key_hash: Hash,
/// #[model(default = now)]
/// pub created_at: OffsetDateTime,
/// }
///
/// let _: Where<ApiKey> = ApiKey::KEY_HASH.eq(Hash::sha256("ak_123"));
/// ```
///
/// `#[model(table = "..")]` is required. Field attributes, all inside `#[model(..)]`:
///
/// - `id`: the primary key.
/// - `references = Model`: a foreign key to that model's `id` column, with
/// `on_delete = Cascade | SetNull | Restrict` (default `Restrict`).
/// - `unique`, `index`, `hidden` (never serialized to clients).
/// - `default = ..`: as for `Fields`.
///
/// Every field is a column; `created_at` and `updated_at` are not added for you. Encoding
/// clones each field, so field types must be `Clone`.
/// Implements `From<E> for AuthError` and `ErrorCodes` for an enum of plugin errors.
///
/// ```
/// use fastauth_core::plugin_prelude::*;
///
/// #[derive(AuthError, Debug)]
/// #[auth_error(prefix = "API_KEY")]
/// pub enum ApiKeyError {
/// #[auth_error(status = 404, message = "API key not found")]
/// NotFound,
/// #[auth_error(status = 403, message = "scope {scope} is not allowed")]
/// ScopeDenied { scope: String },
/// }
///
/// let err = AuthError::from(ApiKeyError::ScopeDenied { scope: "admin".into() });
/// assert_eq!(err.code(), "API_KEY_SCOPE_DENIED");
/// assert_eq!(err.message(), "scope admin is not allowed");
/// ```
///
/// Each variant's code is the prefix and the variant name in `SCREAMING_SNAKE_CASE`
/// (`API_KEY_NOT_FOUND`); without a prefix it is the variant name alone. Messages can name
/// fields of struct-like variants in `{field}` placeholders, formatted with `Display`.