1///|
2/// raw_framer returns a new Framer.
3/// The messages are sent with no wrapping, and rely on json decode consistency
4/// to determine message boundaries.
5pub fn () -> &Framer
raw_framer returns a new Framer.
The messages are sent with no wrapping, and rely on json decode consistency
to determine message boundaries.
raw_framer() -> &trait Framer {
reader(Self, &@io.ByteReader) -> &Reader
writer(Self, &@io.Writer) -> &Writer
}
Framer wraps low level byte readers and writers into jsonrpc2 message
readers and writers.
It is responsible for the framing and encoding of messages into wire form.
Framer {
6 struct RawFramer {
}
RawFramer::{ }
7}
8
9///|
10pub struct RawFramer {}
11
12///|
13struct RawReader {
14 &@io.ByteReader
in_ : &trait @io.ByteReader {
read_byte(Self) -> (Byte, @io.IOError?)
}
ByteReader is the interface that wraps the read_byte method.
read_byte reads and returns the next byte from the input or
any error encountered. If read_byte returns an error, no input
byte was consumed, and the returned byte value is undefined.
read_byte provides an efficient interface for byte-at-time
processing. A [Reader] that does not implement ByteReader
can be wrapped using bufio.NewReader to add this method.
@io.ByteReader
15}
16
17///|
18struct RawWriter {
19 &@io.Writer
out : &trait @io.Writer {
write(Self, @io.Slice[Byte]) -> (Int, @io.IOError?)
}
Writer is the interface that wraps the basic Write method.
Write writes len(p) bytes from p to the underlying data stream.
It returns the number of bytes written from p (0 <= n <= len(p))
and any error encountered that caused the write to stop early.
Write must return a non-None error if it returns n < len(p).
Write must not modify the slice data, even temporarily.
Implementations must not retain p.
@io.Writer
20}
21
22///|
23pub impl trait Framer {
reader(Self, &@io.ByteReader) -> &Reader
writer(Self, &@io.Writer) -> &Writer
}
Framer wraps low level byte readers and writers into jsonrpc2 message
readers and writers.
It is responsible for the framing and encoding of messages into wire form.
Framer for struct RawFramer {
}
RawFramer with (_self : RawFramer, in_ : &@io.ByteReader) -> &Reader
reader(RawFramer
_self, &@io.ByteReader
in_) -> &trait Reader {
read(Self) -> (Message?, Int64, @io.IOError?)
}
Reader abstracts the transport mechanics from the JSON RPC protocol.
A Conn reads messages from the reader it was provided on construction,
and assumes that each call to Read fully transfers a single message,
or returns an error.
A reader is not safe for concurrent use, it is expected it will be used by
a single Conn in a safe manner.
Reader {
24 struct RawReader {
in_: &@io.ByteReader
}
RawReader::{ &@io.ByteReader
in_, }
25}
26
27///|
28pub impl trait Framer {
reader(Self, &@io.ByteReader) -> &Reader
writer(Self, &@io.Writer) -> &Writer
}
Framer wraps low level byte readers and writers into jsonrpc2 message
readers and writers.
It is responsible for the framing and encoding of messages into wire form.
Framer for struct RawFramer {
}
RawFramer with (_self : RawFramer, out : &@io.Writer) -> &Writer
writer(RawFramer
_self, &@io.Writer
out) -> &trait Writer {
write(Self, Message) -> (Int64, @io.IOError?)
}
Writer abstracts the transport mechanics from the JSON RPC protocol.
A Conn writes messages using the writer it was provided on construction,
and assumes that each call to Write fully transfers a single message,
or returns an error.
A writer is not safe for concurrent use, it is expected it will be used by
a single Conn in a safe manner.
Writer {
29 struct RawWriter {
out: &@io.Writer
}
RawWriter::{ &@io.Writer
out, }
30}
31
32///|
33pub impl trait Reader {
read(Self) -> (Message?, Int64, @io.IOError?)
}
Reader abstracts the transport mechanics from the JSON RPC protocol.
A Conn reads messages from the reader it was provided on construction,
and assumes that each call to Read fully transfers a single message,
or returns an error.
A reader is not safe for concurrent use, it is expected it will be used by
a single Conn in a safe manner.
Reader for struct RawReader {
in_: &@io.ByteReader
}
RawReader with (self : RawReader) -> (Message?, Int64, @io.IOError?)
read(RawReader
self) -> (enum Message {
Request(Request)
Response(Response)
BatchRequest(Array[Request])
BatchResponse(Array[Response])
}
Message covers all jsonrpc2 message types.
They share no common functionality, but are a closed set of concrete types
that are allowed to implement this interface. The message types are Request
and Response.
Message?, Int64
Int64, type! @io.IOError String
An IOError can be tested with equality checks.
@io.IOError?) {
34 let (Json
json, Int64
n, @io.IOError?
err) = (src : &@io.ByteReader) -> (Json, Int64, @io.IOError?)
copy_json copies bytes from a source reader until a valid JSON value is completed.
It returns the Json value, the number of bytes copied, and any error that occurred during the process.
@io.copy_json(RawReader
self.&@io.ByteReader
in_)
35 if @io.IOError?
err (x : @io.IOError?, y : @io.IOError?) -> Bool
!= @io.IOError?
None {
36 return (Message?
None, Int64
n, @io.IOError?
err)
37 }
38 let Message
msg : enum Message {
Request(Request)
Response(Response)
BatchRequest(Array[Request])
BatchResponse(Array[Response])
}
Message covers all jsonrpc2 message types.
They share no common functionality, but are a closed set of concrete types
that are allowed to implement this interface. The message types are Request
and Response.
Message = try (json : Json, path~ : @json.JsonPath = ..) -> Message!@json.JsonDecodeError
@json.from_json!(Json
json) catch {
39 @json.JsonDecodeError
e => return (Message?
None, Int64
n, (@io.IOError) -> @io.IOError?
Some(@io.(String) -> @io.IOError
An IOError can be tested with equality checks.
IOError(@json.JsonDecodeError
e.(self : @json.JsonDecodeError) -> String
to_string())))
40 }
41 ((Message) -> Message?
Some(Message
msg), Int64
n, @io.IOError?
None)
42}
43
44///|
45pub impl trait Writer {
write(Self, Message) -> (Int64, @io.IOError?)
}
Writer abstracts the transport mechanics from the JSON RPC protocol.
A Conn writes messages using the writer it was provided on construction,
and assumes that each call to Write fully transfers a single message,
or returns an error.
A writer is not safe for concurrent use, it is expected it will be used by
a single Conn in a safe manner.
Writer for struct RawWriter {
out: &@io.Writer
}
RawWriter with (self : RawWriter, msg : Message) -> (Int64, @io.IOError?)
write(RawWriter
self, Message
msg) -> (Int64
Int64, type! @io.IOError String
An IOError can be tested with equality checks.
@io.IOError?) {
46 let String
s = Message
msg.(self : Message) -> Json
to_json().(self : Json, escape_slash~ : Bool = .., indent~ : Int = ..) -> String
stringify()
47 let @io.Buffer
body = type @io.Buffer
@io.Buffer::(size_hint~ : Int = ..) -> @io.Buffer
Buffer::new creates a new empty Buffer with an optional size hint.
The size hint is used to preallocate memory for the buffer, which can
improve performance when the size of the data to be written is known in advance.
new()
48 let (_, @io.IOError?
err) = @io.Buffer
body.(self : @io.Buffer, s : String) -> (Int, @io.IOError?)
write_string converts the MoonBit UTF-16 String to UTF-8 and
writes the bytes to the buffer.
write_string(String
s)
49 guard @io.IOError?
err is @io.IOError?
None else { return (0, @io.IOError?
err) }
50 let (Int
n, @io.IOError?
err) = RawWriter
self.&@io.Writer
out.(&@io.Writer, @io.Slice[Byte]) -> (Int, @io.IOError?)
write(@io.Buffer
body.(self : @io.Buffer) -> @io.Slice[Byte]
to_slice())
51 guard @io.IOError?
err is @io.IOError?
None else { return (Int
n.(self : Int) -> Int64
Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit
integers can be represented exactly in 64-bit integer format, so the
conversion is lossless.
Parameters:
self : The 32-bit signed integer to be converted.
Returns a 64-bit signed integer that represents the same numerical value as
the input.
Example:
test "Int::to_int64" {
let n = 42
inspect(n.to_int64(), content="42")
let neg = -42
inspect(neg.to_int64(), content="-42")
}
to_int64(), @io.IOError?
err) }
52 (Int
n.(self : Int) -> Int64
Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit
integers can be represented exactly in 64-bit integer format, so the
conversion is lossless.
Parameters:
self : The 32-bit signed integer to be converted.
Returns a 64-bit signed integer that represents the same numerical value as
the input.
Example:
test "Int::to_int64" {
let n = 42
inspect(n.to_int64(), content="42")
let neg = -42
inspect(neg.to_int64(), content="-42")
}
to_int64(), @io.IOError?
err)
53}
54