module Functions.Date ( extractDate ) where import Types (Token, Date(..)) import 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"