1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00
semantic/src/Renderer.hs

38 lines
1.3 KiB
Haskell
Raw Normal View History

2016-08-10 19:17:23 +03:00
module Renderer (Renderer, DiffArguments(..), Output(..), concatOutputs, Format(..)) where
import Prologue
import Data.Functor.Both
import Diff
2016-08-10 19:17:23 +03:00
import Source (SourceBlob)
import Data.Aeson.Types (Series, pairs)
import Data.Text as T (intercalate)
import Data.Aeson.Encoding (encodingToLazyByteString)
2016-07-29 19:24:12 +03:00
-- | A function that will render a diff, given the two source blobs.
type Renderer annotation = Both SourceBlob -> Diff Text annotation -> Output
2016-04-01 22:34:52 +03:00
data DiffArguments = DiffArguments { format :: Format, output :: Maybe FilePath, outputPath :: FilePath }
2016-04-12 20:10:24 +03:00
deriving (Show)
2016-04-01 22:34:52 +03:00
data Output = SplitOutput Text | PatchOutput Text | JSONOutput Series | SummaryOutput Series
2016-08-10 19:17:23 +03:00
concatOutputs :: [Output] -> Text
concatOutputs l@(JSONOutput _ : _) = toS . encodingToLazyByteString . pairs . mconcat $ toSeries <$> l
concatOutputs l@(SummaryOutput _ : _) = toS . encodingToLazyByteString . pairs . mconcat $ toSeries <$> l
concatOutputs l = T.intercalate "\n" (toText <$> l)
toSeries :: Output -> Series
toSeries (JSONOutput series) = series
toSeries (SummaryOutput series) = series
toSeries _ = mempty
2016-08-10 19:17:23 +03:00
toText :: Output -> Text
toText (SplitOutput text) = text
toText (PatchOutput text) = text
toText _ = mempty
2016-08-10 19:17:23 +03:00
2016-04-01 22:34:52 +03:00
-- | The available types of diff rendering.
2016-05-18 19:01:16 +03:00
data Format = Split | Patch | JSON | Summary
2016-04-12 20:10:24 +03:00
deriving (Show)