module Byg.Functions.Date
  ( extractDate
  ) where

import Byg.Types (Token, Date(..))
import Byg.DependencyGenerator (DepGenM, TokenableTo(..), onToken)

import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE

split :: Eq a => a -> [a] -> NonEmpty [a]
split sep = \case
  [] ->
    NE.singleton []
  (c : cs) ->
    (if sep == c
     then NE.cons []
     else \(h :| t) -> (c : h) :| t)
    $ split sep cs

extractDate :: TokenableTo String a => a -> DepGenM (Token Date)
extractDate = onToken $ \dirName -> case split '-' dirName of
  year :| (month : day : _) ->
    Date (read year) (read month) (read day)
  _ ->
    error "unexpected"
