diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-01 07:15:49 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-01 07:15:49 +0400 |
commit | 923ad43a0092f0b359d09edc4ac37539437d80cd (patch) | |
tree | 4121514194ad86b1c2d4e6a7c2bc56d61b962387 /src/Data/BEncode.hs | |
parent | 2c814d0ceb5566ff0223a48eb75ceab4bf0be187 (diff) |
+ better errors
Diffstat (limited to 'src/Data/BEncode.hs')
-rw-r--r-- | src/Data/BEncode.hs | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs index 5d4e14d..d2cc144 100644 --- a/src/Data/BEncode.hs +++ b/src/Data/BEncode.hs | |||
@@ -50,47 +50,62 @@ data BEncode = BInteger Int64 | |||
50 | | BDict Dict | 50 | | BDict Dict |
51 | deriving (Show, Read, Eq, Ord) | 51 | deriving (Show, Read, Eq, Ord) |
52 | 52 | ||
53 | type Result = Either String | ||
54 | |||
53 | class BEncodable a where | 55 | class BEncodable a where |
54 | toBEncode :: a -> BEncode | 56 | toBEncode :: a -> BEncode |
55 | fromBEncode :: BEncode -> Maybe a | 57 | fromBEncode :: BEncode -> Result a |
56 | -- isEncodable :: BEncode -> Bool | 58 | -- isEncodable :: BEncode -> Bool |
59 | -- bencoding :: Iso a | ||
60 | -- bencoding = Iso (Right . toBencode) fromBEncode | ||
61 | |||
62 | decodingError :: String -> Result a | ||
63 | decodingError s = Left ("fromBEncode: unable to match " ++ s) | ||
64 | {-# INLINE decodingError #-} | ||
57 | 65 | ||
58 | instance BEncodable BEncode where | 66 | instance BEncodable BEncode where |
59 | toBEncode = id | 67 | toBEncode = id |
60 | {-# INLINE toBEncode #-} | 68 | {-# INLINE toBEncode #-} |
61 | 69 | ||
62 | fromBEncode = Just | 70 | fromBEncode = Right |
63 | {-# INLINE fromBEncode #-} | 71 | {-# INLINE fromBEncode #-} |
64 | 72 | ||
65 | instance BEncodable Int where | 73 | instance BEncodable Int where |
66 | toBEncode = BInteger . fromIntegral | 74 | toBEncode = BInteger . fromIntegral |
67 | {-# INLINE toBEncode #-} | 75 | {-# INLINE toBEncode #-} |
68 | 76 | ||
69 | fromBEncode (BInteger i) = Just (fromIntegral i) | 77 | fromBEncode (BInteger i) = Right (fromIntegral i) |
70 | fromBEncode _ = Nothing | 78 | fromBEncode _ = decodingError "integer" |
71 | {-# INLINE fromBEncode #-} | 79 | {-# INLINE fromBEncode #-} |
72 | 80 | ||
81 | |||
73 | instance BEncodable Bool where | 82 | instance BEncodable Bool where |
74 | toBEncode = toBEncode . fromEnum | 83 | toBEncode = toBEncode . fromEnum |
75 | {-# INLINE toBEncode #-} | 84 | {-# INLINE toBEncode #-} |
76 | 85 | ||
77 | fromBEncode b = toEnum <$> fromBEncode b | 86 | fromBEncode b = do |
87 | i <- fromBEncode b | ||
88 | case i :: Int of | ||
89 | 0 -> return False | ||
90 | 1 -> return True | ||
91 | _ -> decodingError "bool" | ||
78 | {-# INLINE fromBEncode #-} | 92 | {-# INLINE fromBEncode #-} |
79 | 93 | ||
94 | |||
80 | instance BEncodable Integer where | 95 | instance BEncodable Integer where |
81 | toBEncode = BInteger . fromIntegral | 96 | toBEncode = BInteger . fromIntegral |
82 | {-# INLINE toBEncode #-} | 97 | {-# INLINE toBEncode #-} |
83 | 98 | ||
84 | fromBEncode (BInteger i) = Just (fromIntegral i) | 99 | fromBEncode b = fromIntegral <$> (fromBEncode b :: Result Int) |
85 | fromBEncode _ = Nothing | ||
86 | {-# INLINE fromBEncode #-} | 100 | {-# INLINE fromBEncode #-} |
87 | 101 | ||
102 | |||
88 | instance BEncodable ByteString where | 103 | instance BEncodable ByteString where |
89 | toBEncode = BString | 104 | toBEncode = BString |
90 | {-# INLINE toBEncode #-} | 105 | {-# INLINE toBEncode #-} |
91 | 106 | ||
92 | fromBEncode (BString s) = Just s | 107 | fromBEncode (BString s) = Right s |
93 | fromBEncode _ = Nothing | 108 | fromBEncode _ = decodingError "string" |
94 | {-# INLINE fromBEncode #-} | 109 | {-# INLINE fromBEncode #-} |
95 | 110 | ||
96 | {- | 111 | {- |
@@ -110,7 +125,7 @@ instance BEncodable a => BEncodable [a] where | |||
110 | {-# INLINE toBEncode #-} | 125 | {-# INLINE toBEncode #-} |
111 | 126 | ||
112 | fromBEncode (BList xs) = mapM fromBEncode xs | 127 | fromBEncode (BList xs) = mapM fromBEncode xs |
113 | fromBEncode _ = Nothing | 128 | fromBEncode _ = decodingError "list" |
114 | {-# INLINE fromBEncode #-} | 129 | {-# INLINE fromBEncode #-} |
115 | 130 | ||
116 | instance BEncodable a => BEncodable (Map ByteString a) where | 131 | instance BEncodable a => BEncodable (Map ByteString a) where |
@@ -120,7 +135,7 @@ instance BEncodable a => BEncodable (Map ByteString a) where | |||
120 | {-# INLINE toBEncode #-} | 135 | {-# INLINE toBEncode #-} |
121 | 136 | ||
122 | fromBEncode (BDict d) = traverse fromBEncode d | 137 | fromBEncode (BDict d) = traverse fromBEncode d |
123 | fromBEncode _ = Nothing | 138 | fromBEncode _ = decodingError "dictionary" |
124 | {-# INLINE fromBEncode #-} | 139 | {-# INLINE fromBEncode #-} |
125 | 140 | ||
126 | dictAssoc :: [(ByteString, BEncode)] -> BEncode | 141 | dictAssoc :: [(ByteString, BEncode)] -> BEncode |