scfg-1.0.0: src/Data/Scfg.hs
{- | Parser for the [scfg](https://codeberg.org/emersion/scfg)
configuration file format.
-}
module Data.Scfg (
parse,
parseFile,
format,
formatFile,
Directive (..),
Config,
ParseError (..),
) where
import qualified Data.Scfg.Formatter as F
import qualified Data.Scfg.Parser as P
import Data.Scfg.Types
import Data.Text (Text)
import qualified Data.Text.IO as TIO
{- | Parse scfg from 'Text'. Returns a 'Data.Scfg.Types.ParseError' if
the input is not valid scfg.
-}
parse :: Text -> Either ParseError Config
parse = P.parseConfig
{- | Parse scfg from a file. Returns a 'Data.Scfg.Types.ParseError' if
the input is not valid scfg or if the file cannot be read.
-}
parseFile :: FilePath -> IO (Either ParseError Config)
parseFile path = parse <$> TIO.readFile path
{- | Format a 'Config' as a canonical 'Text' representation. All words
are double-quoted and blocks indented with tabs.
-}
format :: Config -> Text
format = F.format
{- | Format a 'Config' and write it to a file. Returns an 'IOError' if
the file cannot be written.
-}
formatFile :: FilePath -> Config -> IO ()
formatFile path = TIO.writeFile path . F.format