diff options
Diffstat (limited to 'src/Data/BEncode.hs')
-rw-r--r-- | src/Data/BEncode.hs | 38 |
1 files changed, 11 insertions, 27 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs index 427d401..f12d116 100644 --- a/src/Data/BEncode.hs +++ b/src/Data/BEncode.hs | |||
@@ -622,7 +622,7 @@ fromDict _ _ = decodingError (show (typeOf inst)) | |||
622 | {-------------------------------------------------------------------- | 622 | {-------------------------------------------------------------------- |
623 | Building dictionaries | 623 | Building dictionaries |
624 | --------------------------------------------------------------------} | 624 | --------------------------------------------------------------------} |
625 | {- | 625 | |
626 | -- | /Assoc/ used to easily build dictionaries with required and | 626 | -- | /Assoc/ used to easily build dictionaries with required and |
627 | -- optional keys. Suppose we have we following datatype we want to | 627 | -- optional keys. Suppose we have we following datatype we want to |
628 | -- serialize: | 628 | -- serialize: |
@@ -648,42 +648,21 @@ fromDict _ _ = decodingError (show (typeOf inst)) | |||
648 | -- > ] | 648 | -- > ] |
649 | -- > ... | 649 | -- > ... |
650 | -- | 650 | -- |
651 | newtype Assoc = Assoc { unAssoc :: Maybe (ByteString, BValue) } | 651 | type Assoc = Maybe BPair |
652 | |||
653 | -- | Make required key value pair. | ||
654 | (-->) :: BEncode a => BKey -> a -> Assoc | ||
655 | key --> val = Assoc $ Just $ (key, toBEncode val) | ||
656 | {-# INLINE (-->) #-} | ||
657 | |||
658 | -- | Like (-->) but if the value is not present then the key do not | ||
659 | -- appear in resulting bencoded dictionary. | ||
660 | -- | ||
661 | (-->?) :: BEncode a => BKey -> Maybe a -> Assoc | ||
662 | key -->? mval = Assoc $ ((,) key . toBEncode) <$> mval | ||
663 | {-# INLINE (-->?) #-} | ||
664 | |||
665 | -- | Build BEncode dictionary using key -> value description. | ||
666 | fromAssocs :: [Assoc] -> BValue | ||
667 | fromAssocs = undefined -- BDict . M.fromList . mapMaybe unAssoc | ||
668 | {-# INLINE fromAssocs #-} | ||
669 | |||
670 | -- | A faster version of 'fromAssocs'. Should be used only when keys | ||
671 | -- in builder list are sorted by ascending. | ||
672 | fromAscAssocs :: [Assoc] -> BValue | ||
673 | fromAscAssocs = BDict . BD.fromAscList . mapMaybe unAssoc | ||
674 | {-# INLINE fromAscAssocs #-} | ||
675 | -} | ||
676 | 652 | ||
677 | type BPair = (BKey, BValue) | 653 | type BPair = (BKey, BValue) |
678 | type Assoc = Maybe BPair | ||
679 | 654 | ||
680 | -- TODO better name | 655 | -- TODO better name |
656 | -- | Make required key value pair. | ||
681 | (.=!) :: BEncode a => BKey -> a -> Assoc | 657 | (.=!) :: BEncode a => BKey -> a -> Assoc |
682 | k .=! v = Just (k, toBEncode v) | 658 | k .=! v = Just (k, toBEncode v) |
683 | {-# INLINE (.=!) #-} | 659 | {-# INLINE (.=!) #-} |
684 | 660 | ||
685 | infix 6 .=! | 661 | infix 6 .=! |
686 | 662 | ||
663 | -- | Like (.=!) but if the value is not present then the key do not | ||
664 | -- appear in resulting bencoded dictionary. | ||
665 | -- | ||
687 | (.=?) :: BEncode a => BKey -> Maybe a -> Assoc | 666 | (.=?) :: BEncode a => BKey -> Maybe a -> Assoc |
688 | _ .=? Nothing = Nothing | 667 | _ .=? Nothing = Nothing |
689 | k .=? Just v = Just (k, toBEncode v) | 668 | k .=? Just v = Just (k, toBEncode v) |
@@ -698,6 +677,11 @@ Just (k, v) .: d = Cons k v d | |||
698 | 677 | ||
699 | infixr 5 .: | 678 | infixr 5 .: |
700 | 679 | ||
680 | -- | Build BEncode dictionary using key -> value description. | ||
681 | |||
682 | -- | A faster version of 'fromAssocs'. Should be used only when keys | ||
683 | -- in builder list are sorted by ascending. | ||
684 | -- | ||
701 | toDict :: BDict -> BValue | 685 | toDict :: BDict -> BValue |
702 | toDict = BDict | 686 | toDict = BDict |
703 | {-# INLINE toDict #-} | 687 | {-# INLINE toDict #-} |