Skip to main content

cratestack_core/
json.rs

1//! Schema-declared `Json` columns need a model-struct field type that's the
2//! same on every backend so the same struct compiles on server and on
3//! embedded (including `wasm32-unknown-unknown`, which can't depend on sqlx).
4//!
5//! This newtype lives in `cratestack-core` so every backend can reference it
6//! without introducing a cyclic dep. `cratestack-sqlx` provides the sqlx
7//! `Type` / `Encode` / `Decode` impls on native targets.
8
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct Json<T>(pub T);
14
15impl<T> Json<T> {
16    pub fn into_inner(self) -> T {
17        self.0
18    }
19}
20
21impl<T> std::ops::Deref for Json<T> {
22    type Target = T;
23    fn deref(&self) -> &T {
24        &self.0
25    }
26}
27
28impl<T> std::ops::DerefMut for Json<T> {
29    fn deref_mut(&mut self) -> &mut T {
30        &mut self.0
31    }
32}
33
34impl<T> From<T> for Json<T> {
35    fn from(value: T) -> Self {
36        Json(value)
37    }
38}