-- SPDX-FileCopyrightText: 2022 Sarah Vaupel <sarah.vaupel@ifi.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.SemVer.Instances
  () where

import ClassyPrelude
import qualified Data.SemVer as SemVer
import qualified Data.SemVer.Constraint as SemVer (Constraint(..))
import qualified Data.SemVer.Constraint as SemVer.Constraint

import Web.HttpApiData


instance ToHttpApiData SemVer.Version where
  toUrlPiece = SemVer.toText
  
instance ToHttpApiData SemVer.Constraint where
  toUrlPiece SemVer.CAny = "*"
  toUrlPiece (SemVer.CLt v) = "<" <> toUrlPiece v
  toUrlPiece (SemVer.CLtEq v) = "<=" <> toUrlPiece v
  toUrlPiece (SemVer.CGt v) = ">" <> toUrlPiece v
  toUrlPiece (SemVer.CGtEq v) = ">=" <> toUrlPiece v
  toUrlPiece (SemVer.CEq v) = toUrlPiece v
  toUrlPiece (SemVer.CAnd a b) = toUrlPiece a <> " " <> toUrlPiece b
  toUrlPiece (SemVer.COr a b) = toUrlPiece a <> " || " <> toUrlPiece b

instance FromHttpApiData SemVer.Version where
  parseUrlPiece = first pack . SemVer.fromText

instance FromHttpApiData SemVer.Constraint where
  parseUrlPiece = first pack . SemVer.Constraint.fromText
