module Persistence.Range where

import Persistence.Database
import Persistence.Schema as SchemaClass

import Common.Id

import Control.Monad.IO.Class (MonadIO (..))
import Database.Persist

createRange :: MonadIO m
            => Range -> DBMonad m (Maybe FileRangeId)
createRange :: Range -> DBMonad m (Maybe FileRangeId)
createRange range :: Range
range =
  let rangeL :: [Pos]
rangeL = Range -> [Pos]
rangeToList Range
range
  in  if [Pos] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Pos]
rangeL
      then Maybe FileRangeId -> DBMonad m (Maybe FileRangeId)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileRangeId
forall a. Maybe a
Nothing
      else
        let startPos :: Pos
startPos = [Pos] -> Pos
forall a. [a] -> a
head [Pos]
rangeL
            endPosM :: Maybe Pos
endPosM = if [Pos] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Pos] -> Bool) -> [Pos] -> Bool
forall a b. (a -> b) -> a -> b
$ [Pos] -> [Pos]
forall a. [a] -> [a]
tail [Pos]
rangeL
                      then Maybe Pos
forall a. Maybe a
Nothing
                      else Pos -> Maybe Pos
forall a. a -> Maybe a
Just (Pos -> Maybe Pos) -> Pos -> Maybe Pos
forall a b. (a -> b) -> a -> b
$ [Pos] -> Pos
forall a. [a] -> a
head ([Pos] -> Pos) -> [Pos] -> Pos
forall a b. (a -> b) -> a -> b
$ [Pos] -> [Pos]
forall a. [a] -> [a]
tail [Pos]
rangeL
        in (FileRangeId -> Maybe FileRangeId)
-> ReaderT SqlBackend m FileRangeId
-> DBMonad m (Maybe FileRangeId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FileRangeId -> Maybe FileRangeId
forall a. a -> Maybe a
Just (ReaderT SqlBackend m FileRangeId -> DBMonad m (Maybe FileRangeId))
-> ReaderT SqlBackend m FileRangeId
-> DBMonad m (Maybe FileRangeId)
forall a b. (a -> b) -> a -> b
$ FileRange -> ReaderT SqlBackend m FileRangeId
forall backend (m :: * -> *) record.
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
record -> ReaderT backend m (Key record)
insert $WFileRange :: String -> Int -> Int -> Maybe Int -> Maybe Int -> FileRange
SchemaClass.FileRange
             { fileRangePath :: String
fileRangePath = Pos -> String
sourceName Pos
startPos
             , fileRangeStartLine :: Int
fileRangeStartLine = Pos -> Int
sourceLine Pos
startPos
             , fileRangeStartColumn :: Int
fileRangeStartColumn = Pos -> Int
sourceColumn Pos
startPos
             , fileRangeEndLine :: Maybe Int
fileRangeEndLine = (Pos -> Int) -> Maybe Pos -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pos -> Int
sourceLine Maybe Pos
endPosM
             , fileRangeEndColumn :: Maybe Int
fileRangeEndColumn = (Pos -> Int) -> Maybe Pos -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pos -> Int
sourceColumn Maybe Pos
endPosM
             }