Skip to content

Commit ea7eb45

Browse files
committed
Transform some tuple structs to structs with named fields for serialization purposes
1 parent 3ec5457 commit ea7eb45

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/frontend/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,30 @@ pub enum PodClass {
3434

3535
// An Origin, which represents a reference to an ancestor POD.
3636
#[derive(Clone, Debug, PartialEq, Eq, h::Hash, Default, Serialize, Deserialize)]
37+
#[serde(into = "OriginSerdeHelper", from = "OriginSerdeHelper")]
3738
pub struct Origin(pub PodClass, pub PodId);
3839

40+
#[derive(Serialize, Deserialize)]
41+
struct OriginSerdeHelper {
42+
pod_class: PodClass,
43+
pod_id: PodId,
44+
}
45+
46+
impl From<Origin> for OriginSerdeHelper {
47+
fn from(origin: Origin) -> Self {
48+
OriginSerdeHelper {
49+
pod_class: origin.0,
50+
pod_id: origin.1,
51+
}
52+
}
53+
}
54+
55+
impl From<OriginSerdeHelper> for Origin {
56+
fn from(helper: OriginSerdeHelper) -> Self {
57+
Origin(helper.pod_class, helper.pod_id)
58+
}
59+
}
60+
3961
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
4062
pub enum Value {
4163
// Serde cares about the order of the enum variants, with untagged variants
@@ -239,6 +261,7 @@ impl SignedPod {
239261
}
240262

241263
#[derive(Clone, Debug, PartialEq, Eq, h::Hash, Serialize, Deserialize)]
264+
#[serde(into = "AnchoredKeySerdeHelper", from = "AnchoredKeySerdeHelper")]
242265
pub struct AnchoredKey(pub Origin, pub String);
243266

244267
impl From<AnchoredKey> for middleware::AnchoredKey {
@@ -247,6 +270,32 @@ impl From<AnchoredKey> for middleware::AnchoredKey {
247270
}
248271
}
249272

273+
// Tuple structs get serialized to bare JSON arrays, which is not very
274+
// user-friendly. This helper struct allows us to serialize AnchoredKeys
275+
// into a structure with named fields.
276+
#[derive(Serialize, Deserialize)]
277+
struct AnchoredKeySerdeHelper {
278+
origin: Origin,
279+
key: String,
280+
}
281+
282+
// Convert from AnchoredKey to the helper for serialization
283+
impl From<AnchoredKey> for AnchoredKeySerdeHelper {
284+
fn from(ak: AnchoredKey) -> Self {
285+
AnchoredKeySerdeHelper {
286+
origin: ak.0,
287+
key: ak.1,
288+
}
289+
}
290+
}
291+
292+
// Convert from the helper back to AnchoredKey for deserialization
293+
impl From<AnchoredKeySerdeHelper> for AnchoredKey {
294+
fn from(helper: AnchoredKeySerdeHelper) -> Self {
295+
AnchoredKey(helper.origin, helper.key)
296+
}
297+
}
298+
250299
#[derive(Debug)]
251300
pub struct MainPodBuilder {
252301
pub params: Params,

src/frontend/statement.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,30 @@ impl fmt::Display for StatementArg {
2020
}
2121

2222
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23+
#[serde(into = "StatementSerdeHelper", from = "StatementSerdeHelper")]
2324
pub struct Statement(pub Predicate, pub Vec<StatementArg>);
2425

26+
#[derive(Serialize, Deserialize)]
27+
struct StatementSerdeHelper {
28+
predicate: Predicate,
29+
args: Vec<StatementArg>,
30+
}
31+
32+
impl From<Statement> for StatementSerdeHelper {
33+
fn from(statement: Statement) -> Self {
34+
StatementSerdeHelper {
35+
predicate: statement.0,
36+
args: statement.1,
37+
}
38+
}
39+
}
40+
41+
impl From<StatementSerdeHelper> for Statement {
42+
fn from(helper: StatementSerdeHelper) -> Self {
43+
Statement(helper.predicate, helper.args)
44+
}
45+
}
46+
2547
impl From<(&SignedPod, &str)> for Statement {
2648
fn from((pod, key): (&SignedPod, &str)) -> Self {
2749
// TODO: TryFrom.

src/middleware/custom.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ impl CustomPredicateRef {
391391
}
392392

393393
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
394+
#[serde(tag = "type", content = "value")]
394395
pub enum Predicate {
395396
Native(NativePredicate),
396397
BatchSelf(usize),

0 commit comments

Comments
 (0)