module Crypto.Saltine.Class (
IsEncoding (..),
IsNonce (..)
) where
import Data.Profunctor
import Data.ByteString (ByteString)
class IsEncoding a where
encode :: a -> ByteString
decode :: ByteString -> Maybe a
encoded :: (Choice p, Applicative f)
=> p a (f a) -> p ByteString (f ByteString)
encoded = (a -> ByteString)
-> (ByteString -> Maybe a)
-> p a (f a)
-> p ByteString (f ByteString)
forall (f :: * -> *) (p :: * -> * -> *) a1 a a2.
(Applicative f, Choice p) =>
(a1 -> a) -> (a -> Maybe a2) -> p a2 (f a1) -> p a (f a)
prism' a -> ByteString
forall a. IsEncoding a => a -> ByteString
encode ByteString -> Maybe a
forall a. IsEncoding a => ByteString -> Maybe a
decode
{-# INLINE encoded #-}
class IsNonce n where
zero :: n
nudge :: n -> n
prism' :: (Applicative f, Choice p) =>
(a1 -> a) -> (a -> Maybe a2) -> p a2 (f a1) -> p a (f a)
prism' :: forall (f :: * -> *) (p :: * -> * -> *) a1 a a2.
(Applicative f, Choice p) =>
(a1 -> a) -> (a -> Maybe a2) -> p a2 (f a1) -> p a (f a)
prism' a1 -> a
bs a -> Maybe a2
sma = (a1 -> a) -> (a -> Either a a2) -> p a2 (f a1) -> p a (f a)
forall (f :: * -> *) (p :: * -> * -> *) a2 a1 a a3.
(Applicative f, Choice p) =>
(a2 -> a1) -> (a -> Either a1 a3) -> p a3 (f a2) -> p a (f a1)
prism a1 -> a
bs (\a
s -> Either a a2 -> (a2 -> Either a a2) -> Maybe a2 -> Either a a2
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a -> Either a a2
forall a b. a -> Either a b
Left a
s) a2 -> Either a a2
forall a b. b -> Either a b
Right (a -> Maybe a2
sma a
s))
{-# INLINE prism' #-}
prism :: (Applicative f, Choice p) =>
(a2 -> a1) -> (a -> Either a1 a3) -> p a3 (f a2) -> p a (f a1)
prism :: forall (f :: * -> *) (p :: * -> * -> *) a2 a1 a a3.
(Applicative f, Choice p) =>
(a2 -> a1) -> (a -> Either a1 a3) -> p a3 (f a2) -> p a (f a1)
prism a2 -> a1
bt a -> Either a1 a3
seta = (a -> Either a1 a3)
-> (Either a1 (f a2) -> f a1)
-> p (Either a1 a3) (Either a1 (f a2))
-> p a (f a1)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> Either a1 a3
seta ((a1 -> f a1) -> (f a2 -> f a1) -> Either a1 (f a2) -> f a1
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a1 -> f a1
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a2 -> a1) -> f a2 -> f a1
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a2 -> a1
bt)) (p (Either a1 a3) (Either a1 (f a2)) -> p a (f a1))
-> (p a3 (f a2) -> p (Either a1 a3) (Either a1 (f a2)))
-> p a3 (f a2)
-> p a (f a1)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a3 (f a2) -> p (Either a1 a3) (Either a1 (f a2))
forall a b c. p a b -> p (Either c a) (Either c b)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right'
{-# INLINE prism #-}