module Vaultaire.Types.Common
(
Origin(..),
makeOrigin,
Epoch,
NumBuckets
) where
import Control.Applicative
import Control.Exception (Exception, SomeException (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S
import Data.Char
import Data.Either
import Data.Hashable (Hashable)
import Data.Locator
import Data.String (IsString)
import Data.Typeable (Typeable)
import Data.Word (Word64)
import Test.QuickCheck
newtype Origin = Origin { unOrigin :: ByteString }
deriving (Eq, Ord, IsString, Hashable)
instance Arbitrary Origin where
arbitrary = Origin . S.pack . toLocator16a 6 <$> arbitrary `suchThat` (>0)
instance Read Origin where
readsPrec _ = fmap (,"") . rights . (:[]) . makeOrigin . S.pack
instance Show Origin where
show = S.unpack . unOrigin
data BadOrigin = NullOrigin | NonAlphaNumOrigin | OriginTooLong
deriving (Show, Typeable)
instance Exception BadOrigin
makeOrigin :: ByteString -> Either SomeException Origin
makeOrigin bs
| S.null bs = Left (SomeException NullOrigin)
| S.any (not . isAlphaNum) bs = Left (SomeException NonAlphaNumOrigin)
| S.length bs > 8 = Left (SomeException OriginTooLong)
| otherwise = Right (Origin bs)
type Epoch = Word64
type NumBuckets = Word64