1///|
2pub struct ChildType {
3  
Bool
multiple
:
Bool
Bool
4
Bool
required
:
Bool
Bool
5
Array[NodeType]
types
:
type Array[T]
Array
[
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
]
6} derive(
(ChildType) -> Json
ToJson
,
(ChildType, &Logger) -> Unit
Show
)
7 8///| 9impl
trait @json.FromJson {
  from_json(Json, @json.JsonPath) -> Self!@json.JsonDecodeError
}

Trait for types that can be converted from Json

@json.FromJson
for
struct ChildType {
  multiple: Bool
  required: Bool
  types: Array[NodeType]
}
ChildType
with
(json : Json, path : @json.JsonPath) -> ChildType!@json.JsonDecodeError
from_json
(
10
Json
json
:
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
,
11
@json.JsonPath
path
:
type @json.JsonPath
@json.JsonPath
12) ->
struct ChildType {
  multiple: Bool
  required: Bool
  types: Array[NodeType]
}
ChildType
!
type! @json.JsonDecodeError (@json.JsonPath, String)
@json.JsonDecodeError
{
13 match
Json
json
{
14 { 15 "multiple":
Json
True
16 |
Json
False
as multiple,
17 "required":
Json
True
18 |
Json
False
as required,
19 "types":
Json
types
,
20 .. 21 } => 22
struct ChildType {
  multiple: Bool
  required: Bool
  types: Array[NodeType]
}
ChildType
::{
23
Bool
multiple
:
(json : Json, path~ : @json.JsonPath) -> Bool!@json.JsonDecodeError
@json.from_json
!(
Json
multiple
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("multiple")),
24
Bool
required
:
(json : Json, path~ : @json.JsonPath) -> Bool!@json.JsonDecodeError
@json.from_json
!(
Json
required
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("required")),
25
Array[NodeType]
types
:
(json : Json, path~ : @json.JsonPath) -> Array[NodeType]!@json.JsonDecodeError
@json.from_json
!(
Json
types
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("types")),
26 } 27
Json
json
=> raise @json.
((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError
((
@json.JsonPath
path
, "Invalid child type: \{
Json
json
}"))
28 } 29} 30 31///| 32pub enum NodeTypePayload { 33
NodeTypePayload
Empty
34
(subtypes~ : Array[NodeType]) -> NodeTypePayload
Supertype
(
Array[NodeType]
subtypes
~ :
type Array[T]
Array
[
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
])
35
(fields~ : Map[String, ChildType], children~ : ChildType) -> NodeTypePayload
Normal
(
Map[String, ChildType]
fields
~ :
type Map[K, V]
Map
[
String
String
,
struct ChildType {
  multiple: Bool
  required: Bool
  types: Array[NodeType]
}
ChildType
],
ChildType
children
~ :
struct ChildType {
  multiple: Bool
  required: Bool
  types: Array[NodeType]
}
ChildType
)
36} derive(
(NodeTypePayload, &Logger) -> Unit
Show
)
37 38///| 39pub struct NodeType { 40
String
type_
:
String
String
41
Bool
named
:
Bool
Bool
42
NodeTypePayload
payload
:
enum NodeTypePayload {
  Empty
  Supertype(subtypes~ : Array[NodeType])
  Normal(fields~ : Map[String, ChildType], children~ : ChildType)
}
NodeTypePayload
43} derive(
(NodeType, &Logger) -> Unit
Show
)
44 45///| 46pub impl
trait @json.FromJson {
  from_json(Json, @json.JsonPath) -> Self!@json.JsonDecodeError
}

Trait for types that can be converted from Json

@json.FromJson
for
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
with
(json : Json, path : @json.JsonPath) -> NodeType!@json.JsonDecodeError
from_json
(
47
Json
json
:
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
,
48
@json.JsonPath
path
:
type @json.JsonPath
@json.JsonPath
49) ->
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
!
type! @json.JsonDecodeError (@json.JsonPath, String)
@json.JsonDecodeError
{
50 match
Json
json
{
51 { 52 "type":
(String) -> Json
String
(
String
type_
),
53 "named":
Json
True
54 |
Json
False
as named,
55 "subtypes":
Json
subtypes
,
56 .. 57 } => 58
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
::{
59
String
type_
,
60
Bool
named
:
(json : Json, path~ : @json.JsonPath) -> Bool!@json.JsonDecodeError
@json.from_json
!(
Json
named
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("named")),
61
NodeTypePayload
payload
: NodeTypePayload::
(subtypes~ : Array[NodeType]) -> NodeTypePayload
Supertype
(
62
Array[NodeType]
subtypes
=
(json : Json, path~ : @json.JsonPath) -> Array[NodeType]!@json.JsonDecodeError
@json.from_json
!(
Json
subtypes
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("subtypes")),
63 ), 64 } 65 { 66 "type":
(String) -> Json
String
(
String
type_
),
67 "named":
Json
True
68 |
Json
False
as named,
69 "fields":
Json
fields
,
70 "children":
Json
children
,
71 .. 72 } => 73
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
::{
74
String
type_
,
75
Bool
named
:
(json : Json, path~ : @json.JsonPath) -> Bool!@json.JsonDecodeError
@json.from_json
!(
Json
named
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("named")),
76
NodeTypePayload
payload
: NodeTypePayload::
(fields~ : Map[String, ChildType], children~ : ChildType) -> NodeTypePayload
Normal
(
77
Map[String, ChildType]
fields
=
(json : Json, path~ : @json.JsonPath) -> Map[String, ChildType]!@json.JsonDecodeError
@json.from_json
!(
Json
fields
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("fields")),
78
ChildType
children
=
(json : Json, path~ : @json.JsonPath) -> ChildType!@json.JsonDecodeError
@json.from_json
!(
Json
children
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("children")),
79 ), 80 } 81 { "type":
(String) -> Json
String
(
String
type_
), "named":
Json
True
Json
|
Json
False
Json
as named
, .. } =>
82
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
::{
83
String
type_
,
84
Bool
named
:
(json : Json, path~ : @json.JsonPath) -> Bool!@json.JsonDecodeError
@json.from_json
!(
Json
named
,
@json.JsonPath
path
=
@json.JsonPath
path
.
(self : @json.JsonPath, key : String) -> @json.JsonPath
add_key
("named")),
85
NodeTypePayload
payload
: NodeTypePayload::
NodeTypePayload
Empty
,
86 } 87
Json
json
=> raise @json.
((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError
((
@json.JsonPath
path
, "Invalid node type: \{
Json
json
}"))
88 } 89} 90 91///| 92pub impl
trait ToJson {
  to_json(Self) -> Json
}
ToJson
for
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
with
(self : NodeType) -> Json
to_json
(
NodeType
self
:
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
) ->
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
{
93 let
Json
type_
=
NodeType
self
.
String
type_
.
(self : String) -> Json
to_json
()
94 let
Json
named
=
NodeType
self
.
Bool
named
.
(self : Bool) -> Json
to_json
()
95 match
NodeType
self
.
NodeTypePayload
payload
{
96
(Array[NodeType]) -> NodeTypePayload
Supertype
(
Array[NodeType]
subtypes
~) => {
97 let
Json
subtypes
=
Array[NodeType]
subtypes
.
(self : Array[NodeType]) -> Json
to_json
()
98 { "type":
Json
type_
, "named":
Json
named
, "subtypes":
Json
subtypes
}
99 } 100
NodeTypePayload
Empty
=> { "type":
Json
type_
, "named":
Json
named
}
101
(Map[String, ChildType], ChildType) -> NodeTypePayload
Normal
(
Map[String, ChildType]
fields
~,
ChildType
children
~) => {
102 let
Json
fields
=
Map[String, ChildType]
fields
.
(self : Map[String, ChildType]) -> Json
to_json
()
103 let
Json
children
=
ChildType
children
.
(ChildType) -> Json

automatically derived

to_json
()
104 { "type":
Json
type_
, "named":
Json
named
, "fields":
Json
fields
, "children":
Json
children
}
105 } 106 } 107} 108 109///| 110pub type NodeTypes
type Array[T]
Array
[
struct NodeType {
  type_: String
  named: Bool
  payload: NodeTypePayload
}
NodeType
] derive(
(Json, @json.JsonPath) -> NodeTypes!@json.JsonDecodeError

Trait for types that can be converted from Json

@json.FromJson
,
(NodeTypes) -> Json
ToJson
,
(NodeTypes, &Logger) -> Unit
Show
)
111 112///| 113pub fn
type NodeTypes Array[NodeType]
NodeTypes
::
(json : Json) -> NodeTypes!@json.JsonDecodeError
from_json
(
Json
json
:
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
) ->
type NodeTypes Array[NodeType]
NodeTypes
!
type! @json.JsonDecodeError (@json.JsonPath, String)
@json.JsonDecodeError
{
114
(json : Json, path~ : @json.JsonPath = ..) -> NodeTypes!@json.JsonDecodeError
@json.from_json
!(
Json
json
)
115} 116