module Control.Router
( Router (..)
, ErrorHandler
, route
) where
import Control.Applicative
import Control.Command
import Control.Monad
class (Command a) => Router a where
routes :: a -> String -> Bool
type ErrorHandler = String -> IO ()
route :: (Command a, Router a) => ErrorHandler -> [a] -> [String] -> IO ()
route eh cmds [] = eh "Empty argument list. Cannot route command."
route eh cmds (cmd:args) =
case findCommand cmd cmds of
Just a -> execute a args
Nothing -> eh ("Command " ++ cmd ++ " is unknown.")
where
findCommand _ [] =
empty
findCommand cmd (x:xs) =
if routes x cmd
then return x
else findCommand cmd xs