1///|
2/// line_framer returns a new Framer.
3/// The messages are sent with a terminating cr+newline, and rely on json decode consistency
4/// to determine message boundaries.
5pub fn 
() -> &Framer

line_framer returns a new Framer. The messages are sent with a terminating cr+newline, and rely on json decode consistency to determine message boundaries.

line_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 LineFramer {
}
LineFramer
::{ }
7} 8 9///| 10pub struct LineFramer {} 11 12///| 13pub struct LineReader { 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///| 18pub struct LineWriter { 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 LineFramer {
}
LineFramer
with
(_self : LineFramer, in_ : &@io.ByteReader) -> &Reader
reader
(
LineFramer
_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 LineReader {
  in_: &@io.ByteReader
}
LineReader
::{
&@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 LineFramer {
}
LineFramer
with
(_self : LineFramer, out : &@io.Writer) -> &Writer
writer
(
LineFramer
_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 LineWriter {
  out: &@io.Writer
}
LineWriter
::{
&@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 LineReader {
  in_: &@io.ByteReader
}
LineReader
with
(self : LineReader) -> (Message?, Int64, @io.IOError?)
read
(
LineReader
self
) -> (
34
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
?,
35
Int64
Int64
,
36
type! @io.IOError String

An IOError can be tested with equality checks.

@io.IOError
?,
37) { 38 let
@io.Buffer
buf
=
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
()
39 let (
Int64
n
,
@io.IOError?
err
) =
(dst : &@io.Writer, src : &@io.ByteReader, until : Byte) -> (Int64, @io.IOError?)

copy_until copies bytes from a source reader to a destination writer until a specified byte is encountered. It returns the number of bytes copied and any error that occurred during the process.

@io.copy_until
(
@io.Buffer
buf
,
LineReader
self
.
&@io.ByteReader
in_
, b'\n')
40 guard
@io.IOError?
err
is
@io.IOError?
None
else { return (
Message?
None
, 0,
@io.IOError?
err
) }
41 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
42 // Are these conversions really necessary? 43
(json : Json, path~ : @json.JsonPath = ..) -> Message!@json.JsonDecodeError
@json.from_json
!(
(input : String) -> Json!@json.ParseError
@json.parse
!(
@io.Buffer
buf
.
(self : @io.Buffer) -> String
to_string
().
(self : String) -> @string.StringView

Returns the view of the string without the leading and trailing spaces.

trim_space
().
(self : @string.StringView) -> String

Returns a new String containing a copy of the characters in this view.

Examples

let str = "Hello World"
let view = str[0:5]  // "Hello"
inspect(view.to_string(), content="Hello")
to_string
())) catch {
44
Error
e
=> return (
Message?
None
, 0,
(@io.IOError) -> @io.IOError?
Some
(@io.
(String) -> @io.IOError

An IOError can be tested with equality checks.

IOError
(
Error
e
.
(self : Error) -> String
to_string
())))
45 } 46 (
(Message) -> Message?
Some
(
Message
msg
),
Int64
n
,
@io.IOError?
None
)
47} 48 49///| 50pub 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 LineWriter {
  out: &@io.Writer
}
LineWriter
with
(self : LineWriter, msg : Message) -> (Int64, @io.IOError?)
write
(
LineWriter
self
,
Message
msg
) -> (
Int64
Int64
,
type! @io.IOError String

An IOError can be tested with equality checks.

@io.IOError
?) {
51 let
String
s
=
Message
msg
.
(self : Message) -> Json
to_json
().
(self : Json, escape_slash~ : Bool = .., indent~ : Int = ..) -> String
stringify
()
52 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
()
53 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
}\r\n")
54 guard
@io.IOError?
err
is
@io.IOError?
None
else { return (0,
@io.IOError?
err
) }
55 let (
Int
n
,
@io.IOError?
err
) =
LineWriter
self
.
&@io.Writer
out
.
(&@io.Writer, @io.Slice[Byte]) -> (Int, @io.IOError?)
write
(
@io.Buffer
body
.
(self : @io.Buffer) -> @io.Slice[Byte]
to_slice
())
56 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
) }
57 (
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
)
58} 59