-- | Backward-compatibility shim for (de-)serializing 'Nick's
-- using the old 'Read'/'Show' instances which gave freenode
-- special treatment.
module Lambdabot.Compat.FreenodeNick
    ( FreenodeNick(..)
    , freenodeNickMapSerial
    ) where

import Control.Arrow
import qualified Data.Map as M
import Lambdabot.Nick
import Lambdabot.Util.Serial

newtype FreenodeNick = FreenodeNick { FreenodeNick -> Nick
getFreenodeNick :: Nick }
    deriving (FreenodeNick -> FreenodeNick -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FreenodeNick -> FreenodeNick -> Bool
$c/= :: FreenodeNick -> FreenodeNick -> Bool
== :: FreenodeNick -> FreenodeNick -> Bool
$c== :: FreenodeNick -> FreenodeNick -> Bool
Eq, Eq FreenodeNick
FreenodeNick -> FreenodeNick -> Bool
FreenodeNick -> FreenodeNick -> Ordering
FreenodeNick -> FreenodeNick -> FreenodeNick
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FreenodeNick -> FreenodeNick -> FreenodeNick
$cmin :: FreenodeNick -> FreenodeNick -> FreenodeNick
max :: FreenodeNick -> FreenodeNick -> FreenodeNick
$cmax :: FreenodeNick -> FreenodeNick -> FreenodeNick
>= :: FreenodeNick -> FreenodeNick -> Bool
$c>= :: FreenodeNick -> FreenodeNick -> Bool
> :: FreenodeNick -> FreenodeNick -> Bool
$c> :: FreenodeNick -> FreenodeNick -> Bool
<= :: FreenodeNick -> FreenodeNick -> Bool
$c<= :: FreenodeNick -> FreenodeNick -> Bool
< :: FreenodeNick -> FreenodeNick -> Bool
$c< :: FreenodeNick -> FreenodeNick -> Bool
compare :: FreenodeNick -> FreenodeNick -> Ordering
$ccompare :: FreenodeNick -> FreenodeNick -> Ordering
Ord)

instance Show FreenodeNick where
    show :: FreenodeNick -> String
show (FreenodeNick Nick
x)
        | Nick -> String
nTag Nick
x forall a. Eq a => a -> a -> Bool
== String
"freenode" = forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ Nick -> String
nName Nick
x
        | Bool
otherwise            = forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ Nick -> String
pckStr Nick
x

instance Read FreenodeNick where
    readsPrec :: Int -> ReadS FreenodeNick
readsPrec Int
prec String
str = forall a b. (a -> b) -> [a] -> [b]
map (forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (Nick -> FreenodeNick
FreenodeNick forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> Nick
upckStr String
"freenode")) (forall a. Read a => Int -> ReadS a
readsPrec Int
prec String
str)

-- Helper functions
upckStr :: String -> String -> Nick
upckStr :: String -> String -> Nick
upckStr String
def String
str
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ac   = String -> String -> Nick
Nick String
def String
str
    | Bool
otherwise = String -> String -> Nick
Nick String
bc (forall a. [a] -> [a]
tail String
ac)
    where (String
bc, String
ac) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
==Char
':') String
str

pckStr :: Nick -> String
pckStr :: Nick -> String
pckStr Nick
nck = Nick -> String
nTag Nick
nck forall a. [a] -> [a] -> [a]
++ Char
':' forall a. a -> [a] -> [a]
: Nick -> String
nName Nick
nck

freenodeNickMapSerial :: (Show v, Read v) => Serial (M.Map Nick v)
freenodeNickMapSerial :: forall v. (Show v, Read v) => Serial (Map Nick v)
freenodeNickMapSerial = forall s.
(s -> Maybe ByteString) -> (ByteString -> Maybe s) -> Serial s
Serial
    (forall s. Serial s -> s -> Maybe ByteString
serialize forall k v.
(Ord k, Show k, Show v, Read k, Read v) =>
Serial (Map k v)
mapSerial forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeysMonotonic Nick -> FreenodeNick
FreenodeNick)
    (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall k1 k2 a. (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeysMonotonic FreenodeNick -> Nick
getFreenodeNick) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Serial s -> ByteString -> Maybe s
deserialize forall k v.
(Ord k, Show k, Show v, Read k, Read v) =>
Serial (Map k v)
mapSerial)