JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Unified stack `GetValue` implementation

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨325f5af

⁨giterated-models/src/value.rs⁩ - ⁨1621⁩ bytes
Raw
1 use std::{fmt::Debug, marker::PhantomData};
2
3 use serde::{de::DeserializeOwned, Deserialize, Serialize};
4 use serde_json::Value;
5
6 use crate::{error::GetValueError, object::GiteratedObject, operation::GiteratedOperation};
7
8 pub trait GiteratedObjectValue: Send + Sync + Serialize + DeserializeOwned {
9 type Object: GiteratedObject;
10
11 fn value_name() -> &'static str;
12 }
13
14 #[derive(Serialize, Deserialize, Debug, Clone)]
15 pub struct GetValue {
16 pub value_name: String,
17 }
18
19 impl<O: GiteratedObject + Send> GiteratedOperation<O> for GetValue {
20 fn operation_name() -> &'static str {
21 "get_value"
22 }
23 type Success = Value;
24 type Failure = GetValueError;
25 }
26
27 #[derive(Serialize, Deserialize, Debug, Clone)]
28 pub struct GetValueTyped<V: GiteratedObjectValue> {
29 pub value_name: String,
30 pub ty: PhantomData<V>,
31 }
32
33 impl<O: GiteratedObject + Send, V: GiteratedObjectValue<Object = O>> GiteratedOperation<O>
34 for GetValueTyped<V>
35 {
36 fn operation_name() -> &'static str {
37 "get_value"
38 }
39 type Success = V;
40 type Failure = GetValueError;
41 }
42
43 #[derive(Debug, Clone, Deserialize, Serialize)]
44 #[serde(transparent)]
45 pub struct AnyValue<O> {
46 value: Value,
47 #[serde(skip)]
48 _marker: PhantomData<O>,
49 }
50
51 impl<O> AnyValue<O> {
52 pub unsafe fn from_raw(value: Value) -> Self {
53 Self {
54 value,
55 _marker: Default::default(),
56 }
57 }
58
59 pub fn into_inner(self) -> Value {
60 self.value
61 }
62 }
63
64 impl<O: GiteratedObject> GiteratedObjectValue for AnyValue<O> {
65 type Object = O;
66
67 fn value_name() -> &'static str {
68 todo!()
69 }
70 }
71