diff options
author | Joe Crayne <joe@jerkface.net> | 2018-12-12 01:12:25 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-12-16 14:08:26 -0500 |
commit | 1819d80705986d36c3264f60d05a5383c73bc33f (patch) | |
tree | 5d3c2b8980a4cc74988090c658e25da82bd2727a /src/Network/Tox/Crypto | |
parent | d27be4aaf8182bc8c5dd84c7b8cc7f5250614613 (diff) |
Reduced dependencies, updated cabal file.
Diffstat (limited to 'src/Network/Tox/Crypto')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 6e2ab60b..2c13e168 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -1,3 +1,4 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
1 | {-# LANGUAGE DataKinds #-} | 2 | {-# LANGUAGE DataKinds #-} |
2 | {-# LANGUAGE FlexibleInstances #-} | 3 | {-# LANGUAGE FlexibleInstances #-} |
3 | {-# LANGUAGE GADTs #-} | 4 | {-# LANGUAGE GADTs #-} |
@@ -43,8 +44,10 @@ module Network.Tox.Crypto.Transport | |||
43 | , HasMessage(..) | 44 | , HasMessage(..) |
44 | , HasMessageType(..) | 45 | , HasMessageType(..) |
45 | -- lenses | 46 | -- lenses |
47 | #ifdef VERSION_lens | ||
46 | , groupNumber, groupNumberToJoin, peerNumber, messageNumber | 48 | , groupNumber, groupNumberToJoin, peerNumber, messageNumber |
47 | , messageName, messageData, name, title, message, messageType | 49 | , messageName, messageData, name, title, message, messageType |
50 | #endif | ||
48 | -- constructor | 51 | -- constructor |
49 | -- utils | 52 | -- utils |
50 | , sizedN | 53 | , sizedN |
@@ -77,7 +80,8 @@ import Data.Monoid | |||
77 | import Data.Word | 80 | import Data.Word |
78 | import Data.Bits | 81 | import Data.Bits |
79 | import Crypto.Hash | 82 | import Crypto.Hash |
80 | import Control.Lens | 83 | import Data.Functor.Contravariant |
84 | import Data.Functor.Identity | ||
81 | import Data.Text as T | 85 | import Data.Text as T |
82 | import Data.Text.Encoding as T | 86 | import Data.Text.Encoding as T |
83 | import Data.Serialize as S | 87 | import Data.Serialize as S |
@@ -284,8 +288,10 @@ putCryptoMessage seqno (Pkt t :=> Identity x) = do | |||
284 | putPacket seqno x | 288 | putPacket seqno x |
285 | 289 | ||
286 | 290 | ||
291 | #ifdef VERSION_lens | ||
287 | erCompat :: String -> a | 292 | erCompat :: String -> a |
288 | erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type" | 293 | erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type" |
294 | #endif | ||
289 | 295 | ||
290 | 296 | ||
291 | newtype GroupChatId = GrpId ByteString -- 33 bytes | 297 | newtype GroupChatId = GrpId ByteString -- 33 bytes |
@@ -330,8 +336,10 @@ instance HasGroupChatID CryptoMessage where | |||
330 | setGroupChatID _ _= error "setGroupChatID on non-groupchat message." | 336 | setGroupChatID _ _= error "setGroupChatID on non-groupchat message." |
331 | -} | 337 | -} |
332 | 338 | ||
339 | #ifdef VERSION_lens | ||
333 | groupChatID :: (Functor f, HasGroupChatID x) => (GroupChatId -> f GroupChatId) -> (x -> f x) | 340 | groupChatID :: (Functor f, HasGroupChatID x) => (GroupChatId -> f GroupChatId) -> (x -> f x) |
334 | groupChatID = lens getGroupChatID setGroupChatID | 341 | groupChatID = lens getGroupChatID setGroupChatID |
342 | #endif | ||
335 | 343 | ||
336 | type GroupNumber = Word16 | 344 | type GroupNumber = Word16 |
337 | type PeerNumber = Word16 | 345 | type PeerNumber = Word16 |
@@ -362,8 +370,10 @@ instance HasGroupNumber CryptoMessage where | |||
362 | setGroupNumber _ _ = error "setGroupNumber on CryptoMessage without group number field." | 370 | setGroupNumber _ _ = error "setGroupNumber on CryptoMessage without group number field." |
363 | -} | 371 | -} |
364 | 372 | ||
373 | #ifdef VERSION_lens | ||
365 | groupNumber :: (Functor f, HasGroupNumber x) => (Word16 -> f Word16) -> (x -> f x) | 374 | groupNumber :: (Functor f, HasGroupNumber x) => (Word16 -> f Word16) -> (x -> f x) |
366 | groupNumber = lens getGroupNumber setGroupNumber | 375 | groupNumber = lens getGroupNumber setGroupNumber |
376 | #endif | ||
367 | 377 | ||
368 | class HasGroupNumberToJoin x where | 378 | class HasGroupNumberToJoin x where |
369 | getGroupNumberToJoin :: x -> GroupNumber | 379 | getGroupNumberToJoin :: x -> GroupNumber |
@@ -384,8 +394,10 @@ instance HasGroupNumberToJoin CryptoMessage where | |||
384 | setGroupNumberToJoin _ _ = error "setGroupNumberToJoin on CryptoMessage without group number (to join) field." | 394 | setGroupNumberToJoin _ _ = error "setGroupNumberToJoin on CryptoMessage without group number (to join) field." |
385 | -} | 395 | -} |
386 | 396 | ||
397 | #ifdef VERSION_lens | ||
387 | groupNumberToJoin :: (Functor f, HasGroupNumberToJoin x) => (GroupNumber -> f GroupNumber) -> (x -> f x) | 398 | groupNumberToJoin :: (Functor f, HasGroupNumberToJoin x) => (GroupNumber -> f GroupNumber) -> (x -> f x) |
388 | groupNumberToJoin = lens getGroupNumberToJoin setGroupNumberToJoin | 399 | groupNumberToJoin = lens getGroupNumberToJoin setGroupNumberToJoin |
400 | #endif | ||
389 | 401 | ||
390 | class HasPeerNumber x where | 402 | class HasPeerNumber x where |
391 | getPeerNumber :: x -> PeerNumber | 403 | getPeerNumber :: x -> PeerNumber |
@@ -406,8 +418,10 @@ instance HasPeerNumber CryptoMessage where | |||
406 | setPeerNumber _ _ = error "setPeerNumber on CryptoMessage without peer number field." | 418 | setPeerNumber _ _ = error "setPeerNumber on CryptoMessage without peer number field." |
407 | -} | 419 | -} |
408 | 420 | ||
421 | #ifdef VERSION_lens | ||
409 | peerNumber :: (Functor f, HasPeerNumber x) => (Word16 -> f Word16) -> (x -> f x) | 422 | peerNumber :: (Functor f, HasPeerNumber x) => (Word16 -> f Word16) -> (x -> f x) |
410 | peerNumber = lens getPeerNumber setPeerNumber | 423 | peerNumber = lens getPeerNumber setPeerNumber |
424 | #endif | ||
411 | 425 | ||
412 | class HasMessageNumber x where | 426 | class HasMessageNumber x where |
413 | getMessageNumber :: x -> MessageNumber | 427 | getMessageNumber :: x -> MessageNumber |
@@ -428,9 +442,10 @@ instance HasMessageNumber CryptoMessage where | |||
428 | setMessageNumber _ _ = error "setMessageNumber on CryptoMessage without message number field." | 442 | setMessageNumber _ _ = error "setMessageNumber on CryptoMessage without message number field." |
429 | -} | 443 | -} |
430 | 444 | ||
445 | #ifdef VERSION_lens | ||
431 | messageNumber :: (Functor f, HasMessageNumber x) => (Word32 -> f Word32) -> (x -> f x) | 446 | messageNumber :: (Functor f, HasMessageNumber x) => (Word32 -> f Word32) -> (x -> f x) |
432 | messageNumber = lens getMessageNumber setMessageNumber | 447 | messageNumber = lens getMessageNumber setMessageNumber |
433 | 448 | #endif | |
434 | 449 | ||
435 | class HasMessageName x where | 450 | class HasMessageName x where |
436 | getMessageName :: x -> MessageName | 451 | getMessageName :: x -> MessageName |
@@ -453,8 +468,10 @@ instance HasMessageName CryptoMessage where | |||
453 | setMessageName _ _ = error "setMessageName on CryptoMessage without message name field." | 468 | setMessageName _ _ = error "setMessageName on CryptoMessage without message name field." |
454 | -} | 469 | -} |
455 | 470 | ||
471 | #ifdef VERSION_lens | ||
456 | messageName :: (Functor f, HasMessageName x) => (MessageName -> f MessageName) -> (x -> f x) | 472 | messageName :: (Functor f, HasMessageName x) => (MessageName -> f MessageName) -> (x -> f x) |
457 | messageName = lens getMessageName setMessageName | 473 | messageName = lens getMessageName setMessageName |
474 | #endif | ||
458 | 475 | ||
459 | data KnownLossyness = KnownLossy | KnownLossless | 476 | data KnownLossyness = KnownLossy | KnownLossless |
460 | deriving (Eq,Ord,Show,Enum) | 477 | deriving (Eq,Ord,Show,Enum) |
@@ -497,8 +514,10 @@ instance AsWord64 MessageType where | |||
497 | fromWord64 x | x < 1024, x .|. 0x0200 == 0x0200 = GrpMsg (toEnum8 ((x - 512) `div` 256)) (toEnum8 x) | 514 | fromWord64 x | x < 1024, x .|. 0x0200 == 0x0200 = GrpMsg (toEnum8 ((x - 512) `div` 256)) (toEnum8 x) |
498 | fromWord64 x = error "Not clear how to convert Word64 to MessageType" | 515 | fromWord64 x = error "Not clear how to convert Word64 to MessageType" |
499 | 516 | ||
517 | #ifdef VERSION_lens | ||
500 | word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) | 518 | word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) |
501 | word16 = lens toWord16 (\_ x -> fromWord16 x) | 519 | word16 = lens toWord16 (\_ x -> fromWord16 x) |
520 | #endif | ||
502 | 521 | ||
503 | instance Ord MessageType where | 522 | instance Ord MessageType where |
504 | compare (Msg x) (Msg y) = compare x y | 523 | compare (Msg x) (Msg y) = compare x y |
@@ -540,9 +559,11 @@ instance HasMessageType CryptoData where | |||
540 | setMessageType cd@(CryptoData { bufferData=bd }) typ = cd { bufferData=setMessageType bd typ } | 559 | setMessageType cd@(CryptoData { bufferData=bd }) typ = cd { bufferData=setMessageType bd typ } |
541 | -} | 560 | -} |
542 | 561 | ||
562 | #ifdef VERSION_lens | ||
543 | -- | This lens should always succeed on CryptoMessage | 563 | -- | This lens should always succeed on CryptoMessage |
544 | messageType :: (Functor f, HasMessageType x) => (MessageType -> f MessageType) -> (x -> f x) | 564 | messageType :: (Functor f, HasMessageType x) => (MessageType -> f MessageType) -> (x -> f x) |
545 | messageType = lens getMessageType setMessageType | 565 | messageType = lens getMessageType setMessageType |
566 | #endif | ||
546 | 567 | ||
547 | type MessageData = B.ByteString | 568 | type MessageData = B.ByteString |
548 | 569 | ||
@@ -568,8 +589,10 @@ instance HasMessageData CryptoMessage where | |||
568 | setMessageData _ _ = error "setMessageData on CryptoMessage without message data field." | 589 | setMessageData _ _ = error "setMessageData on CryptoMessage without message data field." |
569 | -} | 590 | -} |
570 | 591 | ||
592 | #ifdef VERSION_lens | ||
571 | messageData :: (Functor f, HasMessageData x) => (MessageData -> f MessageData) -> (x -> f x) | 593 | messageData :: (Functor f, HasMessageData x) => (MessageData -> f MessageData) -> (x -> f x) |
572 | messageData = lens getMessageData setMessageData | 594 | messageData = lens getMessageData setMessageData |
595 | #endif | ||
573 | 596 | ||
574 | class HasTitle x where | 597 | class HasTitle x where |
575 | getTitle :: x -> Text | 598 | getTitle :: x -> Text |
@@ -598,8 +621,10 @@ instance HasTitle CryptoMessage where | |||
598 | setTitle _ _ = error "setTitle on CryptoMessage without title field." | 621 | setTitle _ _ = error "setTitle on CryptoMessage without title field." |
599 | -} | 622 | -} |
600 | 623 | ||
624 | #ifdef VERSION_lens | ||
601 | title :: (Functor f, HasTitle x) => (Text -> f Text) -> (x -> f x) | 625 | title :: (Functor f, HasTitle x) => (Text -> f Text) -> (x -> f x) |
602 | title = lens getTitle setTitle | 626 | title = lens getTitle setTitle |
627 | #endif | ||
603 | 628 | ||
604 | class HasMessage x where | 629 | class HasMessage x where |
605 | getMessage :: x -> Text | 630 | getMessage :: x -> Text |
@@ -628,8 +653,10 @@ instance HasMessage CryptoMessage where | |||
628 | setMessage _ _ = error "setMessage on CryptoMessage without message field." | 653 | setMessage _ _ = error "setMessage on CryptoMessage without message field." |
629 | -} | 654 | -} |
630 | 655 | ||
656 | #ifdef VERSION_lens | ||
631 | message :: (Functor f, HasMessage x) => (Text -> f Text) -> (x -> f x) | 657 | message :: (Functor f, HasMessage x) => (Text -> f Text) -> (x -> f x) |
632 | message = lens getMessage setMessage | 658 | message = lens getMessage setMessage |
659 | #endif | ||
633 | 660 | ||
634 | class HasName x where | 661 | class HasName x where |
635 | getName :: x -> Text | 662 | getName :: x -> Text |
@@ -648,8 +675,10 @@ instance HasName CryptoMessage where | |||
648 | setName _ _ = error "setName on CryptoMessage without name field." | 675 | setName _ _ = error "setName on CryptoMessage without name field." |
649 | -} | 676 | -} |
650 | 677 | ||
678 | #ifdef VERSION_lens | ||
651 | name :: (Functor f, HasTitle x) => (Text -> f Text) -> (x -> f x) | 679 | name :: (Functor f, HasTitle x) => (Text -> f Text) -> (x -> f x) |
652 | name = lens getTitle setTitle | 680 | name = lens getTitle setTitle |
681 | #endif | ||
653 | 682 | ||
654 | data PeerInfo | 683 | data PeerInfo |
655 | = PeerInfo | 684 | = PeerInfo |