about summary refs log tree commit diff stats
path: root/src/io/serialize.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-11-15 23:42:20 +0100
committerbptato <nincsnevem662@gmail.com>2022-11-19 14:32:54 +0100
commita6bbcd0dd3f77b0e98527c1fa9e510a40acd954e (patch)
treeca55cc9079afbe788a61986e42d1a8d9d0bc7c2f /src/io/serialize.nim
parente75f62b34f7c7f3127bcde0c4a12cbb785342dd9 (diff)
downloadchawan-a6bbcd0dd3f77b0e98527c1fa9e510a40acd954e.tar.gz
Rewrite buffer/pager for multi-processing
Diffstat (limited to 'src/io/serialize.nim')
-rw-r--r--src/io/serialize.nim79
1 files changed, 73 insertions, 6 deletions
diff --git a/src/io/serialize.nim b/src/io/serialize.nim
index 932b82df..2dde0649 100644
--- a/src/io/serialize.nim
+++ b/src/io/serialize.nim
@@ -4,7 +4,10 @@ import options
 import streams
 import tables
 
+import buffer/cell
 import io/request
+import js/regex
+import types/color
 import types/url
 
 template swrite*[T](stream: Stream, o: T) =
@@ -47,11 +50,9 @@ proc swrite*[T](stream: Stream, s: seq[T]) =
     stream.swrite(m)
 
 proc swrite*[T](stream: Stream, o: Option[T]) =
+  stream.swrite(o.issome)
   if o.issome:
-    stream.swrite(1u8)
     stream.swrite(o.get)
-  else:
-    stream.swrite(0u8)
 
 proc swrite*(stream: Stream, request: Request) =
   stream.swrite(request.httpmethod)
@@ -60,6 +61,39 @@ proc swrite*(stream: Stream, request: Request) =
   stream.swrite(request.body)
   stream.swrite(request.multipart)
 
+proc swrite*(stream: Stream, color: CellColor) =
+  stream.swrite(color.rgb)
+  if color.rgb:
+    stream.swrite(color.rgbcolor)
+  else:
+    stream.swrite(color.color)
+
+proc swrite*(stream: Stream, format: Format) =
+  stream.swrite(format.fgcolor)
+  stream.swrite(format.bgcolor)
+  stream.swrite(format.flags)
+
+proc swrite*(stream: Stream, cell: SimpleFormatCell) =
+  stream.swrite(cell.format)
+  stream.swrite(cell.pos)
+
+proc swrite*(stream: Stream, line: SimpleFlexibleLine) =
+  stream.swrite(line.str)
+  stream.swrite(line.formats)
+
+proc swrite*(stream: Stream, cell: FormatCell) =
+  stream.swrite(cell.format)
+  stream.swrite(cell.pos)
+
+proc swrite*(stream: Stream, line: FlexibleLine) =
+  stream.swrite(line.str)
+  stream.swrite(line.formats)
+
+proc swrite*(stream: Stream, regex: Regex) =
+  stream.swrite(regex.plen)
+  stream.writeData(regex.bytecode, regex.plen)
+  stream.swrite(regex.buf)
+
 template sread*[T](stream: Stream, o: T) =
   stream.read(o)
 
@@ -119,13 +153,13 @@ proc sread*[T](stream: Stream, s: var seq[T]) =
     stream.sread(s[i])
 
 proc sread*[T](stream: Stream, o: var Option[T]) =
-  let c = uint8(stream.readChar())
-  if c == 1u8:
+  var x: bool
+  stream.sread(x)
+  if x:
     var m: T
     stream.sread(m)
     o = some(m)
   else:
-    assert c == 0u8
     o = none(T)
 
 proc sread*(stream: Stream, req: var RequestObj) =
@@ -135,6 +169,39 @@ proc sread*(stream: Stream, req: var RequestObj) =
   stream.sread(req.body)
   stream.sread(req.multipart)
 
+proc sread*(stream: Stream, color: var CellColor) =
+  var rgb: bool
+  stream.sread(rgb)
+  if rgb:
+    color = CellColor(rgb: true)
+    stream.sread(color.rgbcolor)
+  else:
+    color = CellColor(rgb: false)
+    stream.sread(color.color)
+
+proc sread*(stream: Stream, format: var Format) =
+  stream.sread(format.fgcolor)
+  stream.sread(format.bgcolor)
+  stream.sread(format.flags)
+
+proc sread*(stream: Stream, cell: var SimpleFormatCell) =
+  stream.sread(cell.format)
+  stream.sread(cell.pos)
+
+proc sread*(stream: Stream, line: var SimpleFlexibleLine) =
+  stream.sread(line.str)
+  stream.sread(line.formats)
+
+proc sread*(stream: Stream, regex: var Regex) =
+  assert regex.bytecode == nil
+  stream.sread(regex.plen)
+  regex.bytecode = cast[ptr uint8](alloc(regex.plen))
+  regex.clone = true
+  let l = stream.readData(regex.bytecode, regex.plen)
+  stream.sread(regex.buf)
+  if l != regex.plen:
+    `=destroy`(regex)
+
 proc readRequest*(stream: Stream): Request =
   new(result)
   stream.sread(result[])