module Buffet.Parse.ParseMenu
  ( get
  ) where

import qualified Buffet.Parse.Menu as Menu
import qualified Buffet.Parse.ParseMenuFromFile as ParseMenuFromFile
import qualified Buffet.Parse.ParseMenuFromFolder as ParseMenuFromFolder
import qualified Control.Exception as Exception
import Prelude (FilePath, IO, Show, ($), (<>), show)
import qualified System.Directory as Directory

newtype Exception =
  Exception FilePath

instance Show Exception where
  show :: Exception -> String
show (Exception String
path) = String
"No such menu file or folder: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
path

instance Exception.Exception Exception

get :: FilePath -> IO Menu.Menu
get :: String -> IO Menu
get String
menu = do
  Bool
isAvailable <- String -> IO Bool
Directory.doesPathExist String
menu
  if Bool
isAvailable
    then do
      Bool
isFolder <- String -> IO Bool
Directory.doesDirectoryExist String
menu
      if Bool
isFolder
        then String -> IO Menu
ParseMenuFromFolder.get String
menu
        else String -> IO Menu
ParseMenuFromFile.get String
menu
    else Exception -> IO Menu
forall e a. Exception e => e -> IO a
Exception.throwIO (Exception -> IO Menu) -> Exception -> IO Menu
forall a b. (a -> b) -> a -> b
$ String -> Exception
Exception String
menu