<commit_before>{-# LANGUAGE TemplateHaskell #-}

module Pos.Types.Version
       (
         ProtocolVersion (..)
       , SoftwareVersion (..)
       , ApplicationName (..)
       , mkApplicationName
       , applicationNameMaxLength
       ) where

import           Data.Char           (isAscii)
import           Data.SafeCopy       (base, deriveSafeCopySimple)
import qualified Data.Text           as T
import qualified Data.Text.Buildable as Buildable
import           Formatting          (bprint, build, shown, stext, (%))
import           Prelude             (show)
import           Universum           hiding (show)

-- | Communication protocol version.
data ProtocolVersion = ProtocolVersion
    { pvMajor :: Word16
    , pvMinor :: Word16
    , pvAlt   :: Word8
    } deriving (Eq, Generic, Ord, Typeable)

instance Show ProtocolVersion where
    show ProtocolVersion {..} =
        intercalate "." [show pvMajor, show pvMinor, show pvAlt]

instance Buildable ProtocolVersion where
    build = bprint shown

newtype ApplicationName = ApplicationName
    { getApplicationName :: Text
    } deriving (Eq, Ord, Show, Generic, Typeable, ToString)

applicationNameMaxLength :: Integral i => i
applicationNameMaxLength = 10

mkApplicationName :: MonadFail m => Text -> m ApplicationName
mkApplicationName appName
    | T.length appName > applicationNameMaxLength =
        fail "ApplicationName: too long string passed"
    | T.any (not . isAscii) appName =
        fail "ApplicationName: not ascii string passed"
    | otherwise = pure $ ApplicationName appName

-- | Software version.
data SoftwareVersion = SoftwareVersion
    { svAppName :: ApplicationName
    , svMajor   :: Word8
    , svMinor   :: Word16
    }
  deriving (Eq, Generic, Ord, Typeable)

instance Buildable SoftwareVersion where
    build SoftwareVersion {..} =
      bprint (stext % ":" % build % "." % build)
         (getApplicationName svAppName) svMajor svMinor

instance Show SoftwareVersion where
    show SoftwareVersion {..} = mconcat
        [ toString svAppName
        , ":"
        , show svMajor
        , "."
        , show svMinor
        ]

deriveSafeCopySimple 0 'base ''ApplicationName
deriveSafeCopySimple 0 'base ''ProtocolVersion
deriveSafeCopySimple 0 'base ''SoftwareVersion<commit_msg>[CSL-349] Derive Hashable for some types<commit_after>{-# LANGUAGE TemplateHaskell #-}

module Pos.Types.Version
       (
         ProtocolVersion (..)
       , SoftwareVersion (..)
       , ApplicationName (..)
       , mkApplicationName
       , applicationNameMaxLength
       ) where

import           Data.Char           (isAscii)
import           Data.Hashable       (Hashable)
import           Data.SafeCopy       (base, deriveSafeCopySimple)
import qualified Data.Text           as T
import qualified Data.Text.Buildable as Buildable
import           Formatting          (bprint, build, shown, stext, (%))
import           Prelude             (show)
import           Universum           hiding (show)

-- | Communication protocol version.
data ProtocolVersion = ProtocolVersion
    { pvMajor :: Word16
    , pvMinor :: Word16
    , pvAlt   :: Word8
    } deriving (Eq, Generic, Ord, Typeable)

instance Show ProtocolVersion where
    show ProtocolVersion {..} =
        intercalate "." [show pvMajor, show pvMinor, show pvAlt]

instance Buildable ProtocolVersion where
    build = bprint shown

instance Hashable ProtocolVersion

newtype ApplicationName = ApplicationName
    { getApplicationName :: Text
    } deriving (Eq, Ord, Show, Generic, Typeable, ToString, Hashable)

applicationNameMaxLength :: Integral i => i
applicationNameMaxLength = 10

mkApplicationName :: MonadFail m => Text -> m ApplicationName
mkApplicationName appName
    | T.length appName > applicationNameMaxLength =
        fail "ApplicationName: too long string passed"
    | T.any (not . isAscii) appName =
        fail "ApplicationName: not ascii string passed"
    | otherwise = pure $ ApplicationName appName

-- | Software version.
data SoftwareVersion = SoftwareVersion
    { svAppName :: ApplicationName
    , svMajor   :: Word8
    , svMinor   :: Word16
    }
  deriving (Eq, Generic, Ord, Typeable)

instance Buildable SoftwareVersion where
    build SoftwareVersion {..} =
      bprint (stext % ":" % build % "." % build)
         (getApplicationName svAppName) svMajor svMinor

instance Show SoftwareVersion where
    show SoftwareVersion {..} = mconcat
        [ toString svAppName
        , ":"
        , show svMajor
        , "."
        , show svMinor
        ]

instance Hashable SoftwareVersion

deriveSafeCopySimple 0 'base ''ApplicationName
deriveSafeCopySimple 0 'base ''ProtocolVersion
deriveSafeCopySimple 0 'base ''SoftwareVersion
{'fraction_non_alphanumeric': 0.08282143613641178, 'fraction_numerical': 0.006142766363058674, 'mean_word_length': 2.700626959247649, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3481866', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1264, 'n_words': 378}