summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-09-30 07:38:54 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-09-30 07:38:54 +0400
commite908445889d638bd1e892502584775d23078a1a2 (patch)
tree669fbc647c5358599a5d72c908648184a0ced65a
parent2e19d4347e5a3d4a0306bc4ffff0a0cafcdbb2be (diff)
Update documentation
-rw-r--r--src/Data/BEncode.hs33
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--
583data Assoc = Some !BKey BValue 585data 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
602infix 6 .=? 603infix 6 .=?
603 604
605-- | Cons a key\/value pair.
604(.:) :: Assoc -> BDict -> BDict 606(.:) :: Assoc -> BDict -> BDict
605None .: d = d 607None .: d = d
606Some k v .: d = Cons k v d 608Some k v .: d = Cons k v d
@@ -608,15 +610,12 @@ Some k v .: d = Cons k v d
608 610
609infixr 5 .: 611infixr 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--
616toDict :: BDict -> BValue 614toDict :: BDict -> BValue
617toDict = BDict 615toDict = BDict
618{-# INLINE toDict #-} 616{-# INLINE toDict #-}
619 617
618-- | Used to specify end of dictionary. See 'Assoc'.
620endDict :: BDict 619endDict :: BDict
621endDict = Nil 620endDict = 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--
645newtype Get a = Get { runGet :: StateT BDict Result a } 644newtype Get a = Get { runGet :: StateT BDict Result a }
646 deriving (Functor, Applicative, Alternative) 645 deriving (Functor, Applicative, Alternative, Monad)
647 646
648next :: Get BValue 647next :: Get BValue
649next = Get (StateT go) 648next = Get (StateT go)