toasty 0.11.0

An async ORM for Rust supporting SQL and NoSQL databases
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use super::{Expr, IntoExpr, List, Path, Value as StmtValue};
use crate::schema::{Field, Load};
use serde_json::Value as JsonValue;
use toasty_core::schema::app::{FieldPrimitive, FieldTy, SerializeFormat};
use toasty_core::{schema::db, stmt};

use std::fmt;

fn load_json<T>(value: stmt::Value) -> crate::Result<T>
where
    T: for<'de> serde_core::Deserialize<'de>,
{
    let json = <String as Load>::load(value)?;
    serde_json::from_str(&json).map_err(|e| {
        toasty_core::Error::from_args(format_args!("failed to deserialize JSON field: {e}"))
    })
}

fn json_field_ty(storage_ty: Option<db::Type>, missing_type_message: &'static str) -> FieldTy {
    FieldTy::Primitive(FieldPrimitive {
        ty: stmt::Type::String,
        storage_ty: Some(storage_ty.expect(missing_type_message)),
        serialize: Some(SerializeFormat::Json),
    })
}

fn json_expr<T>(value: &(impl serde_core::Serialize + ?Sized)) -> Expr<T> {
    let json = serde_json::to_string(value).expect("failed to serialize JSON field");
    Expr::<String>::from_value(StmtValue::from(json)).cast()
}

/// A field wrapper that serializes `T` as JSON in a database column.
///
/// Use `Json<T>` as a model field type when the column has no native database
/// representation for `T` — for example, a serde-derived struct, a `HashMap`,
/// or any other type that round-trips through
/// [`serde::Serialize`](serde::Serialize) and
/// [`serde::Deserialize`](serde::Deserialize). The value is JSON-encoded on
/// insert and update, and decoded on read.
///
/// Every `Json<T>` field must select its database column type with
/// `#[column(type = ...)]`. Use `text` or `varchar(...)` for text-backed JSON,
/// `json` for PostgreSQL or MySQL native JSON, and `jsonb` for PostgreSQL JSONB.
/// A field whose Rust type is already [`serde_json::Value`] does not need the
/// `Json<T>` wrapper and supports the same column types.
///
/// # Two nullable variants
///
/// `Json<T>` composes with `Option` in two distinct, both-useful ways:
///
/// | Field type           | `None` is stored as      |
/// |----------------------|--------------------------|
/// | `Option<Json<T>>`    | SQL `NULL`               |
/// | `Json<Option<T>>`    | JSON literal `"null"`    |
///
/// # Setter ergonomics
///
/// The crate provides an `IntoExpr<Json<T>>` impl for any
/// `T: serde::Serialize`, so create / update setters and the `create!`
/// macro accept the bare inner value without an explicit `Json(...)`
/// wrapper. Both forms produce the same expression:
///
/// ```ignore
/// // for a `payload: Json<Payload>` (or `Deferred<Json<Payload>>`) field
/// Repository::create().payload(my_payload.clone()).exec(&mut db).await?;
/// Repository::create().payload(Json(my_payload.clone())).exec(&mut db).await?;
/// ```
///
/// The `Json(...)` form is still useful when type inference needs a
/// nudge (e.g., comparison expressions like `.eq(Json("hello"))`).
///
/// # Composition with `Deferred`
///
/// `Json<T>` is the only wrapper allowed inside [`Deferred`](crate::Deferred)
/// for a serde-typed field, since a deferred column needs both lazy
/// loading and a serializer the macro can drive through trait dispatch:
///
/// ```ignore
/// #[derive(Debug, toasty::Model)]
/// struct Repository {
///     #[key] #[auto]
///     id: uuid::Uuid,
///     #[column(type = text)]
///     schema: toasty::Deferred<toasty::Json<MySchema>>,
/// }
/// ```
///
/// # Examples
///
/// ```ignore
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// struct Tags(Vec<String>);
///
/// #[derive(Debug, toasty::Model)]
/// struct Item {
///     #[key] #[auto]
///     id: i64,
///     #[column(type = text)]
///     tags: toasty::Json<Tags>,
/// }
/// ```
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Json<T>(pub T);

impl<T> From<T> for Json<T> {
    fn from(value: T) -> Self {
        Json(value)
    }
}

