diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-23 02:53:05 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-23 02:53:05 +0400 |
commit | 708938679954a5d5f9def82e1fa873c19ed662df (patch) | |
tree | 5edc53d4ad98fd61366169b34f2dd6bc5cdbc6d3 | |
parent | 41f229b5746501738800d721fb19083e32516f8c (diff) |
Add instance IsString for ClientInfo
-rw-r--r-- | bittorrent.cabal | 1 | ||||
-rw-r--r-- | src/Data/Torrent/Client.hs | 29 | ||||
-rw-r--r-- | tests/Data/Torrent/ClientSpec.hs | 6 |
3 files changed, 32 insertions, 4 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index 846b5b16..3c9c0d38 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -98,6 +98,7 @@ library | |||
98 | , data-default | 98 | , data-default |
99 | , IntervalMap | 99 | , IntervalMap |
100 | , intset | 100 | , intset |
101 | , split | ||
101 | , text >= 0.11.0 | 102 | , text >= 0.11.0 |
102 | , unordered-containers | 103 | , unordered-containers |
103 | , vector | 104 | , vector |
diff --git a/src/Data/Torrent/Client.hs b/src/Data/Torrent/Client.hs index a7e94513..f38f7a5c 100644 --- a/src/Data/Torrent/Client.hs +++ b/src/Data/Torrent/Client.hs | |||
@@ -34,10 +34,14 @@ import Data.ByteString as BS | |||
34 | import Data.ByteString.Char8 as BC | 34 | import Data.ByteString.Char8 as BC |
35 | import Data.Default | 35 | import Data.Default |
36 | import Data.List as L | 36 | import Data.List as L |
37 | import Data.List.Split as L | ||
38 | import Data.Maybe | ||
37 | import Data.Monoid | 39 | import Data.Monoid |
40 | import Data.String | ||
38 | import Data.Text as T | 41 | import Data.Text as T |
39 | import Data.Version | 42 | import Data.Version |
40 | import Text.PrettyPrint hiding ((<>)) | 43 | import Text.PrettyPrint hiding ((<>)) |
44 | import Text.Read (readMaybe) | ||
41 | import Paths_bittorrent (version) | 45 | import Paths_bittorrent (version) |
42 | 46 | ||
43 | 47 | ||
@@ -111,6 +115,15 @@ data ClientImpl = | |||
111 | -- | Used to represent a not recognized implementation | 115 | -- | Used to represent a not recognized implementation |
112 | instance Default ClientImpl where | 116 | instance Default ClientImpl where |
113 | def = IUnknown | 117 | def = IUnknown |
118 | {-# INLINE def #-} | ||
119 | |||
120 | instance IsString ClientImpl where | ||
121 | fromString str | ||
122 | | Just impl <- L.lookup str alist = impl | ||
123 | | otherwise = error $ "fromString: not recognized " ++ str | ||
124 | where | ||
125 | alist = L.map mk [minBound..maxBound] | ||
126 | mk x = (L.tail $ show x, x) | ||
114 | 127 | ||
115 | -- | Format client implementation info in human-readable form. | 128 | -- | Format client implementation info in human-readable form. |
116 | ppClientImpl :: ClientImpl -> Doc | 129 | ppClientImpl :: ClientImpl -> Doc |
@@ -119,6 +132,14 @@ ppClientImpl = text . L.tail . show | |||
119 | -- | Just the '0' version. | 132 | -- | Just the '0' version. |
120 | instance Default Version where | 133 | instance Default Version where |
121 | def = Version [0] [] | 134 | def = Version [0] [] |
135 | {-# INLINE def #-} | ||
136 | |||
137 | instance IsString Version where | ||
138 | fromString str | ||
139 | | Just nums <- chunkNums str = Version nums [] | ||
140 | | otherwise = error $ "fromString: invalid version string " ++ str | ||
141 | where | ||
142 | chunkNums = sequence . L.map readMaybe . L.linesBy ('.' ==) | ||
122 | 143 | ||
123 | -- | Format client implementation version in human-readable form. | 144 | -- | Format client implementation version in human-readable form. |
124 | ppVersion :: Version -> Doc | 145 | ppVersion :: Version -> Doc |
@@ -134,6 +155,14 @@ data ClientInfo = ClientInfo { | |||
134 | -- | Unrecognized client implementation. | 155 | -- | Unrecognized client implementation. |
135 | instance Default ClientInfo where | 156 | instance Default ClientInfo where |
136 | def = ClientInfo def def | 157 | def = ClientInfo def def |
158 | {-# INLINE def #-} | ||
159 | |||
160 | instance IsString ClientInfo where | ||
161 | fromString str | ||
162 | | _ : ver <- _ver = ClientInfo (fromString impl) (fromString ver) | ||
163 | | otherwise = error $ "fromString: invalid client info string" ++ str | ||
164 | where | ||
165 | (impl, _ver) = L.span ((/=) '-') str | ||
137 | 166 | ||
138 | -- | Format client info in human-readable form. | 167 | -- | Format client info in human-readable form. |
139 | ppClientInfo :: ClientInfo -> Doc | 168 | ppClientInfo :: ClientInfo -> Doc |
diff --git a/tests/Data/Torrent/ClientSpec.hs b/tests/Data/Torrent/ClientSpec.hs index 85ace5e0..890ee95e 100644 --- a/tests/Data/Torrent/ClientSpec.hs +++ b/tests/Data/Torrent/ClientSpec.hs | |||
@@ -10,7 +10,5 @@ spec :: Spec | |||
10 | spec = do | 10 | spec = do |
11 | describe "client info" $ do | 11 | describe "client info" $ do |
12 | it "decode azureus encoded peer id" $ do | 12 | it "decode azureus encoded peer id" $ do |
13 | clientInfo "-AZ2060-xxxxxxxxxxxx" `shouldBe` | 13 | clientInfo "-AZ2060-xxxxxxxxxxxx" `shouldBe` "Azureus-2060" |
14 | ClientInfo IAzureus (Version [2060] []) | 14 | clientInfo "-BS0000-xxxxxxxxxxxx" `shouldBe` "BTSlave-0" |
15 | clientInfo "-BS0000-xxxxxxxxxxxx" `shouldBe` | ||
16 | ClientInfo IBTSlave (Version [0] []) | ||