1///|
2/// Response is a Message used as a reply to a call Request.
3/// It will have the same ID as the call it is a response to.
4pub(all) struct Response {
5  // id of the request this is a response to.
6  
ID
id
:
enum ID {
  Number(Int64)
  String(String)
}

ID is used to identify the request and tie it to the response.

ID
7 // result is the content of the response or an error. 8
Result[Json, WireError]
result
:
enum Result[A, B] {
  Err(B)
  Ok(A)
}
Result
[
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
,
struct WireError {
  code: Int
  message: String
  data: Json?
}

WireError is a jsonrpc2 error message.

WireError
]
9} derive(
(Response, &Logger) -> Unit

Trait for types that can be converted to String

Show
,
(Response, Response) -> Bool

Trait for types whose elements can test for equality

Eq
)
10 11///| 12/// WireError is a jsonrpc2 error message. 13pub(all) struct WireError { 14
Int
code
:
Int
Int
15
String
message
:
String
String
16
Json?
data
:
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
?
17} derive(
(WireError, &Logger) -> Unit

Trait for types that can be converted to String

Show
,
(WireError, WireError) -> Bool

Trait for types whose elements can test for equality

Eq
,
(Json, @json.JsonPath) -> WireError!@json.JsonDecodeError

Trait for types that can be converted from Json

FromJson
,
(WireError) -> Json

Trait for types that can be converted to Json

ToJson
)
18 19///| 20pub impl
trait ToJson {
  to_json(Self) -> Json
}

Trait for types that can be converted to Json

ToJson
for
struct Response {
  id: ID
  result: Result[Json, WireError]
}

Response is a Message used as a reply to a call Request. It will have the same ID as the call it is a response to.

Response
with
(self : Response) -> Json
to_json
(
Response
self
) {
21 let
Map[String, Json]
obj
= { "jsonrpc": "2.0".
(self : String) -> Json
to_json
() }
22 match
Response
self
.
ID
id
{
23
(String) -> ID
String
(
String
s
) =>
Map[String, Json]
obj
["id"] =
String
s
.
(self : String) -> Json
to_json
()
24
(Int64) -> ID
Number
(
Int64
n
) =>
Map[String, Json]
obj
["id"] =
Int64
n
.
(self : Int64) -> Double

Converts a 64-bit signed integer to a double-precision floating-point number.

Parameters:

  • self : The 64-bit signed integer to be converted.

Returns a double-precision floating-point number that represents the same value as the input integer.

Example:

test "Int64::to_double" {
  let big = 9223372036854775807L // max value of Int64
  inspect(big.to_double(), content="9223372036854776000")
  let neg = -42L
  inspect(neg.to_double(), content="-42")
}
to_double
().
(self : Double) -> Json
to_json
()
25 } 26 match
Response
self
.
Result[Json, WireError]
result
{
27
(Json) -> Result[Json, WireError]
Ok
(
Json
result
) =>
Map[String, Json]
obj
["result"] =
Json
result
28
(WireError) -> Result[Json, WireError]
Err
(
WireError
err
) =>
Map[String, Json]
obj
["error"] =
WireError
err
.
(WireError) -> Json

automatically derived

to_json
()
29 } 30
Map[String, Json]
obj
.
(self : Map[String, Json]) -> Json
to_json
()
31} 32 33///| 34pub 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 Response {
  id: ID
  result: Result[Json, WireError]
}

Response is a Message used as a reply to a call Request. It will have the same ID as the call it is a response to.

