module Language.Docker.Syntax.PortRange where


import Prettyprinter
import Language.Docker.Syntax.Port
import Language.Docker.Syntax.Protocol


-- | A port range starts and ends with either a number or a variable and can
-- have a protocol associated (tcp by default). The protocol of the start and
-- end port shall be ignored.
data PortRange
  = PortRange !Port !Port
  deriving (Int -> PortRange -> ShowS
[PortRange] -> ShowS
PortRange -> String
(Int -> PortRange -> ShowS)
-> (PortRange -> String)
-> ([PortRange] -> ShowS)
-> Show PortRange
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PortRange -> ShowS
showsPrec :: Int -> PortRange -> ShowS
$cshow :: PortRange -> String
show :: PortRange -> String
$cshowList :: [PortRange] -> ShowS
showList :: [PortRange] -> ShowS
Show, PortRange -> PortRange -> Bool
(PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> Bool) -> Eq PortRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PortRange -> PortRange -> Bool
== :: PortRange -> PortRange -> Bool
$c/= :: PortRange -> PortRange -> Bool
/= :: PortRange -> PortRange -> Bool
Eq, Eq PortRange
Eq PortRange =>
(PortRange -> PortRange -> Ordering)
-> (PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> Bool)
-> (PortRange -> PortRange -> PortRange)
-> (PortRange -> PortRange -> PortRange)
-> Ord PortRange
PortRange -> PortRange -> Bool
PortRange -> PortRange -> Ordering
PortRange -> PortRange -> PortRange
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PortRange -> PortRange -> Ordering
compare :: PortRange -> PortRange -> Ordering
$c< :: PortRange -> PortRange -> Bool
< :: PortRange -> PortRange -> Bool
$c<= :: PortRange -> PortRange -> Bool
<= :: PortRange -> PortRange -> Bool
$c> :: PortRange -> PortRange -> Bool
> :: PortRange -> PortRange -> Bool
$c>= :: PortRange -> PortRange -> Bool
>= :: PortRange -> PortRange -> Bool
$cmax :: PortRange -> PortRange -> PortRange
max :: PortRange -> PortRange -> PortRange
$cmin :: PortRange -> PortRange -> PortRange
min :: PortRange -> PortRange -> PortRange
Ord)


instance Pretty PortRange where
  pretty :: forall ann. PortRange -> Doc ann
pretty (PortRange (Port Int
start Protocol
UDP) (Port Int
end Protocol
UDP)) = Int -> Doc ann
forall ann. Int -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Int
start Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"-" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Int -> Doc ann
forall ann. Int -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Int
end Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"/udp"
  pretty (PortRange (Port Int
start Protocol
UDP) Port
end) = Int -> Doc ann
forall ann. Int -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Int
start Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"-" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Port -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Port -> Doc ann
pretty Port
end Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"/udp"
  pretty (PortRange Port
start Port
end) = Port -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Port -> Doc ann
pretty Port
start Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"-" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Port -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Port -> Doc ann
pretty Port
end