impl<T> std::ops::Deref for Json<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> std::ops::DerefMut for Json<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> AsRef<T> for Json<T> {
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T> AsMut<T> for Json<T> {
    fn as_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> Load for Json<T>
where
    T: for<'de> serde_core::Deserialize<'de>,
{
    type Output = Self;

    fn ty() -> stmt::Type {
        stmt::Type::String
    }

    fn load(value: stmt::Value) -> crate::Result<Self> {
        // SQL `NULL` never reaches here: a nullable JSON column is typed
        // `Option<Json<T>>`, and `Option<T>: Load` intercepts `Value::Null`
        // before delegating to the inner type. `Json<Option<T>>` stores the
        // JSON literal `"null"` as a non-null string, so its `None` case
        // comes through as `Value::String("null")` and `serde_json` decodes
        // it. A bare `Value::Null` at this point is a driver bug.
        load_json(value).map(Json)
    }

    fn reload(target: &mut Self, value: stmt::Value) -> crate::Result<()> {
        *target = Self::load(value)?;
        Ok(())
    }
}

impl<T> Field for Json<T>
where
    T: serde_core::Serialize + for<'de> serde_core::Deserialize<'de>,
{
    const REQUIRES_EXPLICIT_COLUMN_TYPE: bool = true;

    type ExprTarget = Self;
    type Path<Origin> = Path<Origin, Self>;
    type ListPath<Origin> = Path<Origin, List<Self::ExprTarget>>;
    type Update<'a> = ();
    type Inner = Self;

    fn new_path<Origin>(path: Path<Origin, Self>) -> Self::Path<Origin> {
        path
    }

    fn new_list_path<Origin>(path: Path<Origin, List<Self::ExprTarget>>) -> Self::ListPath<Origin> {
        path
    }

    fn new_update<'a>(
        _assignments: &'a mut toasty_core::stmt::Assignments,
        _projection: toasty_core::stmt::Projection,
    ) -> Self::Update<'a> {
    }

    /// Tag the schema entry as JSON-serialized with explicit storage.
    ///
    /// The macro never inspects the wrapper at the AST level; the
    /// `serialize: Some(Json)` metadata flows through trait dispatch
    /// alongside the storage type, so external schema consumers can still
    /// see that the column is JSON-typed even though the encoding itself is
    /// invisible to the macro.
    fn field_ty(storage_ty: Option<db::Type>) -> FieldTy {
        json_field_ty(
            storage_ty,
            "`toasty::Json<T>` fields require `#[column(type = ...)]`; use \
             `#[column(type = text)]` for text-backed JSON storage",
        )
    }

    fn key_constraint<Origin>(&self, _target: Path<Origin, Self::Inner>) -> Expr<bool> {
        // JSON columns are not valid foreign-key targets — the comparison
        // would happen at the serialized-string level, which is rarely what
        // the user wants. The trait method exists for every `Field`; we
        // satisfy it with a panic rather than admitting nonsense semantics.
        unreachable!("Json<T> fields cannot be used as foreign-key targets")
    }
}

impl<T> IntoExpr<Json<T>> for Json<T>
where
    T: serde_core::Serialize,
{
    fn into_expr(self) -> Expr<Json<T>> {
        json_expr(&self.0)
    }

    fn by_ref(&self) -> Expr<Json<T>> {
        json_expr(&self.0)
    }
}

// `IntoExpr<Json<T>>` for `&Json<T>` comes from the blanket
// `impl<T: IntoExpr<T>> IntoExpr<T> for &T` in `stmt::into_expr`.

/// Accept a bare `T` wherever the API expects `IntoExpr<Json<T>>`, so
/// callers don't have to spell `Json(value)` at setter sites:
///
/// ```ignore
/// // both forms work for a `payload: Json<Payload>` field
/// Repository::create().payload(Json(payload.clone())).exec(&mut db).await?;
/// Repository::create().payload(payload.clone()).exec(&mut db).await?;
/// ```
///
/// The blanket only fires when `T: serde::Serialize`; it doesn't overlap
/// the explicit `IntoExpr<Json<T>> for Json<T>` impl because `Json<U>`
/// itself is not `Serialize` (no derive on the wrapper).
impl<T> IntoExpr<Json<T>> for T
where
    T: serde_core::Serialize,
{
    fn into_expr(self) -> Expr<Json<T>> {
        json_expr(&self)
    }

    fn by_ref(&self) -> Expr<Json<T>> {
        json_expr(self)
    }
}

/// Accept a borrowed `T` wherever the API expects `IntoExpr<Json<T>>`, so a
/// caller that still needs the value afterwards doesn't have to clone it.
/// The value is serialized out of the borrow.
///
/// This doesn't overlap the `IntoExpr<Json<T>> for T` blanket: unifying the
/// two would require `T == &T`.
impl<T> IntoExpr<Json<T>> for &T
where
    T: serde_core::Serialize,
{
    fn into_expr(self) -> Expr<Json<T>> {
        json_expr(self)
    }

    fn by_ref(&self) -> Expr<Json<T>> {
        json_expr(*self)
    }
}

/// Same, for the explicit `Json(&value)` form.
impl<T> IntoExpr<Json<T>> for Json<&T>
where
    T: serde_core::Serialize,
{
    fn into_expr(self) -> Expr<Json<T>> {
        json_expr(self.0)
    }

    fn by_ref(&self) -> Expr<Json<T>> {
        json_expr(self.0)
    }
}

