Skip to content

Commit

Permalink
Add utf8Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfmpe committed May 31, 2024
1 parent b50ac6a commit cdd192e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/route/src/Obelisk/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ module Obelisk.Route
, fieldMapEncoder
, pathFieldEncoder
, jsonEncoder
, utf8Encoder
) where

import Prelude hiding ((.), id)
Expand Down Expand Up @@ -1262,6 +1263,14 @@ jsonEncoder = unsafeEncoder $ do
Right x -> return x
}

utf8Encoder :: (Applicative check, MonadError Text parse) => Encoder check parse Text BS.ByteString
utf8Encoder = unsafeMkEncoder $ EncoderImpl
{ _encoderImpl_encode = T.encodeUtf8
, _encoderImpl_decode = \bs -> case T.decodeUtf8' bs of
Left err -> throwError $ "utf8Encoder: " <> tshow err
Right x -> pure x
}

-- Useful for app server integration.
-- p must not start with slashes
byteStringsToPageName :: BS.ByteString -> BS.ByteString -> PageName
Expand Down
1 change: 1 addition & 0 deletions lib/route/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ atomicEncoders = let t n e = (n, Ex e) in
[ t "addPathSegmentEncoder" addPathSegmentEncoder
, t "fieldMapEncoder" $ fieldMapEncoder @_ @_ @XY
, t "jsonEncoder" $ jsonEncoder @_ @_ @Input
, t "utf8Encoder" utf8Encoder
, t "maybeToEitherEncoder" $ maybeToEitherEncoder @_ @_ @Input
, t "pathComponentEncoder" fragmentEncoder
, t "pathSegmentsTextEncoder" pathSegmentsTextEncoder
Expand Down

0 comments on commit cdd192e

Please sign in to comment.