yagcdn/frontend/src/Parse.elm

94 lines
2.2 KiB
Elm
Raw Normal View History

2019-07-27 16:24:13 +02:00
module Parse exposing (parseUrl)
import Data exposing (Provider(..), Url, pathSeparator)
parseUrl : String -> Maybe Url
parseUrl url =
stripProtocol url
|> splitProvider
2019-07-28 17:32:50 +02:00
|> Maybe.andThen splitOffHead
|> Maybe.andThen splitOffHead
|> Maybe.andThen splitOffHead
|> Maybe.andThen splitOffHead
2019-07-27 16:24:13 +02:00
|> Maybe.andThen
(\( ( ( ( ( prov, user ), repo ), separator ), gitref ), file ) ->
2019-07-29 22:32:08 +02:00
if
2019-07-29 22:34:40 +02:00
(List.head file |> Maybe.map String.isEmpty |> Maybe.withDefault True)
2019-07-29 22:32:08 +02:00
|| (separator /= pathSeparator prov)
then
2019-07-27 16:24:13 +02:00
Nothing
else
Just
{ prov = prov
, user = user
, repo = repo
, gitref = gitref
, file = String.join "/" file
}
)
2019-07-28 17:32:50 +02:00
splitOffHead : ( a, List b ) -> Maybe ( ( a, b ), List b )
splitOffHead ( head, tail ) =
2019-07-27 16:24:13 +02:00
splitPart tail
|> Maybe.map (\( h, t ) -> ( ( head, h ), t ))
stripProtocol : String -> String
stripProtocol url =
let
index =
String.indexes "://" url
|> List.head
|> Maybe.withDefault -3
in
String.dropLeft (index + 3) url
parseProvider : String -> Maybe Provider
parseProvider prov =
case String.toLower prov of
"github.com" ->
Just GitHub
"bitbucket.org" ->
Just Bitbucket
2019-07-28 17:32:50 +02:00
"gitlab.com" ->
Just GitLab
2019-07-27 16:24:13 +02:00
_ ->
Nothing
splitProvider : String -> Maybe ( Provider, List String )
splitProvider url =
let
split =
String.split "/" url
parts =
splitPart split
in
parts
|> Maybe.andThen
(\( head, tail ) ->
parseProvider head
|> Maybe.map (\prov -> ( prov, tail ))
)
splitPart : List a -> Maybe ( a, List a )
splitPart parts =
let
head =
List.head parts
tail =
List.tail parts
in
head
|> Maybe.andThen (\h -> Maybe.map (\t -> ( h, t )) tail)