{-# LANGUAGE
    DataKinds
  , FlexibleContexts
  , FlexibleInstances
  , TypeOperators
  #-}

module Test.Common where

import Data.Functor.Contravariant
import Data.List
import Data.Text.Lazy.Builder
import Data.Vinyl
import Data.Vinyl.Functor
import Data.Vinyl.Utils.Operator

type Id = Identity

buildDelimited
  :: Buildable rs 
  => Char 
  -> Rec Id rs 
  -> Builder
buildDelimited delim = 
    mconcat 
  . intersperse (singleton delim)
  . build

build 
  :: Buildable rs 
  => Rec Id rs 
  -> [Builder]
build r = 
    map getIdentity
  . recordToList 
  $ builder \$\ r

class Buildable rs where
  builder :: Rec (Op Builder) rs

instance Buildable '[] where
  builder = RNil

instance (Buildable rs, Show r) => Buildable (r ': rs) where
  builder = Op (fromString . show) :& builder

