|
1 |
| -module Data.URI |
2 |
| - ( module Data.URI |
3 |
| - , module Data.URI.Types |
4 |
| - ) where |
| 1 | +module Data.URI where |
5 | 2 |
|
6 | 3 | import Prelude
|
7 | 4 |
|
8 |
| -import Control.Alt ((<|>)) |
9 |
| - |
10 |
| -import Data.Array (catMaybes) |
11 |
| -import Data.Either (Either(..), either) |
12 |
| -import Data.Maybe (Maybe(..)) |
13 |
| -import Data.String as S |
14 |
| -import Data.URI.Fragment (parseFragment) |
15 |
| -import Data.URI.HierarchicalPart (printHierPart, parseHierarchicalPart) |
16 |
| -import Data.URI.Query (printQuery, parseQuery) |
17 |
| -import Data.URI.RelativePart (printRelativePart, parseRelativePart) |
18 |
| -import Data.URI.Scheme (printScheme, parseScheme) |
19 |
| -import Data.URI.Types (Fragment, Port, URIPath, URIPathAbs, URIPathRel, URIRef, UserInfo, AbsoluteURI(..), Authority(..), HierarchicalPart(..), Host(..), Query(..), RelativePart(..), RelativeRef(..), URI(..), URIScheme(..)) |
20 |
| - |
21 |
| -import Text.Parsing.StringParser (Parser, ParseError, runParser, try) |
22 |
| -import Text.Parsing.StringParser.Combinators (optionMaybe) |
23 |
| -import Text.Parsing.StringParser.String (string, eof) |
24 |
| - |
25 |
| -runParseURIRef ∷ String → Either ParseError URIRef |
26 |
| -runParseURIRef = runParser parseURIRef |
27 |
| - |
28 |
| -runParseURI ∷ String → Either ParseError URI |
29 |
| -runParseURI = runParser parseURI |
30 |
| - |
31 |
| -runParseAbsoluteURI ∷ String → Either ParseError AbsoluteURI |
32 |
| -runParseAbsoluteURI = runParser parseAbsoluteURI |
33 |
| - |
34 |
| -runParseRelativeRef ∷ String → Either ParseError RelativeRef |
35 |
| -runParseRelativeRef = runParser parseRelativeRef |
36 |
| - |
37 |
| -parseURIRef ∷ Parser URIRef |
38 |
| -parseURIRef |
39 |
| - = (Left <$> try parseURI) |
40 |
| - <|> (Right <$> parseRelativeRef) |
41 |
| - |
42 |
| -parseURI ∷ Parser URI |
43 |
| -parseURI = URI |
44 |
| - <$> (parseScheme <* string ":") |
45 |
| - <*> parseHierarchicalPart |
46 |
| - <*> optionMaybe (string "?" *> parseQuery) |
47 |
| - <*> optionMaybe (string "#" *> parseFragment) |
48 |
| - <* eof |
49 |
| - |
50 |
| -parseAbsoluteURI ∷ Parser AbsoluteURI |
51 |
| -parseAbsoluteURI = AbsoluteURI |
52 |
| - <$> (parseScheme <* string ":") |
53 |
| - <*> parseHierarchicalPart |
54 |
| - <*> optionMaybe (string "?" *> parseQuery) |
55 |
| - <* eof |
56 |
| - |
57 |
| -parseRelativeRef ∷ Parser RelativeRef |
58 |
| -parseRelativeRef = RelativeRef |
59 |
| - <$> parseRelativePart |
60 |
| - <*> optionMaybe (string "?" *> parseQuery) |
61 |
| - <*> optionMaybe (string "#" *> parseFragment) |
62 |
| - <* eof |
63 |
| - |
64 |
| -printURIRef ∷ URIRef → String |
65 |
| -printURIRef = either printURI printRelativeRef |
66 |
| - |
67 |
| -printURI ∷ URI → String |
68 |
| -printURI (URI s h q f) = |
69 |
| - S.joinWith "" $ catMaybes |
70 |
| - [ printScheme <$> s |
71 |
| - , Just (printHierPart h) |
72 |
| - , printQuery <$> q |
73 |
| - , ("#" <> _) <$> f |
74 |
| - ] |
75 |
| - |
76 |
| -printAbsoluteURI ∷ AbsoluteURI → String |
77 |
| -printAbsoluteURI (AbsoluteURI s h q) = |
78 |
| - S.joinWith "" $ catMaybes |
79 |
| - [ printScheme <$> s |
80 |
| - , Just (printHierPart h) |
81 |
| - , printQuery <$> q |
82 |
| - ] |
83 |
| - |
84 |
| -printRelativeRef ∷ RelativeRef → String |
85 |
| -printRelativeRef (RelativeRef h q f) = |
86 |
| - S.joinWith "" $ catMaybes |
87 |
| - [ Just (printRelativePart h) |
88 |
| - , printQuery <$> q |
89 |
| - , ("#" <> _) <$> f |
90 |
| - ] |
| 5 | +import Data.Either (Either) |
| 6 | +import Data.Generic.Rep (class Generic) |
| 7 | +import Data.Generic.Rep.Show (genericShow) |
| 8 | +import Data.List (List) |
| 9 | +import Data.Maybe (Maybe) |
| 10 | +import Data.Monoid (class Monoid) |
| 11 | +import Data.Newtype (class Newtype) |
| 12 | +import Data.Path.Pathy (Path, File, Dir, Abs, Rel, Sandboxed, Unsandboxed) |
| 13 | +import Data.Tuple (Tuple) |
| 14 | + |
| 15 | +-- | A generic URI |
| 16 | +data URI = URI (Maybe Scheme) HierarchicalPart (Maybe Query) (Maybe Fragment) |
| 17 | + |
| 18 | +derive instance eqURI ∷ Eq URI |
| 19 | +derive instance ordURI ∷ Ord URI |
| 20 | +derive instance genericURI ∷ Generic URI _ |
| 21 | +instance showURI ∷ Show URI where show = genericShow |
| 22 | + |
| 23 | +-- | An absolute URI. |
| 24 | +data AbsoluteURI = AbsoluteURI (Maybe Scheme) HierarchicalPart (Maybe Query) |
| 25 | + |
| 26 | +derive instance eqAbsoluteURI ∷ Eq AbsoluteURI |
| 27 | +derive instance ordAbsoluteURI ∷ Ord AbsoluteURI |
| 28 | +derive instance genericAbsoluteURI ∷ Generic AbsoluteURI _ |
| 29 | +instance showAbsoluteURI ∷ Show AbsoluteURI where show = genericShow |
| 30 | + |
| 31 | +-- | A relative reference for a URI. |
| 32 | +data RelativeRef = RelativeRef RelativePart (Maybe Query) (Maybe Fragment) |
| 33 | + |
| 34 | +derive instance eqRelativeRef ∷ Eq RelativeRef |
| 35 | +derive instance ordRelativeRef ∷ Ord RelativeRef |
| 36 | +derive instance genericRelativeRef ∷ Generic RelativeRef _ |
| 37 | +instance showRelativeRef ∷ Show RelativeRef where show = genericShow |
| 38 | + |
| 39 | +-- | A general URI path, can be used to represent relative or absolute paths |
| 40 | +-- | that are sandboxed or unsandboxed. |
| 41 | +type URIPath a s = Either (Path a Dir s) (Path a File s) |
| 42 | + |
| 43 | +-- | The path part for a generic or absolute URI. |
| 44 | +type URIPathAbs = URIPath Abs Sandboxed |
| 45 | + |
| 46 | +-- | The path part for a relative reference. |
| 47 | +type URIPathRel = URIPath Rel Unsandboxed |
| 48 | + |
| 49 | +-- | An alias for the most common use case of resource identifiers. |
| 50 | +type URIRef = Either URI RelativeRef |
| 51 | + |
| 52 | +-- | The scheme part of an absolute URI. For example: `http`, `ftp`, `git`. |
| 53 | +newtype Scheme = Scheme String |
| 54 | + |
| 55 | +derive newtype instance eqScheme ∷ Eq Scheme |
| 56 | +derive newtype instance ordScheme ∷ Ord Scheme |
| 57 | +derive instance genericScheme ∷ Generic Scheme _ |
| 58 | +derive instance newtypeScheme ∷ Newtype Scheme _ |
| 59 | +instance showScheme ∷ Show Scheme where show = genericShow |
| 60 | + |
| 61 | +-- | The "hierarchical part" of a generic or absolute URI. |
| 62 | +data HierarchicalPart = HierarchicalPart (Maybe Authority) (Maybe URIPathAbs) |
| 63 | + |
| 64 | +derive instance eqHierarchicalPart ∷ Eq HierarchicalPart |
| 65 | +derive instance ordHierarchicalPart ∷ Ord HierarchicalPart |
| 66 | +derive instance genericHierarchicalPart ∷ Generic HierarchicalPart _ |
| 67 | +instance showHierarchicalPart ∷ Show HierarchicalPart where show = genericShow |
| 68 | + |
| 69 | +-- | The "relative part" of a relative reference. |
| 70 | +data RelativePart = RelativePart (Maybe Authority) (Maybe URIPathRel) |
| 71 | + |
| 72 | +derive instance eqRelativePart ∷ Eq RelativePart |
| 73 | +derive instance ordRelativePart ∷ Ord RelativePart |
| 74 | +derive instance genericRelativePart ∷ Generic RelativePart _ |
| 75 | +instance showRelativePart ∷ Show RelativePart where show = genericShow |
| 76 | + |
| 77 | +-- | The authority part of a URI. For example: `purescript.org`, |
| 78 | +-- | `localhost:3000`, `[email protected]` |
| 79 | +data Authority = Authority (Maybe UserInfo) (Array (Tuple Host (Maybe Port))) |
| 80 | + |
| 81 | +derive instance eqAuthority ∷ Eq Authority |
| 82 | +derive instance ordAuthority ∷ Ord Authority |
| 83 | +derive instance genericAuthority ∷ Generic Authority _ |
| 84 | +instance showAuthority ∷ Show Authority where show = genericShow |
| 85 | + |
| 86 | +-- | The user info part of an `Authority`. For example: `user`, `foo:bar`. |
| 87 | +newtype UserInfo = UserInfo String |
| 88 | + |
| 89 | +derive newtype instance eqUserInfo ∷ Eq UserInfo |
| 90 | +derive newtype instance ordUserInfo ∷ Ord UserInfo |
| 91 | +derive instance genericUserInfo ∷ Generic UserInfo _ |
| 92 | +derive instance newtypeUserInfo ∷ Newtype UserInfo _ |
| 93 | +instance showUserInfo ∷ Show UserInfo where show = genericShow |
| 94 | + |
| 95 | +-- | A host address. |
| 96 | +data Host |
| 97 | + = IPv6Address String |
| 98 | + | IPv4Address String |
| 99 | + | NameAddress String |
| 100 | + |
| 101 | +derive instance eqHost ∷ Eq Host |
| 102 | +derive instance ordHost ∷ Ord Host |
| 103 | +derive instance genericHost ∷ Generic Host _ |
| 104 | +instance showHost ∷ Show Host where show = genericShow |
| 105 | + |
| 106 | +-- | A port number. |
| 107 | +newtype Port = Port Int |
| 108 | + |
| 109 | +derive newtype instance eqPort ∷ Eq Port |
| 110 | +derive newtype instance ordPort ∷ Ord Port |
| 111 | +derive instance genericPort ∷ Generic Port _ |
| 112 | +derive instance newtypePort ∷ Newtype Port _ |
| 113 | +instance showPort ∷ Show Port where show = genericShow |
| 114 | + |
| 115 | +-- | The query component of a URI. |
| 116 | +newtype Query = Query (List (Tuple String (Maybe String))) |
| 117 | + |
| 118 | +derive newtype instance eqQuery ∷ Eq Query |
| 119 | +derive newtype instance ordQuery ∷ Ord Query |
| 120 | +derive instance genericQuery ∷ Generic Query _ |
| 121 | +derive instance newtypeQuery ∷ Newtype Query _ |
| 122 | +instance showQuery ∷ Show Query where show = genericShow |
| 123 | +derive newtype instance semigroupQuery ∷ Semigroup Query |
| 124 | +derive newtype instance monoidQuery ∷ Monoid Query |
| 125 | + |
| 126 | +-- | The hash fragment of a URI. |
| 127 | +newtype Fragment = Fragment String |
| 128 | + |
| 129 | +derive newtype instance eqFragment ∷ Eq Fragment |
| 130 | +derive newtype instance ordFragment ∷ Ord Fragment |
| 131 | +derive instance genericFragment ∷ Generic Fragment _ |
| 132 | +derive instance newtypeFragment ∷ Newtype Fragment _ |
| 133 | +instance showFragment ∷ Show Fragment where show = genericShow |
0 commit comments