| 1 |
-- | Backward-compatibility shim for (de-)serializing 'Nick's |
| 2 |
-- using the old 'Read'/'Show' instances which gave freenode |
| 3 |
-- special treatment. |
| 4 |
module Lambdabot.Compat.FreenodeNick |
| 5 |
( FreenodeNick(..) |
| 6 |
, freenodeNickMapSerial |
| 7 |
) where |
| 8 |
|
| 9 |
import Control.Arrow |
| 10 |
import qualified Data.Map as M |
| 11 |
import Lambdabot.Nick |
| 12 |
import Lambdabot.Util.Serial |
| 13 |
|
| 14 |
newtype FreenodeNick = FreenodeNick { getFreenodeNick :: Nick } |
| 15 |
deriving (Eq, Ord) |
| 16 |
|
| 17 |
instance Show FreenodeNick where |
| 18 |
show (FreenodeNick x) |
| 19 |
| nTag x == "freenode" = show $ nName x |
| 20 |
| otherwise = show $ pckStr x |
| 21 |
|
| 22 |
instance Read FreenodeNick where |
| 23 |
readsPrec prec str = map (first (FreenodeNick . upckStr "freenode")) (readsPrec prec str) |
| 24 |
|
| 25 |
-- Helper functions |
| 26 |
upckStr :: String -> String -> Nick |
| 27 |
upckStr def str |
| 28 |
| null ac = Nick def str |
| 29 |
| otherwise = Nick bc (tail ac) |
| 30 |
where (bc, ac) = break (==':') str |
| 31 |
|
| 32 |
pckStr :: Nick -> String |
| 33 |
pckStr nck = nTag nck ++ ':' : nName nck |
| 34 |
|
| 35 |
freenodeNickMapSerial :: (Show v, Read v) => Serial (M.Map Nick v) |
| 36 |
freenodeNickMapSerial = Serial |
| 37 |
(serialize mapSerial . M.mapKeysMonotonic FreenodeNick) |
| 38 |
(fmap (M.mapKeysMonotonic getFreenodeNick) . deserialize mapSerial) |