-- | Project-related types.
--
-- A larger API, including orphan instances for parsing from 'Text', is available in "Unison.Project". Here, we just
-- define the types, which are shared among the low-level database layer (which assumes without verifying that project
-- names and such are syntactically valid) and the higher-level project manipulation exposed by UCM.
module Unison.Core.Project
  ( ProjectName (..),
    ProjectBranchName (..),
    ProjectAndBranch (..),
  )
where

import Data.Bifoldable (Bifoldable (..))
import Data.Bitraversable (Bitraversable (..))
import Unison.Prelude

-- | The name of a project.
newtype ProjectName
  = UnsafeProjectName Text
  deriving stock (ProjectName -> ProjectName -> Bool
(ProjectName -> ProjectName -> Bool)
-> (ProjectName -> ProjectName -> Bool) -> Eq ProjectName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProjectName -> ProjectName -> Bool
== :: ProjectName -> ProjectName -> Bool
$c/= :: ProjectName -> ProjectName -> Bool
/= :: ProjectName -> ProjectName -> Bool
Eq, Eq ProjectName
Eq ProjectName =>
(ProjectName -> ProjectName -> Ordering)
-> (ProjectName -> ProjectName -> Bool)
-> (ProjectName -> ProjectName -> Bool)
-> (ProjectName -> ProjectName -> Bool)
-> (ProjectName -> ProjectName -> Bool)
-> (ProjectName -> ProjectName -> ProjectName)
-> (ProjectName -> ProjectName -> ProjectName)
-> Ord ProjectName
ProjectName -> ProjectName -> Bool
ProjectName -> ProjectName -> Ordering
ProjectName -> ProjectName -> ProjectName
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 :: ProjectName -> ProjectName -> Ordering
compare :: ProjectName -> ProjectName -> Ordering
$c< :: ProjectName -> ProjectName -> Bool
< :: ProjectName -> ProjectName -> Bool
$c<= :: ProjectName -> ProjectName -> Bool
<= :: ProjectName -> ProjectName -> Bool
$c> :: ProjectName -> ProjectName -> Bool
> :: ProjectName -> ProjectName -> Bool
$c>= :: ProjectName -> ProjectName -> Bool
>= :: ProjectName -> ProjectName -> Bool
$cmax :: ProjectName -> ProjectName -> ProjectName
max :: ProjectName -> ProjectName -> ProjectName
$cmin :: ProjectName -> ProjectName -> ProjectName
min :: ProjectName -> ProjectName -> ProjectName
Ord, Int -> ProjectName -> ShowS
[ProjectName] -> ShowS
ProjectName -> String
(Int -> ProjectName -> ShowS)
-> (ProjectName -> String)
-> ([ProjectName] -> ShowS)
-> Show ProjectName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectName -> ShowS
showsPrec :: Int -> ProjectName -> ShowS
$cshow :: ProjectName -> String
show :: ProjectName -> String
$cshowList :: [ProjectName] -> ShowS
showList :: [ProjectName] -> ShowS
Show, (forall x. ProjectName -> Rep ProjectName x)
-> (forall x. Rep ProjectName x -> ProjectName)
-> Generic ProjectName
forall x. Rep ProjectName x -> ProjectName
forall x. ProjectName -> Rep ProjectName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProjectName -> Rep ProjectName x
from :: forall x. ProjectName -> Rep ProjectName x
$cto :: forall x. Rep ProjectName x -> ProjectName
to :: forall x. Rep ProjectName x -> ProjectName
Generic)

