packages feed

typed-encoding 0.5.2.2 → 0.5.2.3

raw patch · 15 files changed

+61/−48 lines, 15 filesdep ~QuickCheckdep ~bytestringdep ~doctest

Dependency ranges changed: QuickCheck, bytestring, doctest, text

Files

ChangeLog.md view
@@ -5,6 +5,9 @@ - `Data.TypedEncoding.Common.Class.IsStringR` expected to be be changed / replaced - (post 0.5) "enc-B64" could be moved to a different package (more distant goal) ++## 0.5.2.3+- compile with ghc 9.4.7 / lts-21.14 ## 0.5.2.2  - `instance UnexpectedDecodeErr Identity` does not use `fail` to allow for base >= 4.9 compilation
README.md view
@@ -84,7 +84,18 @@  ## Examples  -Please see `Examples.TypedEncoding` it the module list.+Here are some code examples:++- [Overview](src/Examples/TypedEncoding/Overview.hs)+- [Conversions between encodings](src/Examples/TypedEncoding/Conversions.hs)+- [DIY encoding, error handling](src/Examples/TypedEncoding/Instances/DiySignEncoding.hs)+- [To and from string conversions](src/Examples/TypedEncoding/ToEncString.hs)+- [Unsafe - working inside encodings](src/Examples/TypedEncoding/Unsafe.hs)+ ++## Hackage++https://hackage.haskell.org/package/typed-encoding  ## Other encoding packages 
src/Data/TypedEncoding/Common/Class/Common.hs view
@@ -45,7 +45,7 @@     symbolVals = []  -- |--- >>> symbolVals @ '["FIRST", "SECOND"]+-- >>> symbolVals @'["FIRST", "SECOND"] -- ["FIRST","SECOND"] instance (SymbolList xs, KnownSymbol x) => SymbolList (x ': xs) where     symbolVals =  symbolVal (Proxy :: Proxy x) : symbolVals @xs@@ -81,8 +81,8 @@ -- >>> displ (Proxy :: Proxy ["FIRST", "SECOND"]) -- "[FIRST,SECOND]" instance (SymbolList xs) => Displ (Proxy xs) where-    displ _ = displ $  symbolVals @ xs-        -- "[" ++ (L.intercalate "," $ map displ $ symbolVals @ xs) ++ "]"+    displ _ = displ $  symbolVals @xs+        -- "[" ++ (L.intercalate "," $ map displ $ symbolVals @xs) ++ "]"   
src/Data/TypedEncoding/Common/Class/Superset.hs view
@@ -94,7 +94,7 @@  -- | -- >>> let Right tstAscii = encodeFAll . toEncoding () $ "Hello World" :: Either EncodeEx (Enc '["r-ASCII"] () T.Text)--- >>> displ (injectInto @ "r-UTF8" tstAscii)+-- >>> displ (injectInto @"r-UTF8" tstAscii) -- "Enc '[r-UTF8] () (Text Hello World)" -- -- @since 0.2.2.0
src/Data/TypedEncoding/Common/Types/CheckedEnc.hs view
@@ -68,14 +68,14 @@ -- @since 0.2.0.0 toCheckedEnc :: forall xs c str . (SymbolList xs) => Enc xs c str -> CheckedEnc c str  toCheckedEnc (UnsafeMkEnc p c s) = -        UnsafeMkCheckedEnc (symbolVals @ xs) c s   +        UnsafeMkCheckedEnc (symbolVals @xs) c s     -- | -- @since 0.2.0.0 fromCheckedEnc :: forall xs c str . SymbolList xs => CheckedEnc c str -> Maybe (Enc xs c str) fromCheckedEnc (UnsafeMkCheckedEnc xs c s) =      let p = Proxy :: Proxy xs-    in if symbolVals @ xs == xs+    in if symbolVals @xs == xs        then Just $ UnsafeMkEnc p c s        else Nothing @@ -88,7 +88,7 @@ -- >>> procToCheckedEncFromCheckedEnc @'["TEST1"] encsometest -- False procToCheckedEncFromCheckedEnc :: forall xs c str . (SymbolList xs, Eq c, Eq str) => CheckedEnc c str -> Bool-procToCheckedEncFromCheckedEnc x = (== Just x) . fmap (toCheckedEnc @ xs) . fromCheckedEnc $ x+procToCheckedEncFromCheckedEnc x = (== Just x) . fmap (toCheckedEnc @xs) . fromCheckedEnc $ x  -- | -- >>> let enctest = unsafeSetPayload () "hello" :: Enc '["TEST"] () T.Text
src/Data/TypedEncoding/Common/Types/UncheckedEnc.hs view
@@ -57,7 +57,7 @@ verifyAnn :: forall xs c str . SymbolList xs => UncheckedEnc c str -> Either String (UncheckedEnc c str) verifyAnn x@(MkUncheckedEnc xs _ _) =      let p = Proxy :: Proxy xs-    in if symbolVals @ xs == xs+    in if symbolVals @xs == xs        then Right x        else Left $ "UncheckedEnc has not matching annotation " ++ displ xs 
src/Data/TypedEncoding/Conv/Text/Encoding.hs view
@@ -4,6 +4,7 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeOperators #-}  -- | -- @since 0.2.2.0@@ -58,11 +59,11 @@ -- To be consistent we make the same assumption of also restricting representable Unicode chars as in /Unicode.D76/. -- -- >>> TE.decodeUtf8 "\237\160\128"--- "*** Exception: Cannot decode byte '\xed': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream+-- "*** Exception: Cannot decode byte '\xed': Data.Text.Encoding: Invalid UTF-8 stream --  -- The "\xdfff" case (@11101101 10111111 10111111@ @ed bf bf@): -- >>> TE.decodeUtf8 "\237\191\191"--- "*** Exception: Cannot decode byte '\xed': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream+-- "*** Exception: Cannot decode byte '\xed': Data.Text.Encoding: Invalid UTF-8 stream -- -- >>> displ . decodeUtf8 $ (unsafeSetPayload () "Hello" :: Enc '["r-ASCII"] () B.ByteString) -- "Enc '[r-ASCII] () (Text Hello)"@@ -74,14 +75,14 @@ -- -- @decodeUtf8@ and @encodeUtf8@ now form isomorphism -- --- prop> \x -> getPayload x == (getPayload . encodeUtf8 . decodeUtf8 @ '["r-UTF8"] @() $ x)+-- prop> \x -> getPayload x == (getPayload . encodeUtf8 . decodeUtf8 @'["r-UTF8"] @() $ x) ----- prop> \x -> getPayload x == (getPayload . decodeUtf8 . encodeUtf8 @ '["r-UTF8"] @() $ x)+-- prop> \x -> getPayload x == (getPayload . decodeUtf8 . encodeUtf8 @'["r-UTF8"] @() $ x) -- -- These nicely work as iso's for "r-ASCII" subset ----- prop> \x -> getPayload x == (getPayload . encodeUtf8 . decodeUtf8 @ '["r-ASCII"] @() $ x)--- prop> \x -> getPayload x == (getPayload . decodeUtf8 . encodeUtf8 @ '["r-ASCII"] @() $ x)+-- prop> \x -> getPayload x == (getPayload . encodeUtf8 . decodeUtf8 @'["r-ASCII"] @() $ x)+-- prop> \x -> getPayload x == (getPayload . decodeUtf8 . encodeUtf8 @'["r-ASCII"] @() $ x) -- -- Similarly to 'Data.TypedEncoding.Conv.ByteString.Char8.pack' this function makes unverified assumption -- that the encoding stack @xs@ does invalidate UTF8 byte layout.  This is safe for any "r-" encoding as well
src/Data/TypedEncoding/Instances/Enc/Base64.hs view
@@ -107,7 +107,7 @@     type EncSuperset "enc-B64" = "r-B64"  -- |--- >>> tstChar8Encodable @ '["enc-B64-len", "enc-B64"]+-- >>> tstChar8Encodable @'["enc-B64-len", "enc-B64"] -- "I am CHAR8 encodable" instance EncodingSuperset "enc-B64-len" where     type EncSuperset "enc-B64-len" = "r-B64"
src/Data/TypedEncoding/Instances/Restriction/ASCII.hs view
@@ -11,7 +11,7 @@ -- -- This is sometimes referred to as ASCII-7 and future versions of @type-encoding@ may change @"r-ASCII"@ symbol annotation to reflect this. --  --- prop> B8.all ((< 128) . ord) . getPayload @ '["r-ASCII"] @() @B.ByteString+-- prop> B8.all ((< 128) . ord) . getPayload @'["r-ASCII"] @() @B.ByteString --  -- >>> :set -XOverloadedStrings -XMultiParamTypeClasses -XDataKinds -- >>> _runEncodings encodings . toEncoding () $ "Hello World" :: Either EncodeEx (Enc '["r-ASCII"] () T.Text)
src/Data/TypedEncoding/Instances/Restriction/Misc.hs view
@@ -37,7 +37,7 @@ instance (IsString str, Applicative f) => ToEncString f "r-Word8-decimal" "r-Word8-decimal" Word8 str where     toEncF  i = pure $ UnsafeMkEnc Proxy () (fromString . show $ i) instance (IsStringR str, UnexpectedDecodeErr f, Applicative f) => FromEncString f "r-Word8-decimal" "r-Word8-decimal" Word8 str where-    fromEncF  = asUnexpected @ "r-Word8-decimal" . readEither . toString . getPayload+    fromEncF  = asUnexpected @"r-Word8-decimal" . readEither . toString . getPayload  encWord8Dec :: (IsStringR str) => Encoding (Either EncodeEx) "r-Word8-decimal" "r-Word8-decimal" c str encWord8Dec = _implEncodingEx (verifyWithRead @Word8 "Word8-decimal")
src/Examples/TypedEncoding/Conversions.hs view
@@ -248,7 +248,7 @@ -- -- @injectInto@ method accepts proxy to specify superset to use. ----- >>> displ $ injectInto @ "r-UTF8" helloAsciiB+-- >>> displ $ injectInto @"r-UTF8" helloAsciiB -- "Enc '[r-UTF8] () (ByteString HeLlo world)" -- -- Superset is intended for @"r-"@ annotations only, should not be used@@ -300,7 +300,7 @@ -- lenient algorithms are not partial and automatically fix invalid input: -- -- >>> recreateFAll . toEncoding () $ "abc==CB" :: Either RecreateEx (Enc '["enc-B64"] () B.ByteString)--- Left (RecreateEx "enc-B64" ("invalid padding"))+-- Left (RecreateEx "enc-B64" ("Base64-encoded bytestring is unpadded or has invalid padding")) -- -- This library allows to recover to "enc-B64-len" which is different than "enc-B64" --@@ -328,6 +328,6 @@ -- thus, we should be able to treat "enc-B64" as "r-ASCII" losing some information. -- this is done using 'FlattenAs' type class ----- >>> :t flattenAs @ "r-ASCII" helloUtf8B64B--- flattenAs @ "r-ASCII" helloUtf8B64B+-- >>> :t flattenAs @"r-ASCII" helloUtf8B64B+-- flattenAs @"r-ASCII" helloUtf8B64B -- ... :: Enc '["r-ASCII"] () B.ByteString
src/Examples/TypedEncoding/Instances/Do/Sample.hs view
@@ -75,7 +75,7 @@ -- | -- @since 0.3.0.0  instance (HasA SizeLimit c, Applicative f) => Encode f "do-size-limit" "do-size-limit" c T.Text where-    encoding = _implEncodingConfP (T.take . unSizeLimit . has @ SizeLimit) +    encoding = _implEncodingConfP (T.take . unSizeLimit . has @SizeLimit)  instance (HasA SizeLimit c, Applicative f) => Encode f "do-size-limit" "do-size-limit" c B.ByteString where-    encoding = _implEncodingConfP (B.take . unSizeLimit .  has @ SizeLimit) +    encoding = _implEncodingConfP (B.take . unSizeLimit .  has @SizeLimit)  
src/Examples/TypedEncoding/Overview.hs view
@@ -91,7 +91,7 @@ -- Right (UnsafeMkEnc Proxy () "SGVsbG8gV29ybGQ=") -- -- >>> recreateFAll . toEncoding () $ "SGVsbG8gV29ybGQ" :: Either RecreateEx (Enc '["enc-B64"] () B.ByteString)--- Left (RecreateEx "enc-B64" ("invalid padding"))+-- Left (RecreateEx "enc-B64" ("Base64-encoded bytestring is unpadded or has invalid padding")) -- -- The above example start by placing payload in zero-encoded @Enc '[] ()@ type and then apply 'recreateFAll' -- this is a good way to recreate encoded type if encoding is known. @@ -143,7 +143,7 @@ -- Again, notice the same expression is used as in previous recovery.  -- -- >>> recreateFAll . toEncoding () $ "SGVsbG8gV29ybGQ=" :: Either RecreateEx (Enc '["enc-B64", "enc-B64"] () B.ByteString)--- Left (RecreateEx "enc-B64" ("invalid padding"))+-- Left (RecreateEx "enc-B64" ("Base64-encoded bytestring is unpadded or has invalid padding")) helloB64B64RecoveredErr :: Either RecreateEx (Enc '["enc-B64", "enc-B64"] () B.ByteString) helloB64B64RecoveredErr = recreateFAll . toEncoding () $ "SGVsbG8gV29ybGQ=" 
src/Examples/TypedEncoding/ToEncString.hs view
@@ -47,10 +47,10 @@ import qualified Data.ByteString as B import           Control.Applicative -- ((<|>)) import           Data.Maybe-import           Data.Semigroup ((<>))   + -- $setup -- >>> :set -XMultiParamTypeClasses -XDataKinds -XPolyKinds -XFlexibleInstances -XTypeApplications -XOverloadedStrings -- >>> import qualified Data.List as L@@ -123,7 +123,7 @@ -- To get 'IpV4' out of the string we need to reverse previous @reduce@. -- This is currently done using helper 'EnT.splitPayload' combinator.  ----- >>> EnT.splitPayload @ '["r-Word8-decimal"] (T.splitOn $ T.pack ".") $ enc +-- >>> EnT.splitPayload @'["r-Word8-decimal"] (T.splitOn $ T.pack ".") $ enc  -- [UnsafeMkEnc Proxy () "128",UnsafeMkEnc Proxy () "1",UnsafeMkEnc Proxy () "1",UnsafeMkEnc Proxy () "10"] --  -- The conversion of a list to IpV4F needs handle errors but these errors @@ -134,7 +134,7 @@ instance (UnexpectedDecodeErr f, Applicative f) => FromEncString f "r-IPv4" "r-IPv4" IpV4 T.Text where        fromEncF = fmap map . unreduce       where unreduce :: Enc '["r-IPv4"] () T.Text -> f (IpV4F (Enc '["r-Word8-decimal"] () T.Text))-            unreduce = asUnexpected @"r-IPv4" . recover . EnT.splitPayload @ '["r-Word8-decimal"] (T.splitOn ".")+            unreduce = asUnexpected @"r-IPv4" . recover . EnT.splitPayload @'["r-Word8-decimal"] (T.splitOn ".")                          map :: IpV4F (Enc '["r-Word8-decimal"] () T.Text) -> IpV4F Word8              map = fmap fromEncString@@ -265,25 +265,25 @@ -- -- This code will not pick it up: ----- >>> fromCheckedEnc @ '["enc-B64", "r-UTF8"] $ piece+-- >>> fromCheckedEnc @'["enc-B64", "r-UTF8"] $ piece -- Nothing -- -- But this one will: ----- >>> fromCheckedEnc @ '["enc-B64", "r-ASCII"]  $ piece+-- >>> fromCheckedEnc @'["enc-B64", "r-ASCII"]  $ piece -- Just (UnsafeMkEnc Proxy () "U29tZSBBU0NJSSBUZXh0") -- -- so we can apply the decoding on the selected piece  ----- >>> fmap (toCheckedEnc . decodePart @'["enc-B64"]) . fromCheckedEnc @ '["enc-B64", "r-ASCII"] $ piece+-- >>> fmap (toCheckedEnc . decodePart @'["enc-B64"]) . fromCheckedEnc @'["enc-B64", "r-ASCII"] $ piece -- Just (UnsafeMkCheckedEnc ["r-ASCII"] () "Some ASCII Text")  decodeB64ForTextOnly :: SimplifiedEmailEncB -> SimplifiedEmailEncB decodeB64ForTextOnly = fmap (runAlternatives fromMaybe [tryUtf8, tryAscii])    where     tryUtf8, tryAscii :: CheckedEnc c B.ByteString -> Maybe (CheckedEnc c B.ByteString)-    tryUtf8 = fmap (toCheckedEnc . decodeToUtf8) . fromCheckedEnc @ '["enc-B64", "r-UTF8"] -    tryAscii = fmap (toCheckedEnc . decodeToAscii) . fromCheckedEnc @ '["enc-B64", "r-ASCII"] +    tryUtf8 = fmap (toCheckedEnc . decodeToUtf8) . fromCheckedEnc @'["enc-B64", "r-UTF8"] +    tryAscii = fmap (toCheckedEnc . decodeToAscii) . fromCheckedEnc @'["enc-B64", "r-ASCII"]        decodeToUtf8 :: Enc '["enc-B64", "r-UTF8"] c B.ByteString -> _     decodeToUtf8 = decodePart @'["enc-B64"]
typed-encoding.cabal view
@@ -1,13 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.36.0. -- -- see: https://github.com/sol/hpack------ hash: 2e99f3592215d03b39a9590e192dd29732e41968794d724d3309f8104bd496fd  name:           typed-encoding-version:        0.5.2.2+version:        0.5.2.3 synopsis:       Type safe string transformations description:    See README.md in the project github repository. category:       Data, Text@@ -100,9 +98,9 @@   build-depends:       base >=4.10 && <5     , base64-bytestring >=1.0 && <1.3-    , bytestring >=0.10 && <0.11+    , bytestring >=0.10 && <0.13     , symbols >=0.3 && <0.3.1-    , text >=1.2 && <1.3+    , text >=1.2 && <3   default-language: Haskell2010  test-suite typed-encoding-doctest@@ -114,15 +112,15 @@       doctest   ghc-options: -threaded -rtsopts -with-rtsopts=-N   build-depends:-      QuickCheck >=2.13.1 && <2.14+      QuickCheck >=2.13.1 && <3     , base >=4.10 && <5     , base64-bytestring >=1.0 && <1.3-    , bytestring >=0.10 && <0.11-    , doctest >=0.16 && <0.17-    , doctest-discover >=0.2 && <0.3+    , bytestring >=0.10 && <0.13+    , doctest >=0.16 && <1+    , doctest-discover ==0.2.*     , quickcheck-instances >=0.3.20 && <0.4     , symbols >=0.3 && <0.3.1-    , text >=1.2 && <1.3+    , text >=1.2 && <3     , typed-encoding   default-language: Haskell2010 @@ -137,13 +135,13 @@       test   ghc-options: -threaded -rtsopts -with-rtsopts=-N   build-depends:-      QuickCheck >=2.13.1 && <2.14+      QuickCheck >=2.13.1 && <3     , base >=4.10 && <5     , base64-bytestring >=1.0 && <1.3-    , bytestring >=0.10 && <0.11+    , bytestring >=0.10 && <0.13     , hspec     , quickcheck-instances >=0.3.20 && <0.4     , symbols >=0.3 && <0.3.1-    , text >=1.2 && <1.3+    , text >=1.2 && <3     , typed-encoding   default-language: Haskell2010