Response
with
(json : Json, path : @json.JsonPath) -> Response!@json.JsonDecodeError
from_json
(
Json
json
,
@json.JsonPath
path
) {
35 guard
Json
json
is
(Map[String, Json]) -> Json
Object
(
Map[String, Json]
obj
) else {
36 raise @json.
((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError
((
@json.JsonPath
path
, "expected object"))
37 } 38 match
Map[String, Json]
obj
{
39 { "jsonrpc":
(String) -> Json
String
("2.0"), "id":
(Double) -> Json
Number
(
Double
id
), "result":
Json
result
, .. } =>
40 {
ID
id
:
(Int64) -> ID
Number
(
Double
id
.
(self : Double) -> Int64

Converts a double-precision floating-point number to a 64-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 64-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int64.max_value (9223372036854775807L) if the input is greater than or equal to @int64.max_value
  • Returns @int64.min_value (-9223372036854775808L) if the input is less than or equal to @int64.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int64" {
  inspect(42.0.to_int64(), content="42")
  inspect((-42.5).to_int64(), content="-42")
  inspect((0.0 / 0.0).to_int64(), content="0") // NaN
  inspect((1.0 / 0.0).to_int64(), content="9223372036854775807") // Infinity
  inspect((-1.0 / 0.0).to_int64(), content="-9223372036854775808") // -Infinity
}
to_int64
()),
Result[Json, WireError]
result
:
(Json) -> Result[Json, WireError]
Ok
(
Json
result
) }
41 { "jsonrpc":
(String) -> Json
String
("2.0"), "id":
(String) -> Json
String
(
String
id
), "result":
Json
result
, .. } =>
42 {
ID
id
:
(String) -> ID
String
(
String
id
),
Result[Json, WireError]
result
:
(Json) -> Result[Json, WireError]
Ok
(
Json
result
) }
43 { 44 "jsonrpc":
(String) -> Json
String
("2.0"),
45 "id":
(Double) -> Json
Number
(
Double
id
),
46 "error": { 47 "code":
(Double) -> Json
Number
(
Double
code
),
48 "message":
(String) -> Json
String
(
String
message
),
49 "data":
Json
data
,
50 .. 51 }, 52 .. 53 } => 54 { 55
ID
id
:
(Int64) -> ID
Number
(
Double
id
.
(self : Double) -> Int64

Converts a double-precision floating-point number to a 64-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 64-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int64.max_value (9223372036854775807L) if the input is greater than or equal to @int64.max_value
  • Returns @int64.min_value (-9223372036854775808L) if the input is less than or equal to @int64.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int64" {
  inspect(42.0.to_int64(), content="42")
  inspect((-42.5).to_int64(), content="-42")
  inspect((0.0 / 0.0).to_int64(), content="0") // NaN
  inspect((1.0 / 0.0).to_int64(), content="9223372036854775807") // Infinity
  inspect((-1.0 / 0.0).to_int64(), content="-9223372036854775808") // -Infinity
}
to_int64
()),
56
Result[Json, WireError]
result
:
(WireError) -> Result[Json, WireError]
Err
({
Int
code
:
Double
code
.
(self : Double) -> Int

Converts a double-precision floating-point number to a 32-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 32-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int.max_value (2147483647) if the input is greater than or equal to @int.max_value
  • Returns @int.min_value (-2147483648) if the input is less than or equal to @int.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int/normal" {
  inspect(42.0.to_int(), content="42")
  inspect((-42.5).to_int(), content="-42")
  inspect((0.0 / 0.0).to_int(), content="0") // NaN
  inspect((1.0 / 0.0).to_int(), content="2147483647") // Infinity
  inspect((-1.0 / 0.0).to_int(), content="-2147483648") // -Infinity
}
to_int
(),
String
message
,
Json?
data
:
(Json) -> Json?
Some
(
Json
data
) }),
57 } 58 { 59 "jsonrpc":
(String) -> Json
String
("2.0"),
60 "id":
(Double) -> Json
Number
(
Double
id
),
61 "error": { "code":
(Double) -> Json
Number
(
Double
code
), "message":
(String) -> Json
String
(
String
message
), .. },
62 .. 63 } => 64 { 65
ID
id
:
(Int64) -> ID
Number
(
Double
id
.
(self : Double) -> Int64

Converts a double-precision floating-point number to a 64-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 64-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int64.max_value (9223372036854775807L) if the input is greater than or equal to @int64.max_value
  • Returns @int64.min_value (-9223372036854775808L) if the input is less than or equal to @int64.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int64" {
  inspect(42.0.to_int64(), content="42")
  inspect((-42.5).to_int64(), content="-42")
  inspect((0.0 / 0.0).to_int64(), content="0") // NaN
  inspect((1.0 / 0.0).to_int64(), content="9223372036854775807") // Infinity
  inspect((-1.0 / 0.0).to_int64(), content="-9223372036854775808") // -Infinity
}
to_int64
()),
66
Result[Json, WireError]
result
:
(WireError) -> Result[Json, WireError]
Err
({
Int
code
:
Double
code
.
(self : Double) -> Int

Converts a double-precision floating-point number to a 32-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 32-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int.max_value (2147483647) if the input is greater than or equal to @int.max_value
  • Returns @int.min_value (-2147483648) if the input is less than or equal to @int.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int/normal" {
  inspect(42.0.to_int(), content="42")
  inspect((-42.5).to_int(), content="-42")
  inspect((0.0 / 0.0).to_int(), content="0") // NaN
  inspect((1.0 / 0.0).to_int(), content="2147483647") // Infinity
  inspect((-1.0 / 0.0).to_int(), content="-2147483648") // -Infinity
}
to_int
(),
String
message
,
Json?
data
:
Json?
None
}),
67 } 68 { 69 "jsonrpc":
(String) -> Json
String
("2.0"),
70 "id":
(String) -> Json
String
(
String
id
),
71 "error": { 72 "code":
(Double) -> Json
Number
(
Double
code
),
73 "message":
(String) -> Json
String
(
String
message
),
74 "data":
Json
data
,
75 .. 76 }, 77 .. 78 } => 79 { 80
ID
id
:
(String) -> ID
String
(
String
id
),
81
Result[Json, WireError]
result
:
(WireError) -> Result[Json, WireError]
Err
({
Int
code
:
Double
code
.
(self : Double) -> Int

Converts a double-precision floating-point number to a 32-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 32-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int.max_value (2147483647) if the input is greater than or equal to @int.max_value
  • Returns @int.min_value (-2147483648) if the input is less than or equal to @int.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int/normal" {
  inspect(42.0.to_int(), content="42")
  inspect((-42.5).to_int(), content="-42")
  inspect((0.0 / 0.0).to_int(), content="0") // NaN
  inspect((1.0 / 0.0).to_int(), content="2147483647") // Infinity
  inspect((-1.0 / 0.0).to_int(), content="-2147483648") // -Infinity
}
to_int
(),
String
message
,
Json?
data
:
(Json) -> Json?
Some
(
Json
data
) }),
82 } 83 { 84 "jsonrpc":
(String) -> Json
String
("2.0"),
85 "id":
(String) -> Json
String
(
String
id
),
86 "error": { "code":
(Double) -> Json
Number
(
Double
code
), "message":
(String) -> Json
String
(
String
message
), .. },
87 .. 88 } => 89 { 90
ID
id
:
(String) -> ID
String
(
String
id
),
91
Result[Json, WireError]
result
:
(WireError) -> Result[Json, WireError]
Err
({
Int
code
:
Double
code
.
(self : Double) -> Int

Converts a double-precision floating-point number to a 32-bit integer. Handles special cases including NaN and numbers outside the valid Int range.

Parameters:

  • self : The double-precision floating-point number to be converted.

Returns an 32-bit integer value according to the following rules:

  • Returns 0 if the input is NaN
  • Returns @int.max_value (2147483647) if the input is greater than or equal to @int.max_value
  • Returns @int.min_value (-2147483648) if the input is less than or equal to @int.min_value
  • Otherwise returns the integer part of the input by truncating towards zero

Example:

test "Double::to_int/normal" {
  inspect(42.0.to_int(), content="42")
  inspect((-42.5).to_int(), content="-42")
  inspect((0.0 / 0.0).to_int(), content="0") // NaN
  inspect((1.0 / 0.0).to_int(), content="2147483647") // Infinity
  inspect((-1.0 / 0.0).to_int(), content="-2147483648") // -Infinity
}
to_int
(),
String
message
,
Json?
data
:
Json?
None
}),
92 } 93 _ => 94 raise @json.
((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError
(
95 (
@json.JsonPath
path
, "expected jsonrpc, id, and result or error"),
96 ) 97 } 98} 99