/// `Json(&value)` on an `Option<Json<T>>` field, mirroring the
/// `IntoExpr<Option<T>> for T` blanket that lets the owned `Json(value)`
/// skip the `Some(...)`.
impl<T> IntoExpr<Option<Json<T>>> for Json<&T>
where
    T: serde_core::Serialize,
{
    fn into_expr(self) -> Expr<Option<Json<T>>> {
        json_expr::<Json<T>>(self.0).cast()
    }

    fn by_ref(&self) -> Expr<Option<Json<T>>> {
        json_expr::<Json<T>>(self.0).cast()
    }
}

impl<T> super::assignment::Assign<Json<T>> for Json<T>
where
    T: serde_core::Serialize,
{
    fn into_assignment(self) -> super::assignment::Assignment<Json<T>> {
        super::set(<Self as IntoExpr<Json<T>>>::into_expr(self))
    }
}

/// Mirrors the `IntoExpr<Json<T>> for T` blanket on the assignment side so
/// update builders accept a bare value too:
///
/// ```ignore
/// repo.update().payload(payload.clone()).exec(&mut db).await?;
/// ```
impl<T> super::assignment::Assign<Json<T>> for T
where
    T: serde_core::Serialize,
{
    fn into_assignment(self) -> super::assignment::Assignment<Json<T>> {
        super::set(<Self as IntoExpr<Json<T>>>::into_expr(self))
    }
}

/// Mirrors the borrowed `IntoExpr` impls on the assignment side, so the
/// update builders take a reference too.
impl<T> super::assignment::Assign<Json<T>> for &T
where
    T: serde_core::Serialize,
{
    fn into_assignment(self) -> super::assignment::Assignment<Json<T>> {
        super::set(<Self as IntoExpr<Json<T>>>::into_expr(self))
    }
}

/// Same, for the explicit `Json(&value)` form.
impl<T> super::assignment::Assign<Json<T>> for Json<&T>
where
    T: serde_core::Serialize,
{
    fn into_assignment(self) -> super::assignment::Assignment<Json<T>> {
        super::set(<Self as IntoExpr<Json<T>>>::into_expr(self))
    }
}

/// `Json(&value)` assigned to an `Option<Json<T>>` field.
impl<T> super::assignment::Assign<Option<Json<T>>> for Json<&T>
where
    T: serde_core::Serialize,
{
    fn into_assignment(self) -> super::assignment::Assignment<Option<Json<T>>> {
        super::set(<Self as IntoExpr<Option<Json<T>>>>::into_expr(self))
    }
}

impl Load for JsonValue {
    type Output = Self;

    fn ty() -> stmt::Type {
        stmt::Type::String
    }

    fn load(value: stmt::Value) -> crate::Result<Self> {
        load_json(value)
    }

    fn reload(target: &mut Self, value: stmt::Value) -> crate::Result<()> {
        *target = Self::load(value)?;
        Ok(())
    }
}

impl Field for JsonValue {
    const REQUIRES_EXPLICIT_COLUMN_TYPE: bool = true;

    type ExprTarget = Self;
    type Path<Origin> = Path<Origin, Self>;
    type ListPath<Origin> = Path<Origin, List<Self::ExprTarget>>;
    type Update<'a> = ();
    type Inner = Self;

    fn new_path<Origin>(path: Path<Origin, Self>) -> Self::Path<Origin> {
        path
    }

    fn new_list_path<Origin>(path: Path<Origin, List<Self::ExprTarget>>) -> Self::ListPath<Origin> {
        path
    }

    fn new_update<'a>(
        _assignments: &'a mut toasty_core::stmt::Assignments,
        _projection: toasty_core::stmt::Projection,
    ) -> Self::Update<'a> {
    }

    fn field_ty(storage_ty: Option<db::Type>) -> FieldTy {
        json_field_ty(
            storage_ty,
            "`serde_json::Value` fields require `#[column(type = ...)]`; use \
             `#[column(type = text)]` for text-backed JSON storage",
        )
    }

    fn key_constraint<Origin>(&self, _target: Path<Origin, Self::Inner>) -> Expr<bool> {
        unreachable!("serde_json::Value fields cannot be used as foreign-key targets")
    }
}

impl IntoExpr<JsonValue> for JsonValue {
    fn into_expr(self) -> Expr<JsonValue> {
        json_expr(&self)
    }

    fn by_ref(&self) -> Expr<JsonValue> {
        json_expr(self)
    }
}

impl super::assignment::Assign<JsonValue> for JsonValue {
    fn into_assignment(self) -> super::assignment::Assignment<JsonValue> {
        super::set(<Self as IntoExpr<JsonValue>>::into_expr(self))
    }
}

impl<T: fmt::Display> fmt::Display for Json<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T> serde_core::Serialize for Json<T>
where
    T: serde_core::Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde_core::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de, T> serde_core::Deserialize<'de> for Json<T>
where
    T: serde_core::Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde_core::Deserializer<'de>,
    {
        T::deserialize(deserializer).map(Json)
    }
}