Skip to content

Commit fb06076

Browse files
authored
Merge pull request #24 from garyb/reorganise-and-fix-encoding
Reorganise and fix encoding
2 parents a9e93c3 + 66b4c27 commit fb06076

20 files changed

+627
-511
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"purescript-pathy": "^4.0.0",
2424
"purescript-string-parsers": "^3.0.0",
2525
"purescript-unfoldable": "^3.0.0",
26-
"purescript-generics": "^4.0.0"
26+
"purescript-generics-rep": "^5.2.0"
2727
},
2828
"devDependencies": {
2929
"purescript-test-unit": "11.0.0",
30-
"purescript-strongcheck": "^3.1.0"
30+
"purescript-quickcheck": "^4.4.0"
3131
}
3232
}

src/Data/URI.purs

Lines changed: 130 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,133 @@
1-
module Data.URI
2-
( module Data.URI
3-
, module Data.URI.Types
4-
) where
1+
module Data.URI where
52

63
import Prelude
74

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 eqURIEq URI
19+
derive instance ordURIOrd URI
20+
derive instance genericURIGeneric URI _
21+
instance showURIShow URI where show = genericShow
22+
23+
-- | An absolute URI.
24+
data AbsoluteURI = AbsoluteURI (Maybe Scheme) HierarchicalPart (Maybe Query)
25+
26+
derive instance eqAbsoluteURIEq AbsoluteURI
27+
derive instance ordAbsoluteURIOrd AbsoluteURI
28+
derive instance genericAbsoluteURIGeneric AbsoluteURI _
29+
instance showAbsoluteURIShow 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 eqRelativeRefEq RelativeRef
35+
derive instance ordRelativeRefOrd RelativeRef
36+
derive instance genericRelativeRefGeneric RelativeRef _
37+
instance showRelativeRefShow 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 eqSchemeEq Scheme
56+
derive newtype instance ordSchemeOrd Scheme
57+
derive instance genericSchemeGeneric Scheme _
58+
derive instance newtypeSchemeNewtype Scheme _
59+
instance showSchemeShow 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 eqHierarchicalPartEq HierarchicalPart
65+
derive instance ordHierarchicalPartOrd HierarchicalPart
66+
derive instance genericHierarchicalPartGeneric HierarchicalPart _
67+
instance showHierarchicalPartShow 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 eqRelativePartEq RelativePart
73+
derive instance ordRelativePartOrd RelativePart
74+
derive instance genericRelativePartGeneric RelativePart _
75+
instance showRelativePartShow 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 eqAuthorityEq Authority
82+
derive instance ordAuthorityOrd Authority
83+
derive instance genericAuthorityGeneric Authority _
84+
instance showAuthorityShow 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 eqUserInfoEq UserInfo
90+
derive newtype instance ordUserInfoOrd UserInfo
91+
derive instance genericUserInfoGeneric UserInfo _
92+
derive instance newtypeUserInfoNewtype UserInfo _
93+
instance showUserInfoShow 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 eqHostEq Host
102+
derive instance ordHostOrd Host
103+
derive instance genericHostGeneric Host _
104+
instance showHostShow Host where show = genericShow
105+
106+
-- | A port number.
107+
newtype Port = Port Int
108+
109+
derive newtype instance eqPortEq Port
110+
derive newtype instance ordPortOrd Port
111+
derive instance genericPortGeneric Port _
112+
derive instance newtypePortNewtype Port _
113+
instance showPortShow 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 eqQueryEq Query
119+
derive newtype instance ordQueryOrd Query
120+
derive instance genericQueryGeneric Query _
121+
derive instance newtypeQueryNewtype Query _
122+
instance showQueryShow Query where show = genericShow
123+
derive newtype instance semigroupQuerySemigroup Query
124+
derive newtype instance monoidQueryMonoid Query
125+
126+
-- | The hash fragment of a URI.
127+
newtype Fragment = Fragment String
128+
129+
derive newtype instance eqFragmentEq Fragment
130+
derive newtype instance ordFragmentOrd Fragment
131+
derive instance genericFragmentGeneric Fragment _
132+
derive instance newtypeFragmentNewtype Fragment _
133+
instance showFragmentShow Fragment where show = genericShow

