damnpacket 0.2.0 → 0.3.0
raw patch · 4 files changed
+29/−20 lines, 4 filesdep +textdep −utf8-string
Dependencies added: text
Dependencies removed: utf8-string
Files
- damnpacket.cabal +3/−2
- src/Text/Damn/Packet/Internal.hs +7/−6
- src/Text/Damn/Packet/Parser.hs +10/−8
- tests/render.hs +9/−4
damnpacket.cabal view
@@ -1,5 +1,5 @@ name: damnpacket-version: 0.2.0+version: 0.3.0 synopsis: Parsing dAmn packets license: MIT license-file: LICENSE@@ -18,8 +18,8 @@ , bytestring >= 0.10 , containers >= 0.4 , deepseq >= 1.3+ , text , trifecta- , utf8-string hs-source-dirs: src default-language: Haskell2010 @@ -33,6 +33,7 @@ , containers >= 0.5 , damnpacket , QuickCheck >= 2.6+ , text default-language: Haskell2010 benchmark parse
src/Text/Damn/Packet/Internal.hs view
@@ -14,12 +14,13 @@ import Control.Applicative import Control.DeepSeq-import Data.ByteString (ByteString)+import Data.ByteString import Data.Data import Data.Map (Map)+import Data.Text -- | A type synonym--because pressing spacebar is pretty irritating.-type Arguments = Map ByteString ByteString+type Arguments = Map Text Text lens :: Functor f => (s -> a) -> (s -> b -> t) -> (a -> f b) -> s -> f t lens sa sbt afb s = sbt s <$> afb (sa s)@@ -52,8 +53,8 @@ -- , 'pktBody' = 'Just' \"value of property\" -- } -- @-data Packet = Packet { pktCommand :: ByteString- , pktParameter :: Maybe ByteString+data Packet = Packet { pktCommand :: Text+ , pktParameter :: Maybe Text , pktArgs :: Arguments , pktBody :: Maybe ByteString } deriving (Eq, Data, Show, Typeable)@@ -67,14 +68,14 @@ -- | A lens on 'pktCommand'. pktCommandL :: Functor f- => (ByteString -> f ByteString)+ => (Text -> f Text) -> Packet -> f Packet pktCommandL = lens pktCommand (\pk b -> pk { pktCommand = b }) {-# INLINE pktCommandL #-} -- | A lens on 'pktParameter'. pktParameterL :: Functor f- => (Maybe ByteString -> f (Maybe ByteString))+ => (Maybe Text -> f (Maybe Text)) -> Packet -> f Packet pktParameterL = lens pktParameter (\pk b -> pk { pktParameter = b }) {-# INLINE pktParameterL #-}
src/Text/Damn/Packet/Parser.hs view
@@ -19,8 +19,9 @@ import qualified Data.Map as M import Data.Monoid import qualified Data.ByteString as B-import Data.ByteString.UTF8 (ByteString)-import qualified Data.ByteString.UTF8 as B hiding (length, drop)+import Data.ByteString (ByteString)+import qualified Data.Text as T+import qualified Data.Text.Encoding as T import Text.Damn.Packet.Internal import Text.Trifecta hiding (render) @@ -52,9 +53,10 @@ -- find it useful if you want to write packets to dAmn. render :: Packet -> ByteString render (Packet cmd prm args b) =- cmd- <> maybe "" (" " <>) prm- <> M.foldrWithKey (\k v m -> "\n" <> k <> "=" <> v <> m) "" args+ T.encodeUtf8 cmd+ <> maybe "" ((" " <>) . T.encodeUtf8) prm+ <> M.foldrWithKey+ (\k v m -> "\n" <> T.encodeUtf8 k <> "=" <> T.encodeUtf8 v <> m) "" args <> maybe "" ("\n\n" <>) b {-# INLINE render #-} @@ -81,15 +83,15 @@ cmd <- some (satisfy (not . isSpace)) prm <- option Nothing paramP args <- argsP- return $ Packet (B.fromString cmd) (fmap B.fromString prm) args Nothing+ return $ Packet (T.pack cmd) (fmap T.pack prm) args Nothing where paramP = fmap Just (char ' ' *> some (satisfy (not . isSpace))) <?> "parameter" argsP = do ch <- optional (char '\n') case ch of Just '\n' -> liftA3 M.insert- (B.fromString <$> some (notChar '='))- (char '=' >> fmap B.fromString (some (notChar '\n')))+ (T.pack <$> some (notChar '='))+ (char '=' >> fmap T.pack (some (notChar '\n'))) argsP _ -> pure mempty
tests/render.hs view
@@ -4,16 +4,21 @@ import Control.Applicative ((<*>), (<$>)) import Control.Monad (unless)+import qualified Data.ByteString as B import Data.Map (fromList, toList)-import Data.ByteString (ByteString, pack, unpack)+import qualified Data.Text as T import System.Exit import Test.QuickCheck import Test.QuickCheck.Test (isSuccess) import Text.Damn.Packet -instance Arbitrary ByteString where- arbitrary = pack <$> vectorOf 10 (elements [97..122])- shrink m = pack <$> shrink (unpack m)+instance Arbitrary B.ByteString where+ arbitrary = B.pack <$> vectorOf 10 (elements [97..122])+ shrink m = B.pack <$> shrink (B.unpack m)++instance Arbitrary T.Text where+ arbitrary = T.pack <$> vectorOf 10 (elements ['a'..'z'])+ shrink m = T.pack <$> shrink (T.unpack m) instance Arbitrary Packet where arbitrary = do