From 01e62d728d64bd2e6e6d40a8b48b5d8108d6fb94 Mon Sep 17 00:00:00 2001 From: Alexandre Esteves Date: Fri, 17 May 2024 14:12:17 +0100 Subject: [PATCH] Add utf8Encoder --- lib/route/src/Obelisk/Route.hs | 9 +++++++++ lib/route/test/Main.hs | 1 + 2 files changed, 10 insertions(+) diff --git a/lib/route/src/Obelisk/Route.hs b/lib/route/src/Obelisk/Route.hs index 6f6f6e8b4..1e1cd66e1 100644 --- a/lib/route/src/Obelisk/Route.hs +++ b/lib/route/src/Obelisk/Route.hs @@ -128,6 +128,7 @@ module Obelisk.Route , fieldMapEncoder , pathFieldEncoder , jsonEncoder + , utf8Encoder ) where import Prelude hiding ((.), id) @@ -1274,6 +1275,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 diff --git a/lib/route/test/Main.hs b/lib/route/test/Main.hs index fb44adcd8..ecacbc1a6 100644 --- a/lib/route/test/Main.hs +++ b/lib/route/test/Main.hs @@ -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