-- | The name of a branch of a project.
newtype ProjectBranchName
  = UnsafeProjectBranchName Text
  deriving stock (ProjectBranchName -> ProjectBranchName -> Bool
(ProjectBranchName -> ProjectBranchName -> Bool)
-> (ProjectBranchName -> ProjectBranchName -> Bool)
-> Eq ProjectBranchName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProjectBranchName -> ProjectBranchName -> Bool
== :: ProjectBranchName -> ProjectBranchName -> Bool
$c/= :: ProjectBranchName -> ProjectBranchName -> Bool
/= :: ProjectBranchName -> ProjectBranchName -> Bool
Eq, Eq ProjectBranchName
Eq ProjectBranchName =>
(ProjectBranchName -> ProjectBranchName -> Ordering)
-> (ProjectBranchName -> ProjectBranchName -> Bool)
-> (ProjectBranchName -> ProjectBranchName -> Bool)
-> (ProjectBranchName -> ProjectBranchName -> Bool)
-> (ProjectBranchName -> ProjectBranchName -> Bool)
-> (ProjectBranchName -> ProjectBranchName -> ProjectBranchName)
-> (ProjectBranchName -> ProjectBranchName -> ProjectBranchName)
-> Ord ProjectBranchName
ProjectBranchName -> ProjectBranchName -> Bool
ProjectBranchName -> ProjectBranchName -> Ordering
ProjectBranchName -> ProjectBranchName -> ProjectBranchName
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 :: ProjectBranchName -> ProjectBranchName -> Ordering
compare :: ProjectBranchName -> ProjectBranchName -> Ordering
$c< :: ProjectBranchName -> ProjectBranchName -> Bool
< :: ProjectBranchName -> ProjectBranchName -> Bool
$c<= :: ProjectBranchName -> ProjectBranchName -> Bool
<= :: ProjectBranchName -> ProjectBranchName -> Bool
$c> :: ProjectBranchName -> ProjectBranchName -> Bool
> :: ProjectBranchName -> ProjectBranchName -> Bool
$c>= :: ProjectBranchName -> ProjectBranchName -> Bool
>= :: ProjectBranchName -> ProjectBranchName -> Bool
$cmax :: ProjectBranchName -> ProjectBranchName -> ProjectBranchName
max :: ProjectBranchName -> ProjectBranchName -> ProjectBranchName
$cmin :: ProjectBranchName -> ProjectBranchName -> ProjectBranchName
min :: ProjectBranchName -> ProjectBranchName -> ProjectBranchName
Ord, Int -> ProjectBranchName -> ShowS
[ProjectBranchName] -> ShowS
ProjectBranchName -> String
(Int -> ProjectBranchName -> ShowS)
-> (ProjectBranchName -> String)
-> ([ProjectBranchName] -> ShowS)
-> Show ProjectBranchName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProjectBranchName -> ShowS
showsPrec :: Int -> ProjectBranchName -> ShowS
$cshow :: ProjectBranchName -> String
show :: ProjectBranchName -> String
$cshowList :: [ProjectBranchName] -> ShowS
showList :: [ProjectBranchName] -> ShowS
Show, (forall x. ProjectBranchName -> Rep ProjectBranchName x)
-> (forall x. Rep ProjectBranchName x -> ProjectBranchName)
-> Generic ProjectBranchName
forall x. Rep ProjectBranchName x -> ProjectBranchName
forall x. ProjectBranchName -> Rep ProjectBranchName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProjectBranchName -> Rep ProjectBranchName x
from :: forall x. ProjectBranchName -> Rep ProjectBranchName x
$cto :: forall x. Rep ProjectBranchName x -> ProjectBranchName
to :: forall x. Rep ProjectBranchName x -> ProjectBranchName
Generic)

