moved several utils

This commit is contained in:
Dmitriy Ryajov 2020-04-17 17:16:46 -06:00
parent 6bdae367a2
commit 03bd04544e

View File

@ -6,7 +6,7 @@ template toFuture*[T](v: T): Future[T] =
fut.complete(v)
fut
template toThrough*[T](s: Stream[T]): Through[T] =
proc toThrough*[T](s: Stream[T]): Through[T] {.inline.} =
proc sinkit(i: Source[T]) {.async.} =
await s.sink()(i)
@ -29,6 +29,12 @@ template pipe*[T](s: Stream[T] | Source[T],
template pipe*[T](s: Stream[T] | Source[T],
x: Stream[T] | Sink[T]): Future[void] =
var pipeline: Source[T]
when s is Source[T]:
pipeline = s
elif s is Stream[T]:
pipeline = s.source
var terminal: Future[void]
when x is Stream[T]:
terminal = x.sink()(pipeline)
@ -41,6 +47,25 @@ template pipe*[T](s: Stream[T] | Source[T],
t: varargs[Through[T]],
x: Stream[T] | Sink[T]): Future[void] =
var pipeline = pipe(s, t)
pipe(s, x)
pipe(pipeline, x)
proc appendNl*(): Through[seq[byte]] =
proc append(item: Future[seq[byte]]): Future[seq[byte]] {.async.} =
result = await item
result.add(byte('\n'))
return proc(i: Source[seq[byte]]): Source[seq[byte]] {.gcsafe.} =
return iterator(): Future[seq[byte]] {.closure.} =
for item in i:
yield append(item)
proc stripNl*(): Through[seq[byte]] =
proc strip(item: Future[seq[byte]]): Future[seq[byte]] {.async.} =
result = await item
if result.len > 0 and result[^1] == byte('\n'):
result.setLen(result.high)
return proc(i: Source[seq[byte]]): Source[seq[byte]] {.gcsafe.} =
return iterator(): Future[seq[byte]] {.closure.} =
for item in i:
yield strip(item)