summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-08-26 06:07:58 +0400
committerSam T <pxqr.sta@gmail.com>2013-08-26 06:07:58 +0400
commit243285da661f710ac1864d9ddd1c05e4a7b1190f (patch)
tree219dfaa612fa6667fc9adad13074a5cb02cf5061 /src
parentc2047b0dc4f7dfd09765e9c9c2bdb92ff1b1e0f6 (diff)
~ Simplify dictionary building.
Diffstat (limited to 'src')
-rw-r--r--src/Data/BEncode.hs24
1 files changed, 5 insertions, 19 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs
index 0f32386..4d76fb4 100644
--- a/src/Data/BEncode.hs
+++ b/src/Data/BEncode.hs
@@ -456,16 +456,10 @@ instance BEncodable Version where
456 fromBEncode _ = decodingError "Data.Version" 456 fromBEncode _ = decodingError "Data.Version"
457 {-# INLINE fromBEncode #-} 457 {-# INLINE fromBEncode #-}
458 458
459dictAssoc :: [(ByteString, BEncode)] -> BEncode
460dictAssoc = BDict . M.fromList
461{-# INLINE dictAssoc #-}
462
463{-------------------------------------------------------------------- 459{--------------------------------------------------------------------
464 Building dictionaries 460 Building dictionaries
465--------------------------------------------------------------------} 461--------------------------------------------------------------------}
466 462
467-- TODO Assoc = Maybe (ByteString, BEncode)
468
469-- | /Assoc/ used to easily build dictionaries with required and 463-- | /Assoc/ used to easily build dictionaries with required and
470-- optional keys. Suppose we have we following datatype we want to 464-- optional keys. Suppose we have we following datatype we want to
471-- serialize: 465-- serialize:
@@ -490,37 +484,29 @@ dictAssoc = BDict . M.fromList
490-- > , "tags" -->? fileTags 484-- > , "tags" -->? fileTags
491-- > ] 485-- > ]
492-- 486--
493data Assoc = Required ByteString BEncode 487newtype Assoc = Assoc { unAssoc :: Maybe (ByteString, BEncode) }
494 | Optional ByteString (Maybe BEncode)
495 488
496-- | Make required key value pair. 489-- | Make required key value pair.
497(-->) :: BEncodable a => ByteString -> a -> Assoc 490(-->) :: BEncodable a => ByteString -> a -> Assoc
498key --> val = Required key (toBEncode val) 491key --> val = Assoc $ Just $ (key, toBEncode val)
499{-# INLINE (-->) #-} 492{-# INLINE (-->) #-}
500 493
501-- | Like (-->) but if the value is not present then the key do not 494-- | Like (-->) but if the value is not present then the key do not
502-- appear in resulting bencoded dictionary. 495-- appear in resulting bencoded dictionary.
503-- 496--
504(-->?) :: BEncodable a => ByteString -> Maybe a -> Assoc 497(-->?) :: BEncodable a => ByteString -> Maybe a -> Assoc
505key -->? mval = Optional key (toBEncode <$> mval) 498key -->? mval = Assoc $ ((,) key . toBEncode) <$> mval
506{-# INLINE (-->?) #-} 499{-# INLINE (-->?) #-}
507 500
508mkAssocs :: [Assoc] -> [(ByteString, BEncode)]
509mkAssocs = mapMaybe unpackAssoc
510 where
511 unpackAssoc (Required n v) = Just (n, v)
512 unpackAssoc (Optional n (Just v)) = Just (n, v)
513 unpackAssoc (Optional _ Nothing) = Nothing
514
515-- | Build BEncode dictionary using key -> value description. 501-- | Build BEncode dictionary using key -> value description.
516fromAssocs :: [Assoc] -> BEncode 502fromAssocs :: [Assoc] -> BEncode
517fromAssocs = BDict . M.fromList . mkAssocs 503fromAssocs = BDict . M.fromList . mapMaybe unAssoc
518{-# INLINE fromAssocs #-} 504{-# INLINE fromAssocs #-}
519 505
520-- | A faster version of 'fromAssocs'. Should be used only when keys 506-- | A faster version of 'fromAssocs'. Should be used only when keys
521-- in builder list are sorted by ascending. 507-- in builder list are sorted by ascending.
522fromAscAssocs :: [Assoc] -> BEncode 508fromAscAssocs :: [Assoc] -> BEncode
523fromAscAssocs = BDict . M.fromList . mkAssocs 509fromAscAssocs = BDict . M.fromAscList . mapMaybe unAssoc
524{-# INLINE fromAscAssocs #-} 510{-# INLINE fromAscAssocs #-}
525 511
526{-------------------------------------------------------------------- 512{--------------------------------------------------------------------