-- | A generic data structure that contains information about a project and a branch in that project.
data ProjectAndBranch a b = ProjectAndBranch
  { forall a b. ProjectAndBranch a b -> a
project :: a,
    forall a b. ProjectAndBranch a b -> b
branch :: b
  }
  deriving stock (ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
(ProjectAndBranch a b -> ProjectAndBranch a b -> Bool)
-> (ProjectAndBranch a b -> ProjectAndBranch a b -> Bool)
-> Eq (ProjectAndBranch a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b.
(Eq a, Eq b) =>
ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
$c== :: forall a b.
(Eq a, Eq b) =>
ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
== :: ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
$c/= :: forall a b.
(Eq a, Eq b) =>
ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
/= :: ProjectAndBranch a b -> ProjectAndBranch a b -> Bool
Eq, (forall x. ProjectAndBranch a b -> Rep (ProjectAndBranch a b) x)
-> (forall x. Rep (ProjectAndBranch a b) x -> ProjectAndBranch a b)
-> Generic (ProjectAndBranch a b)
forall x. Rep (ProjectAndBranch a b) x -> ProjectAndBranch a b
forall x. ProjectAndBranch a b -> Rep (ProjectAndBranch a b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a b x. Rep (ProjectAndBranch a b) x -> ProjectAndBranch a b
forall a b x. ProjectAndBranch a b -> Rep (ProjectAndBranch a b) x
$cfrom :: forall a b x. ProjectAndBranch a b -> Rep (ProjectAndBranch a b) x
from :: forall x. ProjectAndBranch a b -> Rep (ProjectAndBranch a b) x
$cto :: forall a b x. Rep (ProjectAndBranch a b) x -> ProjectAndBranch a b
to :: forall x. Rep (ProjectAndBranch a b) x -> ProjectAndBranch a b
Generic, Int -> ProjectAndBranch a b -> ShowS
[ProjectAndBranch a b] -> ShowS
ProjectAndBranch a b -> String
(Int -> ProjectAndBranch a b -> ShowS)
-> (ProjectAndBranch a b -> String)
-> ([ProjectAndBranch a b] -> ShowS)
-> Show (ProjectAndBranch a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b.
(Show a, Show b) =>
Int -> ProjectAndBranch a b -> ShowS
forall a b. (Show a, Show b) => [ProjectAndBranch a b] -> ShowS
forall a b. (Show a, Show b) => ProjectAndBranch a b -> String
$cshowsPrec :: forall a b.
(Show a, Show b) =>
Int -> ProjectAndBranch a b -> ShowS
showsPrec :: Int -> ProjectAndBranch a b -> ShowS
$cshow :: forall a b. (Show a, Show b) => ProjectAndBranch a b -> String
show :: ProjectAndBranch a b -> String
$cshowList :: forall a b. (Show a, Show b) => [ProjectAndBranch a b] -> ShowS
showList :: [ProjectAndBranch a b] -> ShowS
Show, (forall a b.
 (a -> b) -> ProjectAndBranch a a -> ProjectAndBranch a b)
-> (forall a b. a -> ProjectAndBranch a b -> ProjectAndBranch a a)
-> Functor (ProjectAndBranch a)
forall a b. a -> ProjectAndBranch a b -> ProjectAndBranch a a
forall a b.
(a -> b) -> ProjectAndBranch a a -> ProjectAndBranch a b
forall a a b. a -> ProjectAndBranch a b -> ProjectAndBranch a a
forall a a b.
(a -> b) -> ProjectAndBranch a a -> ProjectAndBranch a b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a a b.
(a -> b) -> ProjectAndBranch a a -> ProjectAndBranch a b
fmap :: forall a b.
(a -> b) -> ProjectAndBranch a a -> ProjectAndBranch a b
$c<$ :: forall a a b. a -> ProjectAndBranch a b -> ProjectAndBranch a a
<$ :: forall a b. a -> ProjectAndBranch a b -> ProjectAndBranch a a
Functor)

instance Bifunctor ProjectAndBranch where
  bimap :: forall a b c d.
(a -> b)
-> (c -> d) -> ProjectAndBranch a c -> ProjectAndBranch b d
bimap a -> b
f c -> d
g (ProjectAndBranch a
a c
b) = b -> d -> ProjectAndBranch b d
forall a b. a -> b -> ProjectAndBranch a b
ProjectAndBranch (a -> b
f a
a) (c -> d
g c
b)

instance Bifoldable ProjectAndBranch where
  bifoldMap :: forall m a b.
Monoid m =>
(a -> m) -> (b -> m) -> ProjectAndBranch a b -> m
bifoldMap a -> m
f b -> m
g (ProjectAndBranch a
a b
b) = a -> m
f a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> b -> m
g b
b

instance Bitraversable ProjectAndBranch where
  bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c)
-> (b -> f d) -> ProjectAndBranch a b -> f (ProjectAndBranch c d)
bitraverse a -> f c
f b -> f d
g (ProjectAndBranch a
a b
b) = c -> d -> ProjectAndBranch c d
forall a b. a -> b -> ProjectAndBranch a b
ProjectAndBranch (c -> d -> ProjectAndBranch c d)
-> f c -> f (d -> ProjectAndBranch c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
a f (d -> ProjectAndBranch c d) -> f d -> f (ProjectAndBranch c d)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> b -> f d
g b
b