packages feed

sext 0.1.2 → 0.1.3

raw patch · 5 files changed

+72/−22 lines, 5 filesdep +tastydep +tasty-hunitPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies added: tasty, tasty-hunit

API changes (from Hackage documentation)

+ Data.Sext: type family Elem a;
+ Data.Sext.Class: instance Data.Sext.Class.Sextable Data.ByteString.Short.Internal.ShortByteString
+ Data.Sext.Class: instance GHC.Classes.Eq (Data.Sext.Class.Sext i Data.ByteString.Short.Internal.ShortByteString)
+ Data.Sext.Class: instance GHC.Classes.Ord (Data.Sext.Class.Sext i Data.ByteString.Short.Internal.ShortByteString)
- Data.Sext: class Sextable a where data Sext (i :: Nat) a where {
+ Data.Sext: class Sextable a where data Sext (i :: Nat) a type Elem a where {

Files

+ CHANGELOG.md view
@@ -0,0 +1,12 @@+# Change Log for [sext](https://github.com/dzhus/sext)++### 0.1.3++#### Added++- `ShortByteString` support++#### Fixed++- a bug in `createLeft` which failed to actually pad/truncate strings+  (reported by Altai-man https://github.com/dzhus/sext/issues/4)
sext.cabal view
@@ -1,5 +1,5 @@ name: sext-version: 0.1.2+version: 0.1.3 cabal-version: >=1.10 build-type: Simple license: BSD3@@ -7,11 +7,13 @@ maintainer: dima@dzhus.org homepage: https://github.com/dzhus/sext#readme bug-reports: https://github.com/dzhus/sext/issues-synopsis: Lists, Texts, ByteStrings, and Vectors with type-encoded length+synopsis: Lists, Texts, ByteStrings and Vectors with type-encoded length description:     Sext (/s/tatic t/ext/) provides type-level safety for basic operations on string-like types (finite lists of elements). Use it when you need static guarantee on lengths of strings produced in your code. category: Data, Text, Type System author: Dmitry Dzhus+extra-source-files:+    CHANGELOG.md  source-repository head     type: git@@ -64,7 +66,10 @@     build-depends:         base <5,         template-haskell <2.12,-        sext <0.2+        bytestring <0.11,+        sext <0.2,+        tasty <0.12,+        tasty-hunit <0.10     default-language: Haskell2010     hs-source-dirs: tests     ghc-options: -Wall
src/Data/Sext.hs view
@@ -20,13 +20,15 @@ An example application would be a network exchange protocol built of packets with fixed-width fields: -> {-# LANGUAGE DataKinds #-}-> {-# LANGUAGE OverloadedStrings #-}-> {-# LANGUAGE TemplateHaskell #-}->+@+{\-\# LANGUAGE DataKinds #-\}+{\-\# LANGUAGE OverloadedStrings #-\}+{\-\# LANGUAGE TemplateHaskell #-\}+@+ > import Data.Sext >-> mkPacket :: String -> Sext 32 String+> mkPacket :: ByteString -> Sext 32 ByteString > mkPacket inp = >   -- 5-character version signature >   $(sext "PKT10") `append`@@ -35,12 +37,12 @@ >   -- 2-character payload checksum >   checksum >   where->     payload = createLeft ' ' inp->     checksum :: Sext 2 String->     checksum = createLeft ' ' $->                show $ Data.Sext.length payload `mod` 100+>     payload = createLeft 0x20 inp+>     checksum :: Sext 2 ByteString+>     checksum = createLeft 0x20 $+>                pack $ show $ Data.Sext.length payload `mod` 100 >-> message :: Sext 64 String+> message :: Sext 64 ByteString > message = mkPacket "Hello" `append` mkPacket "world"  Sext combinators are defined for members of 'Sextable' class. The@@ -75,7 +77,7 @@           -- * Sextable class        , Sext-       , Sextable(unsafeCreate, unwrap)+       , Sextable(Elem, unsafeCreate, unwrap)        )  where@@ -106,7 +108,7 @@               Elem a -> a -> Sext i a createLeft e s =   C.unsafeCreate $-  C.take (C.length s) $+  C.take t $   C.append s $   C.replicate (t - C.length s) e   where
src/Data/Sext/Class.hs view
@@ -20,6 +20,7 @@ #ifdef WITH_BS import qualified Data.ByteString as B import           GHC.Word+import qualified Data.ByteString.Short as BS #endif  #ifdef WITH_TEXT@@ -131,6 +132,24 @@   map = B.map   take = B.take   drop = B.drop++-- | Sextable instance for 'BS.ShortByteString' uses intermediate+-- 'B.ByteString's (pinned) for all modification operations.+instance Sextable BS.ShortByteString where+  type Elem BS.ShortByteString = Word8++  data Sext i BS.ShortByteString = ByteStringS BS.ShortByteString+    deriving (Eq, Ord)++  unsafeCreate = ByteStringS+  unwrap (ByteStringS t) = t++  length = BS.length+  append a b = BS.toShort $ B.append (BS.fromShort a) (BS.fromShort b)+  replicate n = BS.toShort . B.replicate n+  map f = BS.toShort . B.map f . BS.fromShort+  take n = BS.toShort . B.take n . BS.fromShort+  drop n = BS.toShort . B.drop n . BS.fromShort #endif  
tests/Main.hs view
@@ -2,9 +2,14 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} +import Data.ByteString.Char8 (ByteString, length, pack)+import Data.Typeable+import Test.Tasty+import Test.Tasty.HUnit+ import Data.Sext -mkPacket :: String -> Sext 32 String+mkPacket :: ByteString -> Sext 32 ByteString mkPacket inp =   -- 5-character version signature   $(sext "PKT10") `append`@@ -13,12 +18,19 @@   -- 2-character payload checksum   checksum   where-    payload = createLeft ' ' inp-    checksum :: Sext 2 String-    checksum = createLeft ' ' $-               show $ Data.Sext.length payload `mod` 100+    payload = createLeft 0x20 inp+    checksum :: Sext 2 ByteString+    checksum = createLeft 0x20 $+               pack $ show $ Data.Sext.length payload `mod` 100 -message :: Sext 64 String+message :: Sext 64 ByteString message = mkPacket "Hello" `append` mkPacket "world" -main = print message+tests :: [TestTree]+tests =+  [ testCase ("The actual length of " ++ show (typeOf message)) $+    assertEqual "" 64 (Data.ByteString.Char8.length $ unwrap message)+  ]++main :: IO ()+main = defaultMain $ testGroup "Tests" tests