module System.Nix.Base
( BaseEncoding(Base16,NixBase32,Base64)
, encodeWith
, decodeWith
) where
import Data.ByteString (ByteString)
import Data.Text (Text)
import GHC.Generics (Generic)
import qualified Data.Text.Encoding
import qualified Data.ByteString.Base16
import qualified Data.ByteString.Base64
import qualified System.Nix.Base32
data BaseEncoding
= NixBase32
| Base16
| Base64
deriving (BaseEncoding
BaseEncoding -> BaseEncoding -> Bounded BaseEncoding
forall a. a -> a -> Bounded a
$cminBound :: BaseEncoding
minBound :: BaseEncoding
$cmaxBound :: BaseEncoding
maxBound :: BaseEncoding
Bounded, BaseEncoding -> BaseEncoding -> Bool
(BaseEncoding -> BaseEncoding -> Bool)
-> (BaseEncoding -> BaseEncoding -> Bool) -> Eq BaseEncoding
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BaseEncoding -> BaseEncoding -> Bool
== :: BaseEncoding -> BaseEncoding -> Bool
$c/= :: BaseEncoding -> BaseEncoding -> Bool
/= :: BaseEncoding -> BaseEncoding -> Bool
Eq, Int -> BaseEncoding
BaseEncoding -> Int
BaseEncoding -> [BaseEncoding]
BaseEncoding -> BaseEncoding
BaseEncoding -> BaseEncoding -> [BaseEncoding]
BaseEncoding -> BaseEncoding -> BaseEncoding -> [BaseEncoding]
(BaseEncoding -> BaseEncoding)
-> (BaseEncoding -> BaseEncoding)
-> (Int -> BaseEncoding)
-> (BaseEncoding -> Int)
-> (BaseEncoding -> [BaseEncoding])
-> (BaseEncoding -> BaseEncoding -> [BaseEncoding])
-> (BaseEncoding -> BaseEncoding -> [BaseEncoding])
-> (BaseEncoding -> BaseEncoding -> BaseEncoding -> [BaseEncoding])
-> Enum BaseEncoding
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: BaseEncoding -> BaseEncoding
succ :: BaseEncoding -> BaseEncoding
$cpred :: BaseEncoding -> BaseEncoding
pred :: BaseEncoding -> BaseEncoding
$ctoEnum :: Int -> BaseEncoding
toEnum :: Int -> BaseEncoding
$cfromEnum :: BaseEncoding -> Int
fromEnum :: BaseEncoding -> Int
$cenumFrom :: BaseEncoding -> [BaseEncoding]
enumFrom :: BaseEncoding -> [BaseEncoding]
$cenumFromThen :: BaseEncoding -> BaseEncoding -> [BaseEncoding]
enumFromThen :: BaseEncoding -> BaseEncoding -> [BaseEncoding]
$cenumFromTo :: BaseEncoding -> BaseEncoding -> [BaseEncoding]
enumFromTo :: BaseEncoding -> BaseEncoding -> [BaseEncoding]
$cenumFromThenTo :: BaseEncoding -> BaseEncoding -> BaseEncoding -> [BaseEncoding]
enumFromThenTo :: BaseEncoding -> BaseEncoding -> BaseEncoding -> [BaseEncoding]
Enum, (forall x. BaseEncoding -> Rep BaseEncoding x)
-> (forall x. Rep BaseEncoding x -> BaseEncoding)
-> Generic BaseEncoding
forall x. Rep BaseEncoding x -> BaseEncoding
forall x. BaseEncoding -> Rep BaseEncoding x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BaseEncoding -> Rep BaseEncoding x
from :: forall x. BaseEncoding -> Rep BaseEncoding x
$cto :: forall x. Rep BaseEncoding x -> BaseEncoding
to :: forall x. Rep BaseEncoding x -> BaseEncoding
Generic, Eq BaseEncoding
Eq BaseEncoding =>
(BaseEncoding -> BaseEncoding -> Ordering)
-> (BaseEncoding -> BaseEncoding -> Bool)
-> (BaseEncoding -> BaseEncoding -> Bool)
-> (BaseEncoding -> BaseEncoding -> Bool)
-> (BaseEncoding -> BaseEncoding -> Bool)
-> (BaseEncoding -> BaseEncoding -> BaseEncoding)
-> (BaseEncoding -> BaseEncoding -> BaseEncoding)
-> Ord BaseEncoding
BaseEncoding -> BaseEncoding -> Bool
BaseEncoding -> BaseEncoding -> Ordering
BaseEncoding -> BaseEncoding -> BaseEncoding
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
$ccompare :: BaseEncoding -> BaseEncoding -> Ordering
compare :: BaseEncoding -> BaseEncoding -> Ordering
$c< :: BaseEncoding -> BaseEncoding -> Bool
< :: BaseEncoding -> BaseEncoding -> Bool
$c<= :: BaseEncoding -> BaseEncoding -> Bool
<= :: BaseEncoding -> BaseEncoding -> Bool
$c> :: BaseEncoding -> BaseEncoding -> Bool
> :: BaseEncoding -> BaseEncoding -> Bool
$c>= :: BaseEncoding -> BaseEncoding -> Bool
>= :: BaseEncoding -> BaseEncoding -> Bool
$cmax :: BaseEncoding -> BaseEncoding -> BaseEncoding
max :: BaseEncoding -> BaseEncoding -> BaseEncoding
$cmin :: BaseEncoding -> BaseEncoding -> BaseEncoding
min :: BaseEncoding -> BaseEncoding -> BaseEncoding
Ord, Int -> BaseEncoding -> ShowS
[BaseEncoding] -> ShowS
BaseEncoding -> String
(Int -> BaseEncoding -> ShowS)
-> (BaseEncoding -> String)
-> ([BaseEncoding] -> ShowS)
-> Show BaseEncoding
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BaseEncoding -> ShowS
showsPrec :: Int -> BaseEncoding -> ShowS
$cshow :: BaseEncoding -> String
show :: BaseEncoding -> String
$cshowList :: [BaseEncoding] -> ShowS
showList :: [BaseEncoding] -> ShowS
Show)
encodeWith :: BaseEncoding -> ByteString -> Text
encodeWith :: BaseEncoding -> ByteString -> Text
encodeWith BaseEncoding
Base16 =
ByteString -> Text
Data.Text.Encoding.decodeUtf8
(ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
Data.ByteString.Base16.encode
encodeWith BaseEncoding
NixBase32 = ByteString -> Text
System.Nix.Base32.encode
encodeWith BaseEncoding
Base64 =
ByteString -> Text
Data.Text.Encoding.decodeUtf8
(ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
Data.ByteString.Base64.encode
decodeWith :: BaseEncoding -> Text -> Either String ByteString
decodeWith :: BaseEncoding -> Text -> Either String ByteString
decodeWith BaseEncoding
Base16 =
ByteString -> Either String ByteString
Data.ByteString.Base16.decode
(ByteString -> Either String ByteString)
-> (Text -> ByteString) -> Text -> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Data.Text.Encoding.encodeUtf8
decodeWith BaseEncoding
NixBase32 = Text -> Either String ByteString
System.Nix.Base32.decode
decodeWith BaseEncoding
Base64 =
ByteString -> Either String ByteString
Data.ByteString.Base64.decode
(ByteString -> Either String ByteString)
-> (Text -> ByteString) -> Text -> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Data.Text.Encoding.encodeUtf8