syndicate-nim/src/sam/protocols/stream.nim

118 lines
2.9 KiB
Nim
Raw Normal View History

import
2023-05-18 10:20:44 +00:00
preserves
type
2021-09-24 19:25:47 +00:00
CreditAmountKind* {.pure.} = enum
`count`, `unbounded`
`CreditAmount`* {.preservesOr.} = object
case orKind*: CreditAmountKind
of CreditAmountKind.`count`:
2024-01-07 22:11:59 +00:00
`count`*: BiggestInt
2021-09-24 19:25:47 +00:00
of CreditAmountKind.`unbounded`:
`unbounded`* {.preservesLiteral: "unbounded".}: bool
2021-09-24 19:25:47 +00:00
StreamError* {.preservesRecord: "error".} = object
`message`*: string
2023-12-31 17:15:06 +00:00
StreamListenerError* {.preservesRecord: "stream-listener-error".} = object
`spec`*: Value
`message`*: string
2023-12-31 17:15:06 +00:00
StreamConnection* {.preservesRecord: "stream-connection".} = object
2024-01-07 22:11:59 +00:00
`source`* {.preservesEmbedded.}: EmbeddedRef
`sink`* {.preservesEmbedded.}: EmbeddedRef
2023-12-31 17:15:06 +00:00
`spec`*: Value
2021-09-24 19:25:47 +00:00
`LineMode`* {.preservesOr, pure.} = enum
2021-09-24 19:25:47 +00:00
`lf`, `crlf`
SourceKind* {.pure.} = enum
2021-09-24 19:25:47 +00:00
`sink`, `StreamError`, `credit`
2023-12-31 17:15:06 +00:00
SourceSink* {.preservesRecord: "sink".} = object
2024-01-07 22:11:59 +00:00
`controller`* {.preservesEmbedded.}: EmbeddedRef
2023-12-31 17:15:06 +00:00
SourceCredit* {.preservesRecord: "credit".} = object
`amount`*: CreditAmount
2023-12-31 17:15:06 +00:00
`mode`*: Mode
2023-12-31 17:15:06 +00:00
`Source`* {.acyclic, preservesOr.} = ref object
2021-09-24 19:25:47 +00:00
case orKind*: SourceKind
of SourceKind.`sink`:
2024-01-07 22:11:59 +00:00
`sink`* {.preservesEmbedded.}: SourceSink
2021-09-24 19:25:47 +00:00
of SourceKind.`StreamError`:
`streamerror`*: StreamError
of SourceKind.`credit`:
2023-12-31 17:15:06 +00:00
`credit`*: SourceCredit
SinkKind* {.pure.} = enum
2021-09-24 19:25:47 +00:00
`source`, `StreamError`, `data`, `eof`
2023-12-31 17:15:06 +00:00
SinkSource* {.preservesRecord: "source".} = object
2024-01-07 22:11:59 +00:00
`controller`* {.preservesEmbedded.}: EmbeddedRef
2023-12-31 17:15:06 +00:00
SinkData* {.preservesRecord: "data".} = object
`payload`*: Value
`mode`*: Mode
2021-09-24 19:25:47 +00:00
SinkEof* {.preservesRecord: "eof".} = object
2023-12-31 17:15:06 +00:00
`Sink`* {.acyclic, preservesOr.} = ref object
2021-09-24 19:25:47 +00:00
case orKind*: SinkKind
of SinkKind.`source`:
2024-01-07 22:11:59 +00:00
`source`* {.preservesEmbedded.}: SinkSource
2021-09-24 19:25:47 +00:00
of SinkKind.`StreamError`:
`streamerror`*: StreamError
of SinkKind.`data`:
2023-12-31 17:15:06 +00:00
`data`*: SinkData
of SinkKind.`eof`:
2021-09-24 19:25:47 +00:00
`eof`*: SinkEof
2023-12-31 17:15:06 +00:00
StreamListenerReady* {.preservesRecord: "stream-listener-ready".} = object
`spec`*: Value
ModeKind* {.pure.} = enum
`bytes`, `lines`, `packet`, `object`
2021-09-24 19:25:47 +00:00
ModePacket* {.preservesRecord: "packet".} = object
2022-07-08 12:49:24 +00:00
`size`*: BiggestInt
2023-12-31 17:15:06 +00:00
ModeObject* {.preservesRecord: "object".} = object
`description`*: Value
2023-12-31 17:15:06 +00:00
`Mode`* {.preservesOr.} = object
2021-09-24 19:25:47 +00:00
case orKind*: ModeKind
of ModeKind.`bytes`:
2021-09-24 19:25:47 +00:00
`bytes`* {.preservesLiteral: "bytes".}: bool
of ModeKind.`lines`:
`lines`*: LineMode
of ModeKind.`packet`:
2021-09-24 19:25:47 +00:00
`packet`*: ModePacket
of ModeKind.`object`:
2023-12-31 17:15:06 +00:00
`object`*: ModeObject
2023-12-31 17:15:06 +00:00
proc `$`*(x: CreditAmount | StreamError | StreamListenerError | StreamConnection |
Source |
Sink |
StreamListenerReady |
Mode): string =
`$`(toPreserves(x))
proc encode*(x: CreditAmount | StreamError | StreamListenerError |
StreamConnection |
Source |
Sink |
StreamListenerReady |
Mode): seq[byte] =
encode(toPreserves(x))