{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-|
Module:      TextShow.Data.Version
Copyright:   (C) 2014-2017 Ryan Scott
License:     BSD-style (see the file LICENSE)
Maintainer:  Ryan Scott
Stability:   Provisional
Portability: GHC

Provides a 'TextShow' instance for 'Version' and the 'showbVersion' function.

/Since: 2/
-}
module TextShow.Data.Version (showbVersion) where

import Data.List (intersperse)
import Data.Text.Lazy.Builder (Builder, fromString, singleton)
import Data.Version (Version(..))

import Prelude ()
import Prelude.Compat

import TextShow.Classes (TextShow(..))
import TextShow.Data.Char ()
import TextShow.Data.Integral ()
import TextShow.Data.List ()
import TextShow.TH.Internal (deriveTextShow)

-- | Provides one possible concrete representation for 'Version'.  For
-- a version with 'versionBranch' @= [1,2,3]@ and 'versionTags'
-- @= [\"tag1\",\"tag2\"]@, the output will be @1.2.3-tag1-tag2@.
--
-- /Since: 3.6/
showbVersion :: Version -> Builder
showbVersion :: Version -> Builder
showbVersion (Version [Int]
branch [String]
tags)
    = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat (Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
intersperse (Char -> Builder
singleton Char
'.') ([Builder] -> [Builder]) -> [Builder] -> [Builder]
forall a b. (a -> b) -> a -> b
$ (Int -> Builder) -> [Int] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Builder
forall a. TextShow a => a -> Builder
showb [Int]
branch) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
        [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((String -> Builder) -> [String] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Builder
singleton Char
'-' Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>) (Builder -> Builder) -> (String -> Builder) -> String -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
fromString) [String]
tags)
{-# INLINE showbVersion #-}

-- | /Since: 2/
$(deriveTextShow ''Version)