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 /src/Data/Torrent | |
parent | 41f229b5746501738800d721fb19083e32516f8c (diff) |
Add instance IsString for ClientInfo
Diffstat (limited to 'src/Data/Torrent')
-rw-r--r-- | src/Data/Torrent/Client.hs | 29 |
1 files changed, 29 insertions, 0 deletions
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 |