packages feed

phino-0.0.99 / src / CLI / Validators.hs

-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule CLI.Validators whereimport ASTimport CLI.Typesimport Control.Exceptionimport Control.Monad (forM_, when, (>=>))import Data.Foldable (for_)import Data.Maybe (isJust)import Mustimport Parser (parseExpressionThrows)import Printerimport Text.Printf (printf)invalidCLIArguments :: String -> IO ainvalidCLIArguments msg = throwIO (InvalidCLIArguments msg)-- Validate given expressions as valid dispatchesvalidatedDispatches :: String -> [String] -> IO [Expression]validatedDispatches opt = traverse (parseExpressionThrows >=> asDispatch)  where    asDispatch :: Expression -> IO Expression    asDispatch expr = go expr      where        go :: Expression -> IO Expression        go ex@ExRoot = pure ex        go disp@(ExDispatch ex _) = go ex >> pure disp        go _ =          invalidCLIArguments            ( printf                "Only dispatch expression started with Φ (or Q) can be used in --%s, but given: %s"                opt                (printExpression' expr logPrintConfig)            )-- Validate LaTeX optionsvalidateLatexOptions :: IOFormat -> [(Bool, String)] -> [(Maybe String, String)] -> [(Maybe Int, String)] -> IO ()validateLatexOptions LATEX _ _ _ = pure ()validateLatexOptions _ bools strings ints = do  let (bools', opts) = unzip bools      msg = "The --%s option can stay together with --output=latex only"      callback :: (Maybe a, String) -> IO ()      callback (maybe', opt) = when (isJust maybe') (invalidCLIArguments (printf msg opt))  validateBoolOpts (zip bools' (map (printf msg) opts))  forM_ strings callback  forM_ ints callback-- Validate 'must' optionvalidateMust' :: Must -> IO ()validateMust' must = for_ (validateMust must) invalidCLIArguments-- Validate options for output to XMIRvalidateXmirOptions :: IOFormat -> [(Bool, String)] -> String -> IO ()validateXmirOptions XMIR _ focus = when (focus /= "Q") (invalidCLIArguments "Only --focus=Q is allowed to be used with --output=xmir")validateXmirOptions _ bools _ =  let (bools', opts) = unzip bools   in validateBoolOpts (zip bools' (map (printf "The --%s can be used only with --output=xmir") opts))validateBoolOpts :: [(Bool, String)] -> IO ()validateBoolOpts bools = forM_ bools (\(bool, msg) -> when bool (invalidCLIArguments msg))