@@ -15,21 +15,22 @@ module JbeamEdit.Transformation.Config (
1515) where
1616
1717import Control.Monad (forM , when )
18+ import Control.Monad.Except (ExceptT (.. ), runExceptT )
19+ import Data.Bifunctor (first )
20+ import Data.ByteString.Lazy qualified as LBS
1821import Data.Functor (($>) )
19- import Data.List (isPrefixOf )
2022import Data.Scientific (Scientific )
2123import Data.Text (Text )
2224import Data.Text qualified as T
2325import Data.Yaml (
2426 Object ,
2527 ParseException (.. ),
2628 Parser ,
27- decodeFileEither ,
29+ decodeEither' ,
2830 prettyPrintParseException ,
2931 )
3032import Data.Yaml.Aeson (
3133 FromJSON (.. ),
32- YamlException (.. ),
3334 withArray ,
3435 withObject ,
3536 withText ,
@@ -38,9 +39,11 @@ import Data.Yaml.Aeson (
3839 (.:?) ,
3940 )
4041import GHC.Generics
42+ import GHC.IO.Exception (IOErrorType (NoSuchThing ))
4143import GHC.IsList
4244import JbeamEdit.IOUtils
4345import JbeamEdit.Transformation.Types (VertexTreeType (.. ))
46+ import System.OsPath
4447import Text.Read
4548
4649defaultSortingThreshold :: Scientific
@@ -139,17 +142,25 @@ instance FromJSON TransformationConfig where
139142 <*> parseSupportThreshold o
140143 <*> o .:? " max-support-coordinates" .!= defaultMaxSupportCoordinates
141144
142- formatParseError :: ParseException -> IO ()
143- formatParseError (AesonException err) = putErrorStringLn err
144- formatParseError excp = case excp of
145- (InvalidYaml (Just (YamlException errMsg)))
146- | " Yaml file not found:" `isPrefixOf` errMsg -> pure ()
147- _ -> putErrorStringLn (prettyPrintParseException excp)
148-
149- transformationConfigFile :: FilePath
150- transformationConfigFile = " .jbeam-edit.yaml"
151-
152- loadTransformationConfig :: FilePath -> IO TransformationConfig
153- loadTransformationConfig filename =
154- decodeFileEither filename
155- >>= either ((newTransformationConfig <$ ) . formatParseError) pure
145+ formatParseError :: ParseException -> String
146+ formatParseError (AesonException err) = err
147+ formatParseError excp = prettyPrintParseException excp
148+
149+ transformationConfigFile :: OsPath
150+ transformationConfigFile = unsafeEncodeUtf " .jbeam-edit.yaml"
151+
152+ decodeConfig :: LBS. ByteString -> Either Text TransformationConfig
153+ decodeConfig " " = Right newTransformationConfig
154+ decodeConfig content =
155+ first
156+ (T. pack . formatParseError)
157+ (decodeEither' $ LBS. toStrict content)
158+
159+ loadTransformationConfig :: OsPath -> IO TransformationConfig
160+ loadTransformationConfig filename = do
161+ configEither <-
162+ runExceptT
163+ (ExceptT (tryReadFile [NoSuchThing ] filename) >>= ExceptT . pure . decodeConfig)
164+ case configEither of
165+ Right config -> pure config
166+ Left err -> putErrorLine err $> newTransformationConfig
0 commit comments