src/Data/URI/AbsoluteURI.purs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Data.URI.AbsoluteURI where
2+
3+
import Prelude
4+
5+
import Data.Array (catMaybes)
6+
import Data.Either (Either)
7+
import Data.Maybe (Maybe(..))
8+
import Data.String as S
9+
import Data.URI (AbsoluteURI(..))
10+
import Data.URI.HierarchicalPart as HPart
11+
import Data.URI.Query as Query
12+
import Data.URI.Scheme as Scheme
13+
import Text.Parsing.StringParser (ParseError, Parser, runParser)
14+
import Text.Parsing.StringParser.Combinators (optionMaybe)
15+
import Text.Parsing.StringParser.String (string, eof)
16+
17+
parse String Either ParseError AbsoluteURI
18+
parse = runParser parser
19+
20+
parser Parser AbsoluteURI
21+
parser = AbsoluteURI
22+
<$> (optionMaybe Scheme.parser <* string ":")
23+
<*> (string "//" *> HPart.parser)
24+
<*> optionMaybe (string "?" *> Query.parser)
25+
<* eof
26+
27+
print AbsoluteURI String
28+
print (AbsoluteURI s h q) =
29+
S.joinWith "" $ catMaybes
30+
[ (\scheme → Scheme.print scheme <> "//") <$> s
31+
, Just (HPart.print h)
32+
, Query.print <$> q
33+
]

src/Data/URI/Authority.purs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
1-
module Data.URI.Authority
2-
( module Data.URI.Authority
3-
, module Data.URI.Types
4-
) where
1+
module Data.URI.Authority where
52

63
import Prelude
74

85
import Data.Array (fromFoldable)
9-
import Data.Int (fromNumber)
10-
import Data.Maybe (Maybe(..), maybe)
6+
import Data.Maybe (maybe)
117
import Data.String as S
128
import Data.Tuple (Tuple(..))
13-
import Data.URI.Common (rxPat)
14-
import Data.URI.Host (printHost, parseHost)
15-
import Data.URI.Types (Authority(..), Host, Port, UserInfo)
16-
import Data.URI.UserInfo (parseUserInfo)
17-
18-
import Global (readInt)
19-
20-
import Text.Parsing.StringParser (Parser, fail)
9+
import Data.URI (Authority(..))
10+
import Data.URI.Host as Host
11+
import Data.URI.Port as Port
12+
import Data.URI.UserInfo as UserInfo
13+
import Text.Parsing.StringParser (Parser, try)
2114
import Text.Parsing.StringParser.Combinators (optionMaybe, sepBy)
2215
import Text.Parsing.StringParser.String (string)
2316

24-
parseAuthority Parser Authority
25-
parseAuthority = do
26-
ui ← optionMaybe parseUserInfo
17+
parser Parser Authority
18+
parser = do
19+
ui ← optionMaybe $ try (UserInfo.parser <* string "@")
2720
hosts ← flip sepBy (string ",") $
28-
Tuple <$> parseHost <*> optionMaybe (string ":" *> parsePort)
21+
Tuple <$> Host.parser <*> optionMaybe (string ":" *> Port.parser)
2922
pure $ Authority ui (fromFoldable hosts)
3023

31-
parsePort Parser Port
32-
parsePort = do
33-
s ← rxPat "[0-9]+"
34-
case fromNumber $ readInt 10 s of
35-
Just x → pure x
36-
_ → fail "Expected valid port number"
37-
38-
printAuthority Authority String
39-
printAuthority (Authority u hs) =
40-
"//" <> maybe "" (_ <> "@") u <> S.joinWith "," (printHostAndPort <$> hs)
24+
print Authority String
25+
print (Authority ui hs) =
26+
printUserInfo <> S.joinWith "," (printHostAndPort <$> hs)
4127
where
42-
printHostAndPort (Tuple h p) = printHost h <> maybe "" (\n → ":" <> show n) p
28+
printUserInfo =
29+
maybe "" (\u → UserInfo.print u <> "@") ui
30+
printHostAndPort (Tuple h p) =
31+
Host.print h <> maybe "" (\n → ":" <> Port.print n) p

0 commit comments

Comments
 (0)