diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-30 07:38:54 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-30 07:38:54 +0400 |
commit | e908445889d638bd1e892502584775d23078a1a2 (patch) | |
tree | 669fbc647c5358599a5d72c908648184a0ced65a | |
parent | 2e19d4347e5a3d4a0306bc4ffff0a0cafcdbb2be (diff) |
Update documentation
-rw-r--r-- | src/Data/BEncode.hs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs index 8580d40..eff900f 100644 --- a/src/Data/BEncode.hs +++ b/src/Data/BEncode.hs | |||
@@ -566,24 +566,25 @@ instance (BEncode a, BEncode b, BEncode c, BEncode d, BEncode e) | |||
566 | -- > , fileTags :: Maybe [Text] | 566 | -- > , fileTags :: Maybe [Text] |
567 | -- > } deriving (Show, Read, Eq) | 567 | -- > } deriving (Show, Read, Eq) |
568 | -- | 568 | -- |
569 | -- We need to make /instance BEncodable FileInfo/, though we don't | 569 | -- We need to make /instance BEncode FileInfo/, though we don't want |
570 | -- want to check the both /maybes/ manually. The more declarative and | 570 | -- to check the both /maybes/ manually. The more declarative and |
571 | -- convenient way to define the 'toBEncode' method is to use | 571 | -- convenient way to define the 'toBEncode' method is to use |
572 | -- dictionary builders: | 572 | -- dictionary builders: |
573 | -- | 573 | -- |
574 | -- > instance BEncodable FileInfo where | 574 | -- > instance BEncode FileInfo where |
575 | -- > toBEncode FileInfo {..} = fromAssocs | 575 | -- > toBEncode FileInfo {..} = toDict $ |
576 | -- > [ "length" --> fileLength | 576 | -- > "length" .=! fileLength |
577 | -- > , "md5sum" -->? fileMD5sum | 577 | -- > .: "md5sum" .=? fileMD5sum |
578 | -- > , "path" --> filePath | 578 | -- > .: "path" .=! filePath |
579 | -- > , "tags" -->? fileTags | 579 | -- > .: "tags" .=? fileTags |
580 | -- > ] | 580 | -- > .: endDict |
581 | -- > ... | 581 | -- |
582 | -- NOTE: the list of pair SHOULD be sorted lexicographically by keys, | ||
583 | -- so: "length" < "md5sum" < "path" < "tags". | ||
582 | -- | 584 | -- |
583 | data Assoc = Some !BKey BValue | 585 | data Assoc = Some !BKey BValue |
584 | | None | 586 | | None |
585 | 587 | ||
586 | -- TODO better name | ||
587 | -- | Make required key value pair. | 588 | -- | Make required key value pair. |
588 | (.=!) :: BEncode a => BKey -> a -> Assoc | 589 | (.=!) :: BEncode a => BKey -> a -> Assoc |
589 | (!k) .=! v = Some k (toBEncode v) | 590 | (!k) .=! v = Some k (toBEncode v) |
@@ -601,6 +602,7 @@ k .=? Just v = Some k (toBEncode v) | |||
601 | 602 | ||
602 | infix 6 .=? | 603 | infix 6 .=? |
603 | 604 | ||
605 | -- | Cons a key\/value pair. | ||
604 | (.:) :: Assoc -> BDict -> BDict | 606 | (.:) :: Assoc -> BDict -> BDict |
605 | None .: d = d | 607 | None .: d = d |
606 | Some k v .: d = Cons k v d | 608 | Some k v .: d = Cons k v d |
@@ -608,15 +610,12 @@ Some k v .: d = Cons k v d | |||
608 | 610 | ||
609 | infixr 5 .: | 611 | infixr 5 .: |
610 | 612 | ||
611 | -- | Build BEncode dictionary using key -> value description. | 613 | -- | Make a bencode value from dictionary description. |
612 | |||
613 | -- | A faster version of 'fromAssocs'. Should be used only when keys | ||
614 | -- in builder list are sorted by ascending. | ||
615 | -- | ||
616 | toDict :: BDict -> BValue | 614 | toDict :: BDict -> BValue |
617 | toDict = BDict | 615 | toDict = BDict |
618 | {-# INLINE toDict #-} | 616 | {-# INLINE toDict #-} |
619 | 617 | ||
618 | -- | Used to specify end of dictionary. See 'Assoc'. | ||
620 | endDict :: BDict | 619 | endDict :: BDict |
621 | endDict = Nil | 620 | endDict = Nil |
622 | {-# INLINE endDict #-} | 621 | {-# INLINE endDict #-} |
@@ -643,7 +642,7 @@ endDict = Nil | |||
643 | -- then whole destructuring fail. | 642 | -- then whole destructuring fail. |
644 | -- | 643 | -- |
645 | newtype Get a = Get { runGet :: StateT BDict Result a } | 644 | newtype Get a = Get { runGet :: StateT BDict Result a } |
646 | deriving (Functor, Applicative, Alternative) | 645 | deriving (Functor, Applicative, Alternative, Monad) |
647 | 646 | ||
648 | next :: Get BValue | 647 | next :: Get BValue |
649 | next = Get (StateT go) | 648 | next = Get (StateT go) |