1///|
2/// A Request is a message sent to a peer to request behavior.
3/// If it has an ID then it is a call and therefore requests a response,
4/// otherwise it is a notification which receives no response.
5pub(all) struct Request {
6 // id of this request, used to tie the Response back to the request.
7 // This will be None for notifications.
8 ID?
id : enum ID {
Number(Int64)
String(String)
}
ID is used to identify the request and tie it to the response.
ID?
9 // method_ is a String containing the method name to invoke.
10 String
method_ : String
String
11 // params is either a struct or an array with the parameters of the method.
12 Json
params : enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json
13} derive((Request, &Logger) -> Unit
Trait for types that can be converted to String
Show, (Request, Request) -> Bool
Trait for types whose elements can test for equality
Eq)
14
15///|
16pub impl trait ToJson {
to_json(Self) -> Json
}
Trait for types that can be converted to Json
ToJson for struct Request {
id: ID?
method_: String
params: Json
}
A Request is a message sent to a peer to request behavior.
If it has an ID then it is a call and therefore requests a response,
otherwise it is a notification which receives no response.
Request with (self : Request) -> Json
to_json(Request
self) {
17 let Map[String, Json]
obj = { "jsonrpc": "2.0".(self : String) -> Json
to_json(), "method": Request
self.String
method_.(self : String) -> Json
to_json() }
18 if (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
test "not" {
inspect(not(true), content="false")
inspect(not(false), content="true")
}
not(Request
self.Json
params is Json
Null) {
19 Map[String, Json]
obj["params"] = Request
self.Json
params
20 }
21 match Request
self.ID?
id {
22 (ID) -> ID?
Some((String) -> ID
String(String
s)) => Map[String, Json]
obj["id"] = String
s.(self : String) -> Json
to_json()
23 (ID) -> ID?
Some((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()
24 ID?
None => ()
25 }
26 Map[String, Json]
obj.(self : Map[String, Json]) -> Json
to_json()
27}
28
29///|
30pub 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 Request {
id: ID?
method_: String
params: Json
}
A Request is a message sent to a peer to request behavior.
If it has an ID then it is a call and therefore requests a response,
otherwise it is a notification which receives no response.
Request with (json : Json, path : @json.JsonPath) -> Request!@json.JsonDecodeError
from_json(Json
json, @json.JsonPath
path) {
31 guard Json
json is (Map[String, Json]) -> Json
Object(Map[String, Json]
obj) else {
32 raise @json.((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError((@json.JsonPath
path, "expected object"))
33 }
34 match Map[String, Json]
obj {
35 // with params
36 {
37 "jsonrpc": (String) -> Json
String("2.0"),
38 "id": (Double) -> Json
Number(Double
id),
39 "method": (String) -> Json
String(String
method_),
40 "params": Json
params,
41 ..
42 } => { ID?
id: (ID) -> ID?
Some((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())), String
method_, Json
params }
43 {
44 "jsonrpc": (String) -> Json
String("2.0"),
45 "id": (String) -> Json
String(String
id),
46 "method": (String) -> Json
String(String
method_),
47 "params": Json
params,
48 ..
49 } => { ID?
id: (ID) -> ID?
Some((String) -> ID
String(String
id)), String
method_, Json
params }
50 {
51 "jsonrpc": (String) -> Json
String("2.0"),
52 "method": (String) -> Json
String(String
method_),
53 "params": Json
params,
54 ..
55 } => { ID?
id: ID?
None, String
method_, Json
params }
56 // without params
57 {
58 "jsonrpc": (String) -> Json
String("2.0"),
59 "id": (Double) -> Json
Number(Double
id),
60 "method": (String) -> Json
String(String
method_),
61 ..
62 } => { ID?
id: (ID) -> ID?
Some((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())), String
method_, Json
params: enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json::() -> Json
Creates a JSON null value.
Returns a JSON value representing null.
Example:
test "null" {
inspect(Json::null(), content="Null")
}
null() }
63 {
64 "jsonrpc": (String) -> Json
String("2.0"),
65 "id": (String) -> Json
String(String
id),
66 "method": (String) -> Json
String(String
method_),
67 ..
68 } => { ID?
id: (ID) -> ID?
Some((String) -> ID
String(String
id)), String
method_, Json
params: enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json::() -> Json
Creates a JSON null value.
Returns a JSON value representing null.
Example:
test "null" {
inspect(Json::null(), content="Null")
}
null() }
69 { "jsonrpc": (String) -> Json
String("2.0"), "method": (String) -> Json
String(String
method_), .. } =>
70 { ID?
id: ID?
None, String
method_, Json
params: enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json::() -> Json
Creates a JSON null value.
Returns a JSON value representing null.
Example:
test "null" {
inspect(Json::null(), content="Null")
}
null() }
71 _ =>
72 raise @json.((@json.JsonPath, String)) -> @json.JsonDecodeError
JsonDecodeError((@json.JsonPath
path, "expected jsonrpc and method fields"))
73 }
74}
75