diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-08-26 06:07:58 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-08-26 06:07:58 +0400 |
commit | 243285da661f710ac1864d9ddd1c05e4a7b1190f (patch) | |
tree | 219dfaa612fa6667fc9adad13074a5cb02cf5061 | |
parent | c2047b0dc4f7dfd09765e9c9c2bdb92ff1b1e0f6 (diff) |
~ Simplify dictionary building.
-rw-r--r-- | TODO.org | 3 | ||||
-rw-r--r-- | src/Data/BEncode.hs | 24 |
2 files changed, 7 insertions, 20 deletions
@@ -3,4 +3,5 @@ | |||
3 | * TODO lens compatible selectors renaming | 3 | * TODO lens compatible selectors renaming |
4 | * TODO benchmarks for generic decode | 4 | * TODO benchmarks for generic decode |
5 | * TODO documentation | 5 | * TODO documentation |
6 | * TODO v0.1.1.0 (reason: Dict ty syn exposed) | 6 | * TODO v0.2.0.0 (reason: dictAssoc is hidded now) |
7 | * TODO use HashMap | ||
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 | ||
459 | dictAssoc :: [(ByteString, BEncode)] -> BEncode | ||
460 | dictAssoc = 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 | -- |
493 | data Assoc = Required ByteString BEncode | 487 | newtype 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 |
498 | key --> val = Required key (toBEncode val) | 491 | key --> 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 |
505 | key -->? mval = Optional key (toBEncode <$> mval) | 498 | key -->? mval = Assoc $ ((,) key . toBEncode) <$> mval |
506 | {-# INLINE (-->?) #-} | 499 | {-# INLINE (-->?) #-} |
507 | 500 | ||
508 | mkAssocs :: [Assoc] -> [(ByteString, BEncode)] | ||
509 | mkAssocs = 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. |
516 | fromAssocs :: [Assoc] -> BEncode | 502 | fromAssocs :: [Assoc] -> BEncode |
517 | fromAssocs = BDict . M.fromList . mkAssocs | 503 | fromAssocs = 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. |
522 | fromAscAssocs :: [Assoc] -> BEncode | 508 | fromAscAssocs :: [Assoc] -> BEncode |
523 | fromAscAssocs = BDict . M.fromList . mkAssocs | 509 | fromAscAssocs = BDict . M.fromAscList . mapMaybe unAssoc |
524 | {-# INLINE fromAscAssocs #-} | 510 | {-# INLINE fromAscAssocs #-} |
525 | 511 | ||
526 | {-------------------------------------------------------------------- | 512 | {-------------------------------------------------------------------- |