{-# LANGUAGE TemplateHaskell #-}

-- |
-- Lenses for several data types of the 'Distribution.Version' module.
-- All lenses are named after their field names with a 'L' appended.

module CabalLenses.Version where

import Data.Maybe (fromMaybe)
import Distribution.Version
import Distribution.Types.VersionInterval
import Control.Lens


versionBranchL :: Iso' Version [Int]
versionBranchL :: Iso' Version [Int]
versionBranchL = (Version -> [Int]) -> ([Int] -> Version) -> Iso' Version [Int]
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Version -> [Int]
versionNumbers [Int] -> Version
mkVersion


intervals :: Iso' VersionRange [VersionInterval]
intervals :: Iso' VersionRange [VersionInterval]
intervals = (VersionRange -> [VersionInterval])
-> ([VersionInterval] -> VersionRange)
-> Iso' VersionRange [VersionInterval]
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso VersionRange -> [VersionInterval]
asVersionIntervals [VersionInterval] -> VersionRange
toVersionRange
   where
      toVersionRange :: [VersionInterval] -> VersionRange
toVersionRange []        = VersionRange
anyVersion
      toVersionRange [VersionInterval]
intervals = VersionRange -> Maybe VersionRange -> VersionRange
forall a. a -> Maybe a -> a
fromMaybe VersionRange
noVersion (VersionIntervals -> VersionRange
fromVersionIntervals (VersionIntervals -> VersionRange)
-> Maybe VersionIntervals -> Maybe VersionRange
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VersionInterval] -> Maybe VersionIntervals
mkVersionIntervals [VersionInterval]
intervals)


lowerBound :: Lens' VersionInterval LowerBound
lowerBound :: Lens' VersionInterval LowerBound
lowerBound = (VersionInterval -> LowerBound)
-> (VersionInterval -> LowerBound -> VersionInterval)
-> Lens' VersionInterval LowerBound
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens VersionInterval -> LowerBound
getLowerBound VersionInterval -> LowerBound -> VersionInterval
setLowerBound
   where
      getLowerBound :: VersionInterval -> LowerBound
getLowerBound (VersionInterval LowerBound
lowerBound UpperBound
_)            = LowerBound
lowerBound
      setLowerBound :: VersionInterval -> LowerBound -> VersionInterval
setLowerBound (VersionInterval LowerBound
_ UpperBound
upperBound) LowerBound
lowerBound = LowerBound -> UpperBound -> VersionInterval
VersionInterval LowerBound
lowerBound UpperBound
upperBound


version :: Lens' LowerBound Version
version :: Lens' LowerBound Version
version = (LowerBound -> Version)
-> (LowerBound -> Version -> LowerBound)
-> Lens' LowerBound Version
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens LowerBound -> Version
getVersion LowerBound -> Version -> LowerBound
setVersion
   where
      getVersion :: LowerBound -> Version
getVersion (LowerBound Version
vers Bound
_)          = Version
vers
      setVersion :: LowerBound -> Version -> LowerBound
setVersion (LowerBound Version
_    Bound
bound) Version
vers = Version -> Bound -> LowerBound
LowerBound Version
vers Bound
bound


bound :: Lens' LowerBound Bound
bound :: Lens' LowerBound Bound
bound = (LowerBound -> Bound)
-> (LowerBound -> Bound -> LowerBound) -> Lens' LowerBound Bound
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens LowerBound -> Bound
getBound LowerBound -> Bound -> LowerBound
setBound
   where
      getBound :: LowerBound -> Bound
getBound (LowerBound Version
_ Bound
bound)      = Bound
bound
      setBound :: LowerBound -> Bound -> LowerBound
setBound (LowerBound Version
vers Bound
_) Bound
bound = Version -> Bound -> LowerBound
LowerBound Version
vers Bound
bound


upperBound :: Lens' VersionInterval UpperBound
upperBound :: Lens' VersionInterval UpperBound
upperBound = (VersionInterval -> UpperBound)
-> (VersionInterval -> UpperBound -> VersionInterval)
-> Lens' VersionInterval UpperBound
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens VersionInterval -> UpperBound
getUpperBound VersionInterval -> UpperBound -> VersionInterval
setUpperBound
   where
      getUpperBound :: VersionInterval -> UpperBound
getUpperBound (VersionInterval LowerBound
_ UpperBound
upperBound)            = UpperBound
upperBound
      setUpperBound :: VersionInterval -> UpperBound -> VersionInterval
setUpperBound (VersionInterval LowerBound
lowerBound UpperBound
_) UpperBound
upperBound = LowerBound -> UpperBound -> VersionInterval
VersionInterval LowerBound
lowerBound UpperBound
upperBound


noLowerBound :: LowerBound
noLowerBound :: LowerBound
noLowerBound = Version -> Bound -> LowerBound
LowerBound ([Int] -> Version
mkVersion [Int
0]) Bound
InclusiveBound