{-# LANGUAGE FlexibleContexts #-}

module Saturn.Unstable.Type.Weekday where

import qualified Data.Coerce as Coerce
import qualified Data.Set as Set
import qualified Data.Text.Lazy.Builder as Builder
import qualified Data.Word as Word
import qualified Saturn.Unstable.Type.Field as Field
import qualified Text.Parsec as Parsec

newtype Weekday
  = Weekday Field.Field
  deriving (Weekday -> Weekday -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Weekday -> Weekday -> Bool
$c/= :: Weekday -> Weekday -> Bool
== :: Weekday -> Weekday -> Bool
$c== :: Weekday -> Weekday -> Bool
Eq, Int -> Weekday -> ShowS
[Weekday] -> ShowS
Weekday -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Weekday] -> ShowS
$cshowList :: [Weekday] -> ShowS
show :: Weekday -> String
$cshow :: Weekday -> String
showsPrec :: Int -> Weekday -> ShowS
$cshowsPrec :: Int -> Weekday -> ShowS
Show)

bounds :: (Word.Word8, Word.Word8)
bounds :: (Word8, Word8)
bounds = (Word8
0, Word8
6)

fromField :: Field.Field -> Maybe Weekday
fromField :: Field -> Maybe Weekday
fromField Field
field =
  if (Word8, Word8) -> Field -> Bool
Field.isValid (Word8, Word8)
bounds Field
field then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Field -> Weekday
Weekday Field
field else forall a. Maybe a
Nothing

toField :: Weekday -> Field.Field
toField :: Weekday -> Field
toField = coerce :: forall a b. Coercible a b => a -> b
Coerce.coerce

parsec :: (Parsec.Stream s m Char) => Parsec.ParsecT s u m Weekday
parsec :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Weekday
parsec = do
  Field
field <- forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Field
Field.parsec
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid Weekday") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Field -> Maybe Weekday
fromField Field
field

toBuilder :: Weekday -> Builder.Builder
toBuilder :: Weekday -> Builder
toBuilder = Field -> Builder
Field.toBuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. Weekday -> Field
toField

expand :: Weekday -> Set.Set Word.Word8
expand :: Weekday -> Set Word8
expand = (Word8, Word8) -> Field -> Set Word8
Field.expand (Word8, Word8)
bounds forall b c a. (b -> c) -> (a -> b) -> a -> c
. Weekday -> Field
toField

isMatch :: Word.Word8 -> Weekday -> Bool
isMatch :: Word8 -> Weekday -> Bool
isMatch Word8
word8 = forall a. Ord a => a -> Set a -> Bool
Set.member Word8
word8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Weekday -> Set Word8
expand