Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src-extra/transformation/JbeamEdit/Transformation/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ module JbeamEdit.Transformation.Config (

import Data.Scientific (Scientific)
import Data.Text qualified as T
import Data.Yaml (decodeFileEither)
import Data.Yaml (
Object,
ParseException (..),
Parser,
decodeFileEither,
prettyPrintParseException,
)
import Data.Yaml.Aeson (
FromJSON (..),
withArray,
Expand Down Expand Up @@ -107,25 +113,28 @@ instance FromJSON XGroupBreakpoints where
obj
pure $ XGroupBreakpoints lst

parseSupportThreshold :: Object -> Parser Double
parseSupportThreshold o = do
thr <- o .: "support-threshold"
when (thr < 1) failWithMessage $> thr
where
failWithMessage =
fail
"'support-threshold' must be a percentage value of 1 or higher (e.g., 80 or 80.8). Values below 1 (e.g., 0.80) are not allowed."

instance FromJSON TransformationConfig where
parseJSON = withObject "TransformationConfig" $ \o ->
TransformationConfig
<$> o .:? "z-sorting-threshold" .!= defaultSortingThreshold
<*> o .:? "x-group-breakpoints" .!= defaultBreakpoints
<*> o .:? "support-threshold" .!= defaultSupportThreshold
<*> parseSupportThreshold o
<*> o .:? "max-support-coordinates" .!= defaultMaxSupportCoordinates

formatParseError :: ParseException -> IO ()
formatParseError (AesonException err) = putErrorStringLn err
formatParseError excp = putErrorStringLn (prettyPrintParseException excp)

loadTransformationConfig :: FilePath -> IO TransformationConfig
loadTransformationConfig filename = do
res <- decodeFileEither filename
case res of
Right tc -> pure tc
Left err -> do
putErrorLine $ show err
pure
( TransformationConfig
defaultSortingThreshold
defaultBreakpoints
defaultSupportThreshold
defaultMaxSupportCoordinates
)
either ((newTransformationConfig <$) . formatParseError) pure res
3 changes: 1 addition & 2 deletions src/JbeamEdit/Formatting/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module JbeamEdit.Formatting.Config (readFormattingConfig, copyToConfigDir, ConfigType (..)) where

import Data.Text (pack)
import GHC.IO.Exception (IOErrorType (NoSuchThing))
import JbeamEdit.Formatting.Rules
import JbeamEdit.IOUtils
Expand Down Expand Up @@ -71,7 +70,7 @@ readFormattingConfig maybeJbflPath = do
configDir <- getConfigDir
case maybeJbflPath of
Just jbfl ->
putErrorLine $ "Loading jbfl: " <> pack jbfl
putErrorStringLn $ "Loading jbfl: " ++ jbfl
Nothing ->
createRuleFileIfDoesNotExist (configDir </> "rules.jbfl")
configPath <- getConfigPath maybeJbflPath configDir
Expand Down
7 changes: 5 additions & 2 deletions src/JbeamEdit/IOUtils.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module JbeamEdit.IOUtils (tryReadFile, putErrorLine, reportInvalidNodes) where
module JbeamEdit.IOUtils (tryReadFile, putErrorLine, putErrorStringLn, reportInvalidNodes) where

import Control.Exception (IOException, try)
import Control.Monad (unless)
Expand All @@ -13,8 +13,11 @@ import JbeamEdit.Core.Node (Node)
import JbeamEdit.Formatting (formatNode, newRuleSet)
import System.IO (hPutStrLn, stderr)

putErrorStringLn :: String -> IO ()
putErrorStringLn = hPutStrLn stderr

putErrorLine :: Text -> IO ()
putErrorLine = hPutStrLn stderr . T.unpack
putErrorLine = putErrorStringLn . T.unpack

reportInvalidNodes :: Text -> [Node] -> IO ()
reportInvalidNodes msg nodes =
Expand Down