{-# LANGUAGE FlexibleContexts #-}

module Saturn.Unstable.Type.Day 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 Day
  = Day Field.Field
  deriving (Day -> Day -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Day -> Day -> Bool
$c/= :: Day -> Day -> Bool
== :: Day -> Day -> Bool
$c== :: Day -> Day -> Bool
Eq, Int -> Day -> ShowS
[Day] -> ShowS
Day -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Day] -> ShowS
$cshowList :: [Day] -> ShowS
show :: Day -> String
$cshow :: Day -> String
showsPrec :: Int -> Day -> ShowS
$cshowsPrec :: Int -> Day -> ShowS
Show)

bounds :: (Word.Word8, Word.Word8)
bounds :: (Word8, Word8)
bounds = (Word8
1, Word8
31)

fromField :: Field.Field -> Maybe Day
fromField :: Field -> Maybe Day
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 -> Day
Day Field
field else forall a. Maybe a
Nothing

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

parsec :: (Parsec.Stream s m Char) => Parsec.ParsecT s u m Day
parsec :: forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Day
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 Day") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Field -> Maybe Day
fromField Field
field

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

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

isMatch :: Word.Word8 -> Day -> Bool
isMatch :: Word8 -> Day -> 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
. Day -> Set Word8
expand