/
RelativePart.purs
50 lines (41 loc) · 1.33 KB
/
RelativePart.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Data.URI.RelativePart where
import Prelude
import Control.Alt ((<|>))
import Data.Array (catMaybes)
import Data.Lens (Lens', lens)
import Data.Maybe (Maybe(..))
import Data.String as S
import Data.URI (Authority, RelativePart(..), URIPathRel)
import Data.URI.Authority as Authority
import Data.URI.Path (printPath, parseURIPathRel, parsePathNoScheme, parsePathAbsolute, parsePathAbEmpty)
import Text.Parsing.StringParser (Parser)
import Text.Parsing.StringParser.String (string)
parser ∷ Parser RelativePart
parser = withAuth <|> withoutAuth
where
withAuth =
RelativePart
<$> Just <$> (string "//" *> Authority.parser)
<*> parsePathAbEmpty parseURIPathRel
withoutAuth = RelativePart Nothing <$> noAuthPath
noAuthPath
= (Just <$> parsePathAbsolute parseURIPathRel)
<|> (Just <$> parsePathNoScheme parseURIPathRel)
<|> pure Nothing
print ∷ RelativePart → String
print (RelativePart a p) =
S.joinWith "" $
catMaybes
[ (\auth → "//" <> Authority.print auth) <$> a
, printPath <$> p
]
_authority ∷ Lens' RelativePart (Maybe Authority)
_authority =
lens
(\(RelativePart a _) → a)
(\(RelativePart _ p) a → RelativePart a p)
_path ∷ Lens' RelativePart (Maybe URIPathRel)
_path =
lens
(\(RelativePart _ p) → p)
(\(RelativePart a _) p → RelativePart a p)