Skip to content

Commit 15cb9d0

Browse files
committed
readBytesAsString no longer returns Maybe, instead it will cancel the stream if the bytes cannot be converted to a String.
1 parent d0f618f commit 15cb9d0

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/Stream.gren

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ We have three kinds of streams: readable streams, writable streams, and transfor
8080
import Array exposing (Array)
8181
import Basics exposing (Bool, Int, (<|), (|>), (++), max)
8282
import Bytes exposing (Bytes)
83-
import Maybe exposing (Maybe)
83+
import Maybe exposing (Maybe(..))
8484
import Result exposing (Result(..))
8585
import String exposing (String)
8686
import Task exposing (Task)
@@ -160,12 +160,26 @@ read =
160160
Gren.Kernel.Stream.read
161161

162162

163-
{-| Reads `Bytes` off the stream and attempt to convert it into `String`.
163+
{-| Reads `Bytes` off the stream and attempt to convert it into `String`. If the conversion fails,
164+
the stream will be cancelled.
164165
-}
165-
readBytesAsString : Readable Bytes -> Task Error (Maybe String)
166+
readBytesAsString : Readable Bytes -> Task Error String
166167
readBytesAsString stream =
167168
read stream
168-
|> Task.map Bytes.toString
169+
|> Task.andThen
170+
(\bytes ->
171+
when Bytes.toString bytes is
172+
Just str ->
173+
Task.succeed str
174+
175+
Nothing ->
176+
let
177+
reason =
178+
"Failed to convert bytes to string"
179+
in
180+
cancelReadable reason stream
181+
|> Task.andThen (\_ -> Task.fail (Cancelled reason))
182+
)
169183

170184

171185
{-| Reads values of the stream, incrementally building a value with the provided function, until

0 commit comments

Comments
 (0)