module Data.Foscam.File.DeviceIdCharacter(
DeviceIdCharacter
, AsDeviceIdCharacter(..)
, deviceIdCharacter
, getDeviceIdCharacter
) where
import Control.Applicative(Applicative(pure))
import Control.Category(id, (.))
import Control.Lens(Optic', Choice, Getter, prism', re)
import Control.Monad(Monad(fail))
import Data.Char(Char)
import Data.Eq(Eq)
import Data.Functor(fmap)
import Data.Foscam.File.Internal(boolj, charP)
import Data.List(elem, (++))
import Data.Ord(Ord)
import Data.String(String)
import Data.Traversable(traverse)
import Text.Parser.Char(CharParsing)
import Text.Parser.Combinators((<?>))
import Prelude(Show)
newtype DeviceIdCharacter =
DeviceIdCharacter Char
deriving (Eq, Ord, Show)
class AsDeviceIdCharacter p f s where
_DeviceIdCharacter ::
Optic' p f s DeviceIdCharacter
instance AsDeviceIdCharacter p f DeviceIdCharacter where
_DeviceIdCharacter =
id
instance (Choice p, Applicative f) => AsDeviceIdCharacter p f Char where
_DeviceIdCharacter =
prism'
(\(DeviceIdCharacter c) -> c)
(fmap DeviceIdCharacter . boolj (`elem` (['A'..'F'] ++ ['0'..'9'])))
instance (p ~ (->), Applicative f) => AsDeviceIdCharacter p f String where
_DeviceIdCharacter =
traverse . _DeviceIdCharacter
getDeviceIdCharacter ::
Getter DeviceIdCharacter Char
getDeviceIdCharacter =
re _DeviceIdCharacter
deviceIdCharacter ::
(Monad f, CharParsing f) =>
f DeviceIdCharacter
deviceIdCharacter =
charP (fail . ("not a device ID character: " ++) . pure) _DeviceIdCharacter <?> "device ID character"