/
AbsoluteURI.purs
151 lines (139 loc) · 6.01 KB
/
AbsoluteURI.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module URI.AbsoluteURI
( AbsoluteURI(..)
, AbsoluteURIOptions
, AbsoluteURIParseOptions
, AbsoluteURIPrintOptions
, parser
, print
, _scheme
, _hierPart
, _query
, module URI.HierarchicalPart
, module URI.Query
, module URI.Scheme
) where
import Prelude
import Data.Array as Array
import Data.Either (Either)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Lens (Lens', lens)
import Data.Maybe (Maybe(..))
import Data.String as String
import Text.Parsing.Parser (Parser)
import Text.Parsing.Parser.Combinators (optionMaybe)
import Text.Parsing.Parser.String (eof)
import URI.Common (URIPartParseError, wrapParser)
import URI.HierarchicalPart (Authority(..), AuthorityOptions, AuthorityParseOptions, AuthorityPrintOptions, HierPath, HierarchicalPart(..), HierarchicalPartOptions, HierarchicalPartParseOptions, HierarchicalPartPrintOptions, Host(..), IPv4Address, IPv6Address, Path(..), PathAbsolute(..), PathRootless(..), Port, RegName, UserInfo, _IPv4Address, _IPv6Address, _NameAddress, _authority, _hierPath, _hosts, _path, _userInfo)
import URI.HierarchicalPart as HPart
import URI.Query (Query)
import URI.Query as Query
import URI.Scheme (Scheme)
import URI.Scheme as Scheme
-- | A strictly absolute URI. An absolute URI can still contain relative paths
-- | but is required to have a `Scheme` component.
data AbsoluteURI userInfo hosts path hierPath query = AbsoluteURI Scheme (HierarchicalPart userInfo hosts path hierPath) (Maybe query)
derive instance eqAbsoluteURI ∷ (Eq userInfo, Eq hosts, Eq path, Eq hierPath, Eq query) ⇒ Eq (AbsoluteURI userInfo hosts path hierPath query)
derive instance ordAbsoluteURI ∷ (Ord userInfo, Ord hosts, Ord path, Ord hierPath, Ord query) ⇒ Ord (AbsoluteURI userInfo hosts path hierPath query)
derive instance genericAbsoluteURI ∷ Generic (AbsoluteURI userInfo hosts path hierPath query) _
instance showAbsoluteURI ∷ (Show userInfo, Show hosts, Show path, Show hierPath, Show query) ⇒ Show (AbsoluteURI userInfo hosts path hierPath query) where show = genericShow
-- | A row type for describing the options fields used by the absolute URI
-- | parser and printer.
-- |
-- | Used as `Record (AbsoluteURIOptions userInfo hosts path hierPath query)`
-- | when type anotating an options record.
-- |
-- | See below for details of how to use these configuration options.
type AbsoluteURIOptions userInfo hosts path hierPath query =
AbsoluteURIParseOptions userInfo hosts path hierPath query
(AbsoluteURIPrintOptions userInfo hosts path hierPath query ())
-- | A row type for describing the options fields used by the absolute URI
-- | parser.
-- |
-- | Used as `Record (AbsoluteURIParseOptions userInfo hosts path hierPath query ())`
-- | when type anotating an options record.
-- |
-- | Having this options record allows custom representations to be used for
-- | the URI components. If this is not necessary, `pure` can be used for all
-- | the options aside from `parseHosts`, which will typically be
-- | `HostPortPair.parseHosts pure pure`. See [`URI.HostPortPair`](../URI.HostPortPair)
-- | for more information on the host/port pair parser.
type AbsoluteURIParseOptions userInfo hosts path hierPath query r =
( parseUserInfo ∷ UserInfo → Either URIPartParseError userInfo
, parseHosts ∷ Parser String hosts
, parsePath ∷ Path → Either URIPartParseError path
, parseHierPath ∷ Either PathAbsolute PathRootless → Either URIPartParseError hierPath
, parseQuery ∷ Query → Either URIPartParseError query
| r
)
-- | A row type for describing the options fields used by the absolute URI
-- | printer.
-- |
-- | Used as `Record (AbsoluteURIPrintOptions userInfo hosts path hierPath query ())`
-- | when type anotating an options record.
-- |
-- | As a reverse of the parse options, this specifies how to print values back
-- | from custom representations. If this is not necessary, `id` can be used for
-- | all the options aside from `printHosts`, which will typically be
-- | `HostPortPair.printHosts id id`. See [`URI.HostPortPair`](../URI.HostPortPair)
-- | for more information on the host/port pair printer.
type AbsoluteURIPrintOptions userInfo hosts path hierPath query r =
( printUserInfo ∷ userInfo → UserInfo
, printHosts ∷ hosts → String
, printPath ∷ path → Path
, printHierPath ∷ hierPath → Either PathAbsolute PathRootless
, printQuery ∷ query → Query
| r
)
-- | A parser for an absolute URI.
parser
∷ ∀ userInfo hosts path hierPath query r
. Record (AbsoluteURIParseOptions userInfo hosts path hierPath query r)
→ Parser String (AbsoluteURI userInfo hosts path hierPath query)
parser opts = AbsoluteURI
<$> Scheme.parser
<*> HPart.parser opts
<*> optionMaybe (wrapParser opts.parseQuery Query.parser)
<* eof
-- | A printer for an absolute URI.
print
∷ ∀ userInfo hosts path hierPath query r
. Record (AbsoluteURIPrintOptions userInfo hosts path hierPath query r)
→ AbsoluteURI userInfo hosts path hierPath query
→ String
print opts (AbsoluteURI s h q) =
String.joinWith "" $ Array.catMaybes
[ Just (Scheme.print s)
, Just (HPart.print opts h)
, Query.print <<< opts.printQuery <$> q
]
-- | The scheme component of an absolute URI.
_scheme
∷ ∀ userInfo hosts path hierPath query
. Lens'
(AbsoluteURI userInfo hosts path hierPath query)
Scheme
_scheme =
lens
(\(AbsoluteURI s _ _) → s)
(\(AbsoluteURI _ h q) s → AbsoluteURI s h q)
-- | The hierarchical-part component of an absolute URI.
_hierPart
∷ ∀ userInfo hosts path hierPath query
. Lens'
(AbsoluteURI userInfo hosts path hierPath query)
(HierarchicalPart userInfo hosts path hierPath)
_hierPart =
lens
(\(AbsoluteURI _ h _) → h)
(\(AbsoluteURI s _ q) h → AbsoluteURI s h q)
-- | The query component of an absolute URI.
_query
∷ ∀ userInfo hosts path hierPath query
. Lens'
(AbsoluteURI userInfo hosts path hierPath query)
(Maybe query)
_query =
lens
(\(AbsoluteURI _ _ q) → q)
(\(AbsoluteURI s h _) q → AbsoluteURI s h q)