{-# LANGUAGE OverloadedStrings #-}
module B9.Artifact.Content.CloudConfigYaml
( CloudConfigYaml (..),
cloudConfigFileHeader,
)
where
import B9.Artifact.Content.AST
import B9.Artifact.Content.YamlObject
import B9.Text
import Control.Parallel.Strategies (NFData)
import Data.Data
( Data,
Typeable,
)
import Data.Hashable (Hashable)
import Data.Text as Text
import GHC.Generics (Generic)
import Test.QuickCheck (Arbitrary)
newtype CloudConfigYaml
= MkCloudConfigYaml
{ fromCloudConfigYaml :: YamlObject
}
deriving (Hashable, NFData, Eq, Data, Typeable, Generic, Arbitrary, Read, Show, Semigroup)
cloudConfigFileHeader :: Text
cloudConfigFileHeader = "#cloud-config\n"
instance FromAST CloudConfigYaml where
fromAST ast = MkCloudConfigYaml <$> fromAST (fromCloudConfigYaml <$> ast)
instance Textual CloudConfigYaml where
parseFromText txt = do
let header = Text.take (Text.length cloudConfigFileHeader) txt
txt' =
if header == cloudConfigFileHeader
then Text.drop (Text.length cloudConfigFileHeader) txt
else txt
y <- parseFromText txt'
return (MkCloudConfigYaml y)
renderToText (MkCloudConfigYaml y) = do
txt <- renderToText y
return (Text.unlines [cloudConfigFileHeader, txt])