module Distribution.Fields.Field (
Field (..),
fieldName,
fieldAnn,
fieldUniverse,
FieldLine (..),
fieldLineAnn,
fieldLineBS,
SectionArg (..),
sectionArgAnn,
FieldName,
Name (..),
mkName,
getName,
nameAnn,
sectionArgsToString,
fieldLinesToString,
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import qualified Data.Char as Char
import Distribution.Compat.Prelude
import Distribution.Pretty (showTokenStr)
import Distribution.Simple.Utils (fromUTF8BS)
import Prelude ()
data Field ann
= Field !(Name ann) [FieldLine ann]
| Section !(Name ann) [SectionArg ann] [Field ann]
deriving (Eq, Show, Functor, Foldable, Traversable)
fieldName :: Field ann -> Name ann
fieldName (Field n _ ) = n
fieldName (Section n _ _) = n
fieldAnn :: Field ann -> ann
fieldAnn = nameAnn . fieldName
fieldUniverse :: Field ann -> [Field ann]
fieldUniverse f@(Section _ _ fs) = f : concatMap fieldUniverse fs
fieldUniverse f@(Field _ _) = [f]
data FieldLine ann = FieldLine !ann !ByteString
deriving (Eq, Show, Functor, Foldable, Traversable)
fieldLineAnn :: FieldLine ann -> ann
fieldLineAnn (FieldLine ann _) = ann
fieldLineBS :: FieldLine ann -> ByteString
fieldLineBS (FieldLine _ bs) = bs
data SectionArg ann
= SecArgName !ann !ByteString
| SecArgStr !ann !ByteString
| SecArgOther !ann !ByteString
deriving (Eq, Show, Functor, Foldable, Traversable)
sectionArgAnn :: SectionArg ann -> ann
sectionArgAnn (SecArgName ann _) = ann
sectionArgAnn (SecArgStr ann _) = ann
sectionArgAnn (SecArgOther ann _) = ann
type FieldName = ByteString
data Name ann = Name !ann !FieldName
deriving (Eq, Show, Functor, Foldable, Traversable)
mkName :: ann -> FieldName -> Name ann
mkName ann bs = Name ann (B.map Char.toLower bs)
getName :: Name ann -> FieldName
getName (Name _ bs) = bs
nameAnn :: Name ann -> ann
nameAnn (Name ann _) = ann
sectionArgsToString :: [SectionArg ann] -> String
sectionArgsToString = unwords . map toStr where
toStr :: SectionArg ann -> String
toStr (SecArgName _ bs) = showTokenStr (fromUTF8BS bs)
toStr (SecArgStr _ bs) = showTokenStr (fromUTF8BS bs)
toStr (SecArgOther _ bs) = fromUTF8BS bs
fieldLinesToString :: [FieldLine ann] -> String
fieldLinesToString =
intercalate "\n" . map toStr
where
toStr (FieldLine _ bs) = fromUTF8BS bs