summaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Message.hs
blob: 069ab991d226d41ee66629d128d7917dd9e4cef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}

-- Module     : Hsm.Core.Message
-- Maintainer : contact@pauloliver.dev
module Hsm.Core.Message
  ( message
  , topic
  , body
  )
where

import Data.Binary (Binary, decode, encode)
import Data.ByteString (ByteString, fromStrict, toStrict)
import Data.ByteString.Char8 qualified as B (breakSubstring, drop, length)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)

sep :: ByteString
sep = "//"

message :: Binary a => Text -> a -> ByteString
message t b = encodeUtf8 t <> sep <> toStrict (encode b)

topic :: ByteString -> ByteString
topic = fst . B.breakSubstring sep

body :: Binary a => ByteString -> a
body = decode . fromStrict . B.drop (B.length sep) . snd . B.breakSubstring sep