never executed always true always false
    1 module ElmFormat.Messages (PromptMessage(..), InfoMessage(..), ErrorMessage(..)) where
    2 
    3 -- inspired by:
    4 -- https://wiki.haskell.org/Internationalization_of_Haskell_programs_using_Haskell_data_types
    5 
    6 import Prelude ()
    7 import Relude
    8 
    9 import CommandLine.InfoFormatter (ToConsole(..), Loggable(..))
   10 import CommandLine.ResolveFiles as ResolveFiles
   11 import qualified Data.Text as Text
   12 import qualified ElmFormat.Version
   13 import ElmVersion
   14 import qualified Reporting.Annotation as A
   15 import qualified Reporting.Error.Syntax as Syntax
   16 import Reporting.Region (Region(..), Position(..))
   17 import qualified Data.Aeson as Aeson
   18 import Data.Aeson ((.=))
   19 
   20 
   21 data InfoMessage
   22   = ProcessingFile FilePath
   23   | FileWouldChange ElmVersion FilePath
   24   | ParseError FilePath [A.Located Syntax.Error]
   25   | JsonParseError FilePath Text
   26 
   27 
   28 data PromptMessage
   29     = FilesWillBeOverwritten [FilePath]
   30 
   31 
   32 data ErrorMessage
   33   = BadInputFiles [ResolveFiles.Error]
   34   | NoInputs
   35   | SingleOutputWithMultipleInputs
   36   | TooManyInputs
   37   | OutputAndValidate
   38   | MustSpecifyVersionWithUpgrade ElmVersion
   39 
   40 
   41 showFiles :: [FilePath] -> Text
   42 showFiles = unlines . fmap (\filename -> "    " <> Text.pack filename)
   43 
   44 
   45 instance ToConsole PromptMessage where
   46     toConsole = \case
   47         FilesWillBeOverwritten filePaths ->
   48             unlines
   49                 [ "This will overwrite the following files to use Elm's preferred style:"
   50                 , ""
   51                 , showFiles filePaths
   52                 , "This cannot be undone! Make sure to back up these files before proceeding."
   53                 , ""
   54                 , "Are you sure you want to overwrite these files with formatted versions? (y/n)"
   55                 ]
   56 
   57 
   58 instance ToConsole InfoMessage where
   59     toConsole = \case
   60         ProcessingFile file ->
   61             "Processing file " <> Text.pack file
   62 
   63         FileWouldChange _ file ->
   64             "File would be changed " <> Text.pack file
   65 
   66         ParseError inputFile errs ->
   67             let
   68                 location =
   69                     Text.pack $
   70                     case errs of
   71                         [] -> inputFile
   72                         (A.A (Region (Position line col) _) _) : _ -> inputFile ++ ":" ++ show line ++ ":" ++ show col
   73             in
   74             "Unable to parse file " <> location <> " To see a detailed explanation, run elm make on the file."
   75 
   76         JsonParseError inputFile err ->
   77             "Unable to parse JSON file " <> Text.pack inputFile <> "\n\n" <> err
   78 
   79 
   80 instance Loggable InfoMessage where
   81     jsonInfoMessage =
   82         let
   83             fileMessage filename message =
   84                 Aeson.pairs $ mconcat
   85                     [ "path" .= (filename :: FilePath)
   86                     , "message" .= (message :: String)
   87                     ]
   88         in
   89         \case
   90         ProcessingFile _ -> Nothing
   91         FileWouldChange elmVersion file ->
   92             Just $ fileMessage file $
   93                 "File is not formatted with elm-format-" <> ElmFormat.Version.asString
   94                 <> " --elm-version=" <> show elmVersion
   95         ParseError inputFile _ ->
   96             Just $ fileMessage inputFile "Error parsing the file"
   97         JsonParseError inputFile _ ->
   98             Just $ fileMessage inputFile "Error parsing the JSON file"
   99 
  100 
  101 instance ToConsole ErrorMessage where
  102     toConsole = \case
  103         BadInputFiles filePaths ->
  104             unlines
  105                 [ "There was a problem reading one or more of the specified INPUT paths:"
  106                 , ""
  107                 , unlines $ map ((<>) "    " . toConsole) filePaths
  108                 , "Please check the given paths."
  109                 ]
  110 
  111         SingleOutputWithMultipleInputs ->
  112             unlines
  113                 [ "Can't write to the OUTPUT path, because multiple .elm files have been specified."
  114                 , ""
  115                 , "Please remove the --output argument. The .elm files in INPUT will be formatted in place."
  116                 ]
  117 
  118         TooManyInputs ->
  119             "Too many input sources! Please only provide one of either INPUT or --stdin"
  120 
  121         OutputAndValidate ->
  122             "Cannot use --output and --validate together"
  123 
  124         MustSpecifyVersionWithUpgrade elmVersion ->
  125             "I can only upgrade code to specific Elm versions.  To make sure I'm doing what you expect, you must also specify --elm-version=" <> Text.pack (show elmVersion) <> " when you use --upgrade."
  126 
  127         NoInputs ->
  128             error "Error case NoInputs should be handled elsewhere.  Please report this issue at https://github.com/avh4/elm-format/issues"