migrate-to-gitea/app/Types.hs

63 lines
1.4 KiB
Haskell

module Types
( Gitea,
mkGitea,
User,
mkUser,
Source (..),
sourceFromText,
Destination (..),
destinationFromText,
Forge (..),
forgeFromText,
forgeUrl,
)
where
import Data.Text (splitOn)
import Network.HTTP.Req (Scheme (Https), Url, https)
newtype Gitea = G (Url 'Https)
deriving (Eq, Ord, Show)
mkGitea :: Text -> Gitea
mkGitea = G . https
newtype User = U Text
deriving (Eq, Ord, Show)
mkUser :: Text -> User
mkUser = U
data Source = Source Forge User
deriving (Eq, Ord, Show)
sourceFromText :: Text -> Maybe Source
sourceFromText (splitOn ":" -> [forge, user]) = Just (Source (forgeFromText forge) (mkUser user))
sourceFromText _ = Nothing
data Destination = Destination Gitea User
deriving (Eq, Ord, Show)
destinationFromText :: Text -> Maybe Destination
destinationFromText (splitOn ":" -> [host, user]) = Just (Destination (mkGitea host) (mkUser user))
destinationFromText _ = Nothing
data Forge
= Github
| Gitlab
| Gitea Gitea
deriving (Eq, Ord, Show)
forgeFromText :: Text -> Forge
forgeFromText "github" = Github
forgeFromText "github.com" = Github
forgeFromText "gitlab" = Gitlab
forgeFromText "gitlab.com" = Gitlab
forgeFromText "codeberg" = Gitea (mkGitea "codeberg.org")
forgeFromText host = Gitea (mkGitea host)
forgeUrl :: Forge -> Url 'Https
forgeUrl Github = https "github.com"
forgeUrl Gitlab = https "gitlab.com"
forgeUrl (Gitea (G url)) = url