summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-09-28 05:59:08 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-09-28 05:59:08 +0400
commitf8d3081122e35c96e291c61ad53c5da6dcda1fb2 (patch)
treee7f21f2639f38592c789d0574a6206eb17b34c3d
parent51fecc396f8c7ccd3f91125d5fbac040051915c6 (diff)
Rename BEncoding to BEncode
-rw-r--r--bench/Main.hs2
-rw-r--r--src/Data/BEncode.hs86
-rw-r--r--tests/properties.hs6
3 files changed, 47 insertions, 47 deletions
diff --git a/bench/Main.hs b/bench/Main.hs
index 0822395..ef9f08c 100644
--- a/bench/Main.hs
+++ b/bench/Main.hs
@@ -37,7 +37,7 @@ getRight = either error id
37data List a = Cons a (List a) | Nil 37data List a = Cons a (List a) | Nil
38 deriving Generic 38 deriving Generic
39 39
40instance BEncodable a => BEncodable (List a) 40instance C.BEncode a => C.BEncode (List a)
41 41
42instance NFData a => NFData (List a) where 42instance NFData a => NFData (List a) where
43 rnf Nil = () 43 rnf Nil = ()
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs
index 4133ae5..b6897ec 100644
--- a/src/Data/BEncode.hs
+++ b/src/Data/BEncode.hs
@@ -66,7 +66,7 @@ module Data.BEncode
66 , ppBEncode 66 , ppBEncode
67 67
68 -- * Conversion 68 -- * Conversion
69 , BEncodable (..) 69 , BEncode (..)
70 , Result 70 , Result
71 71
72 -- * Serialization 72 -- * Serialization
@@ -187,7 +187,7 @@ type Result = Either String
187-- 187--
188-- Note that '_' prefixes are omitted. 188-- Note that '_' prefixes are omitted.
189-- 189--
190class BEncodable a where 190class BEncode a where
191 -- | See an example of implementation here 'Assoc' 191 -- | See an example of implementation here 'Assoc'
192 toBEncode :: a -> BValue 192 toBEncode :: a -> BValue
193 193
@@ -237,7 +237,7 @@ class GBEncodable f e where
237 gto :: f a -> e 237 gto :: f a -> e
238 gfrom :: e -> Result (f a) 238 gfrom :: e -> Result (f a)
239 239
240instance BEncodable f 240instance BEncode f
241 => GBEncodable (K1 R f) BValue where 241 => GBEncodable (K1 R f) BValue where
242 {-# INLINE gto #-} 242 {-# INLINE gto #-}
243 gto = toBEncode . unK1 243 gto = toBEncode . unK1
@@ -344,14 +344,14 @@ instance GBEncodable f e
344-- Native instances 344-- Native instances
345--------------------------------------------------------------------} 345--------------------------------------------------------------------}
346 346
347instance BEncodable BValue where 347instance BEncode BValue where
348 toBEncode = id 348 toBEncode = id
349 {-# INLINE toBEncode #-} 349 {-# INLINE toBEncode #-}
350 350
351 fromBEncode = pure 351 fromBEncode = pure
352 {-# INLINE fromBEncode #-} 352 {-# INLINE fromBEncode #-}
353 353
354instance BEncodable BInteger where 354instance BEncode BInteger where
355 toBEncode = BInteger 355 toBEncode = BInteger
356 {-# INLINE toBEncode #-} 356 {-# INLINE toBEncode #-}
357 357
@@ -359,7 +359,7 @@ instance BEncodable BInteger where
359 fromBEncode _ = decodingError "BInteger" 359 fromBEncode _ = decodingError "BInteger"
360 {-# INLINE fromBEncode #-} 360 {-# INLINE fromBEncode #-}
361 361
362instance BEncodable BString where 362instance BEncode BString where
363 toBEncode = BString 363 toBEncode = BString
364 {-# INLINE toBEncode #-} 364 {-# INLINE toBEncode #-}
365 365
@@ -405,70 +405,70 @@ fromBEncodeIntegral _
405{-# INLINE fromBEncodeIntegral #-} 405{-# INLINE fromBEncodeIntegral #-}
406 406
407 407
408instance BEncodable Word8 where 408instance BEncode Word8 where
409 toBEncode = toBEncodeIntegral 409 toBEncode = toBEncodeIntegral
410 {-# INLINE toBEncode #-} 410 {-# INLINE toBEncode #-}
411 411
412 fromBEncode = fromBEncodeIntegral 412 fromBEncode = fromBEncodeIntegral
413 {-# INLINE fromBEncode #-} 413 {-# INLINE fromBEncode #-}
414 414
415instance BEncodable Word16 where 415instance BEncode Word16 where
416 toBEncode = toBEncodeIntegral 416 toBEncode = toBEncodeIntegral
417 {-# INLINE toBEncode #-} 417 {-# INLINE toBEncode #-}
418 418
419 fromBEncode = fromBEncodeIntegral 419 fromBEncode = fromBEncodeIntegral
420 {-# INLINE fromBEncode #-} 420 {-# INLINE fromBEncode #-}
421 421
422instance BEncodable Word32 where 422instance BEncode Word32 where
423 toBEncode = toBEncodeIntegral 423 toBEncode = toBEncodeIntegral
424 {-# INLINE toBEncode #-} 424 {-# INLINE toBEncode #-}
425 425
426 fromBEncode = fromBEncodeIntegral 426 fromBEncode = fromBEncodeIntegral
427 {-# INLINE fromBEncode #-} 427 {-# INLINE fromBEncode #-}
428 428
429instance BEncodable Word64 where 429instance BEncode Word64 where
430 toBEncode = toBEncodeIntegral 430 toBEncode = toBEncodeIntegral
431 {-# INLINE toBEncode #-} 431 {-# INLINE toBEncode #-}
432 432
433 fromBEncode = fromBEncodeIntegral 433 fromBEncode = fromBEncodeIntegral
434 {-# INLINE fromBEncode #-} 434 {-# INLINE fromBEncode #-}
435 435
436instance BEncodable Word where 436instance BEncode Word where
437 toBEncode = toBEncodeIntegral 437 toBEncode = toBEncodeIntegral
438 {-# INLINE toBEncode #-} 438 {-# INLINE toBEncode #-}
439 439
440 fromBEncode = fromBEncodeIntegral 440 fromBEncode = fromBEncodeIntegral
441 {-# INLINE fromBEncode #-} 441 {-# INLINE fromBEncode #-}
442 442
443instance BEncodable Int8 where 443instance BEncode Int8 where
444 toBEncode = toBEncodeIntegral 444 toBEncode = toBEncodeIntegral
445 {-# INLINE toBEncode #-} 445 {-# INLINE toBEncode #-}
446 446
447 fromBEncode = fromBEncodeIntegral 447 fromBEncode = fromBEncodeIntegral
448 {-# INLINE fromBEncode #-} 448 {-# INLINE fromBEncode #-}
449 449
450instance BEncodable Int16 where 450instance BEncode Int16 where
451 toBEncode = toBEncodeIntegral 451 toBEncode = toBEncodeIntegral
452 {-# INLINE toBEncode #-} 452 {-# INLINE toBEncode #-}
453 453
454 fromBEncode = fromBEncodeIntegral 454 fromBEncode = fromBEncodeIntegral
455 {-# INLINE fromBEncode #-} 455 {-# INLINE fromBEncode #-}
456 456
457instance BEncodable Int32 where 457instance BEncode Int32 where
458 toBEncode = toBEncodeIntegral 458 toBEncode = toBEncodeIntegral
459 {-# INLINE toBEncode #-} 459 {-# INLINE toBEncode #-}
460 460
461 fromBEncode = fromBEncodeIntegral 461 fromBEncode = fromBEncodeIntegral
462 {-# INLINE fromBEncode #-} 462 {-# INLINE fromBEncode #-}
463 463
464instance BEncodable Int64 where 464instance BEncode Int64 where
465 toBEncode = toBEncodeIntegral 465 toBEncode = toBEncodeIntegral
466 {-# INLINE toBEncode #-} 466 {-# INLINE toBEncode #-}
467 467
468 fromBEncode = fromBEncodeIntegral 468 fromBEncode = fromBEncodeIntegral
469 {-# INLINE fromBEncode #-} 469 {-# INLINE fromBEncode #-}
470 470
471instance BEncodable Int where 471instance BEncode Int where
472 toBEncode = toBEncodeIntegral 472 toBEncode = toBEncodeIntegral
473 {-# INLINE toBEncode #-} 473 {-# INLINE toBEncode #-}
474 474
@@ -479,7 +479,7 @@ instance BEncodable Int where
479-- Derived instances 479-- Derived instances
480--------------------------------------------------------------------} 480--------------------------------------------------------------------}
481 481
482instance BEncodable Bool where 482instance BEncode Bool where
483 toBEncode = toBEncode . fromEnum 483 toBEncode = toBEncode . fromEnum
484 {-# INLINE toBEncode #-} 484 {-# INLINE toBEncode #-}
485 485
@@ -491,15 +491,15 @@ instance BEncodable Bool where
491 _ -> decodingError "Bool" 491 _ -> decodingError "Bool"
492 {-# INLINE fromBEncode #-} 492 {-# INLINE fromBEncode #-}
493 493
494instance BEncodable Text where 494instance BEncode Text where
495 toBEncode = toBEncode . T.encodeUtf8 495 toBEncode = toBEncode . T.encodeUtf8
496 {-# INLINE toBEncode #-} 496 {-# INLINE toBEncode #-}
497 497
498 fromBEncode b = T.decodeUtf8 <$> fromBEncode b 498 fromBEncode b = T.decodeUtf8 <$> fromBEncode b
499 {-# INLINE fromBEncode #-} 499 {-# INLINE fromBEncode #-}
500 500
501instance BEncodable a => BEncodable [a] where 501instance BEncode a => BEncode [a] where
502 {-# SPECIALIZE instance BEncodable BList #-} 502 {-# SPECIALIZE instance BEncode BList #-}
503 toBEncode = BList . map toBEncode 503 toBEncode = BList . map toBEncode
504 {-# INLINE toBEncode #-} 504 {-# INLINE toBEncode #-}
505 505
@@ -507,8 +507,8 @@ instance BEncodable a => BEncodable [a] where
507 fromBEncode _ = decodingError "list" 507 fromBEncode _ = decodingError "list"
508 {-# INLINE fromBEncode #-} 508 {-# INLINE fromBEncode #-}
509 509
510instance BEncodable a => BEncodable (Map ByteString a) where 510instance BEncode a => BEncode (Map BKey a) where
511 {-# SPECIALIZE instance BEncodable BDict #-} 511 {-# SPECIALIZE instance BEncode BDict #-}
512 toBEncode = BDict . M.map toBEncode 512 toBEncode = BDict . M.map toBEncode
513 {-# INLINE toBEncode #-} 513 {-# INLINE toBEncode #-}
514 514
@@ -516,8 +516,8 @@ instance BEncodable a => BEncodable (Map ByteString a) where
516 fromBEncode _ = decodingError "dictionary" 516 fromBEncode _ = decodingError "dictionary"
517 {-# INLINE fromBEncode #-} 517 {-# INLINE fromBEncode #-}
518 518
519instance (Eq a, BEncodable a) => BEncodable (Set a) where 519instance (Eq a, BEncode a) => BEncode (Set a) where
520 {-# SPECIALIZE instance BEncodable (Set BValue) #-} 520 {-# SPECIALIZE instance BEncode (Set BValue) #-}
521 toBEncode = BList . map toBEncode . S.toAscList 521 toBEncode = BList . map toBEncode . S.toAscList
522 {-# INLINE toBEncode #-} 522 {-# INLINE toBEncode #-}
523 523
@@ -525,7 +525,7 @@ instance (Eq a, BEncodable a) => BEncodable (Set a) where
525 fromBEncode _ = decodingError "Data.Set" 525 fromBEncode _ = decodingError "Data.Set"
526 {-# INLINE fromBEncode #-} 526 {-# INLINE fromBEncode #-}
527 527
528instance BEncodable Version where 528instance BEncode Version where
529 toBEncode = toBEncode . BC.pack . showVersion 529 toBEncode = toBEncode . BC.pack . showVersion
530 {-# INLINE toBEncode #-} 530 {-# INLINE toBEncode #-}
531 531
@@ -539,7 +539,7 @@ instance BEncodable Version where
539-- Tuple instances 539-- Tuple instances
540--------------------------------------------------------------------} 540--------------------------------------------------------------------}
541 541
542instance BEncodable () where 542instance BEncode () where
543 toBEncode () = BList [] 543 toBEncode () = BList []
544 {-# INLINE toBEncode #-} 544 {-# INLINE toBEncode #-}
545 545
@@ -547,10 +547,10 @@ instance BEncodable () where
547 fromBEncode _ = decodingError "Unable to decode unit value" 547 fromBEncode _ = decodingError "Unable to decode unit value"
548 {-# INLINE fromBEncode #-} 548 {-# INLINE fromBEncode #-}
549 549
550instance (BEncodable a, BEncodable b) => BEncodable (a, b) where 550instance (BEncode a, BEncode b) => BEncode (a, b) where
551 {-# SPECIALIZE instance (BEncodable b) => BEncodable (BValue, b) #-} 551 {-# SPECIALIZE instance (BEncode b) => BEncode (BValue, b) #-}
552 {-# SPECIALIZE instance (BEncodable a) => BEncodable (a, BValue) #-} 552 {-# SPECIALIZE instance (BEncode a) => BEncode (a, BValue) #-}
553 {-# SPECIALIZE instance BEncodable (BValue, BValue) #-} 553 {-# SPECIALIZE instance BEncode (BValue, BValue) #-}
554 toBEncode (a, b) = BList [toBEncode a, toBEncode b] 554 toBEncode (a, b) = BList [toBEncode a, toBEncode b]
555 {-# INLINE toBEncode #-} 555 {-# INLINE toBEncode #-}
556 556
@@ -558,7 +558,7 @@ instance (BEncodable a, BEncodable b) => BEncodable (a, b) where
558 fromBEncode _ = decodingError "Unable to decode a pair." 558 fromBEncode _ = decodingError "Unable to decode a pair."
559 {-# INLINE fromBEncode #-} 559 {-# INLINE fromBEncode #-}
560 560
561instance (BEncodable a, BEncodable b, BEncodable c) => BEncodable (a, b, c) where 561instance (BEncode a, BEncode b, BEncode c) => BEncode (a, b, c) where
562 toBEncode (a, b, c) = BList [toBEncode a, toBEncode b, toBEncode c] 562 toBEncode (a, b, c) = BList [toBEncode a, toBEncode b, toBEncode c]
563 {-# INLINE toBEncode #-} 563 {-# INLINE toBEncode #-}
564 564
@@ -567,8 +567,8 @@ instance (BEncodable a, BEncodable b, BEncodable c) => BEncodable (a, b, c) wher
567 fromBEncode _ = decodingError "Unable to decode a triple" 567 fromBEncode _ = decodingError "Unable to decode a triple"
568 {-# INLINE fromBEncode #-} 568 {-# INLINE fromBEncode #-}
569 569
570instance (BEncodable a, BEncodable b, BEncodable c, BEncodable d) 570instance (BEncode a, BEncode b, BEncode c, BEncode d)
571 => BEncodable (a, b, c, d) where 571 => BEncode (a, b, c, d) where
572 toBEncode (a, b, c, d) = BList [ toBEncode a, toBEncode b 572 toBEncode (a, b, c, d) = BList [ toBEncode a, toBEncode b
573 , toBEncode c, toBEncode d 573 , toBEncode c, toBEncode d
574 ] 574 ]
@@ -580,8 +580,8 @@ instance (BEncodable a, BEncodable b, BEncodable c, BEncodable d)
580 fromBEncode _ = decodingError "Unable to decode a tuple4" 580 fromBEncode _ = decodingError "Unable to decode a tuple4"
581 {-# INLINE fromBEncode #-} 581 {-# INLINE fromBEncode #-}
582 582
583instance (BEncodable a, BEncodable b, BEncodable c, BEncodable d, BEncodable e) 583instance (BEncode a, BEncode b, BEncode c, BEncode d, BEncode e)
584 => BEncodable (a, b, c, d, e) where 584 => BEncode (a, b, c, d, e) where
585 toBEncode (a, b, c, d, e) = BList [ toBEncode a, toBEncode b 585 toBEncode (a, b, c, d, e) = BList [ toBEncode a, toBEncode b
586 , toBEncode c, toBEncode d 586 , toBEncode c, toBEncode d
587 , toBEncode e 587 , toBEncode e
@@ -626,14 +626,14 @@ instance (BEncodable a, BEncodable b, BEncodable c, BEncodable d, BEncodable e)
626newtype Assoc = Assoc { unAssoc :: Maybe (ByteString, BValue) } 626newtype Assoc = Assoc { unAssoc :: Maybe (ByteString, BValue) }
627 627
628-- | Make required key value pair. 628-- | Make required key value pair.
629(-->) :: BEncodable a => ByteString -> a -> Assoc 629(-->) :: BEncode a => BKey -> a -> Assoc
630key --> val = Assoc $ Just $ (key, toBEncode val) 630key --> val = Assoc $ Just $ (key, toBEncode val)
631{-# INLINE (-->) #-} 631{-# INLINE (-->) #-}
632 632
633-- | Like (-->) but if the value is not present then the key do not 633-- | Like (-->) but if the value is not present then the key do not
634-- appear in resulting bencoded dictionary. 634-- appear in resulting bencoded dictionary.
635-- 635--
636(-->?) :: BEncodable a => ByteString -> Maybe a -> Assoc 636(-->?) :: BEncode a => BKey -> Maybe a -> Assoc
637key -->? mval = Assoc $ ((,) key . toBEncode) <$> mval 637key -->? mval = Assoc $ ((,) key . toBEncode) <$> mval
638{-# INLINE (-->?) #-} 638{-# INLINE (-->?) #-}
639 639
@@ -668,7 +668,7 @@ fromAscAssocs = BDict . M.fromAscList . mapMaybe unAssoc
668-- 668--
669-- The /reqKey/ is used to extract required key — if lookup is failed 669-- The /reqKey/ is used to extract required key — if lookup is failed
670-- then whole destructuring fail. 670-- then whole destructuring fail.
671reqKey :: BEncodable a => BDict -> BKey -> Result a 671reqKey :: BEncode a => BDict -> BKey -> Result a
672reqKey d key 672reqKey d key
673 | Just b <- M.lookup key d = fromBEncode b 673 | Just b <- M.lookup key d = fromBEncode b
674 | otherwise = Left msg 674 | otherwise = Left msg
@@ -677,19 +677,19 @@ reqKey d key
677 677
678-- | Used to extract optional key — if lookup is failed returns 678-- | Used to extract optional key — if lookup is failed returns
679-- 'Nothing'. 679-- 'Nothing'.
680optKey :: BEncodable a => BDict -> BKey -> Result (Maybe a) 680optKey :: BEncode a => BDict -> BKey -> Result (Maybe a)
681optKey d key 681optKey d key
682 | Just b <- M.lookup key d 682 | Just b <- M.lookup key d
683 , Right r <- fromBEncode b = return (Just r) 683 , Right r <- fromBEncode b = return (Just r)
684 | otherwise = return Nothing 684 | otherwise = return Nothing
685 685
686-- | Infix version of the 'reqKey'. 686-- | Infix version of the 'reqKey'.
687(>--) :: BEncodable a => BDict -> BKey -> Result a 687(>--) :: BEncode a => BDict -> BKey -> Result a
688(>--) = reqKey 688(>--) = reqKey
689{-# INLINE (>--) #-} 689{-# INLINE (>--) #-}
690 690
691-- | Infix version of the 'optKey'. 691-- | Infix version of the 'optKey'.
692(>--?) :: BEncodable a => BDict -> BKey -> Result (Maybe a) 692(>--?) :: BEncode a => BDict -> BKey -> Result (Maybe a)
693(>--?) = optKey 693(>--?) = optKey
694{-# INLINE (>--?) #-} 694{-# INLINE (>--?) #-}
695 695
@@ -736,11 +736,11 @@ decode :: ByteString -> Result BValue
736decode = P.parseOnly parser 736decode = P.parseOnly parser
737 737
738-- | The same as 'decode' but returns any bencodable value. 738-- | The same as 'decode' but returns any bencodable value.
739decoded :: BEncodable a => ByteString -> Result a 739decoded :: BEncode a => ByteString -> Result a
740decoded = decode >=> fromBEncode 740decoded = decode >=> fromBEncode
741 741
742-- | The same as 'encode' but takes any bencodable value. 742-- | The same as 'encode' but takes any bencodable value.
743encoded :: BEncodable a => a -> Lazy.ByteString 743encoded :: BEncode a => a -> Lazy.ByteString
744encoded = encode . toBEncode 744encoded = encode . toBEncode
745 745
746{-------------------------------------------------------------------- 746{--------------------------------------------------------------------
diff --git a/tests/properties.hs b/tests/properties.hs
index 0d49445..586e94d 100644
--- a/tests/properties.hs
+++ b/tests/properties.hs
@@ -32,7 +32,7 @@ prop_EncDec x = case decode (L.toStrict (encode x)) of
32data List a = Cons a (List a) | Nil 32data List a = Cons a (List a) | Nil
33 deriving (Show, Eq, Generic) 33 deriving (Show, Eq, Generic)
34 34
35instance BEncodable a => BEncodable (List a) 35instance BEncode a => BEncode (List a)
36 36
37instance Arbitrary a => Arbitrary (List a) where 37instance Arbitrary a => Arbitrary (List a) where
38 arbitrary = frequency 38 arbitrary = frequency
@@ -46,14 +46,14 @@ data FileInfo = FileInfo
46 , fiMD5Sum :: B.ByteString 46 , fiMD5Sum :: B.ByteString
47 } deriving (Show, Eq, Generic) 47 } deriving (Show, Eq, Generic)
48 48
49instance BEncodable FileInfo 49instance BEncode FileInfo
50 50
51instance Arbitrary FileInfo where 51instance Arbitrary FileInfo where
52 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary 52 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
53 53
54data T a = T 54data T a = T
55 55
56prop_bencodable :: Eq a => BEncodable a => T a -> a -> Bool 56prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool
57prop_bencodable _ x = decoded (L.toStrict (encoded x)) == Right x 57prop_bencodable _ x = decoded (L.toStrict (encoded x)) == Right x
58 58
59-- All tests are (encode >>> decode = id) 59-- All tests are (encode >>> decode = id)