Skip to content

Commit c9ddc05

Browse files
Merge pull request #178 from meiersi/fix-method-ord-instance
Fix #117: compatible Eq and Ord instances for Method.
2 parents d025f43 + e79225f commit c9ddc05

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

src/Snap/Internal/Http/Types.hs

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,69 @@ deleteHeader k = updateHeaders $ H.delete k
133133
-- <http://tools.ietf.org/html/rfc2068.html#section-5.1.1>).
134134
data Method = GET | HEAD | POST | PUT | DELETE | TRACE | OPTIONS | CONNECT |
135135
PATCH | Method ByteString
136-
deriving(Show,Read,Ord)
137-
136+
deriving(Show, Read)
138137

139138
instance Eq Method where
140-
GET == GET = True
141-
GET == Method "GET" = True
142-
HEAD == HEAD = True
143-
HEAD == Method "HEAD" = True
144-
POST == POST = True
145-
POST == Method "POST" = True
146-
PUT == PUT = True
147-
PUT == Method "PUT" = True
148-
DELETE == DELETE = True
149-
DELETE == Method "DELETE" = True
150-
TRACE == TRACE = True
151-
TRACE == Method "TRACE" = True
152-
OPTIONS == OPTIONS = True
153-
OPTIONS == Method "OPTIONS" = True
154-
CONNECT == CONNECT = True
155-
CONNECT == Method "CONNECT" = True
156-
PATCH == PATCH = True
157-
PATCH == Method "PATCH" = True
158-
Method a == Method b = a == b
159-
m@(Method _) == other = other == m
160-
_ == _ = False
139+
a == b =
140+
normalizeMethod a `eq` normalizeMethod b
141+
where
142+
GET `eq` GET = True
143+
HEAD `eq` HEAD = True
144+
POST `eq` POST = True
145+
PUT `eq` PUT = True
146+
DELETE `eq` DELETE = True
147+
TRACE `eq` TRACE = True
148+
OPTIONS `eq` OPTIONS = True
149+
CONNECT `eq` CONNECT = True
150+
PATCH `eq` PATCH = True
151+
Method x1 `eq` Method y1 = x1 == y1
152+
_ `eq` _ = False
153+
154+
instance Ord Method where
155+
compare a b =
156+
check (normalizeMethod a) (normalizeMethod b)
157+
where
158+
check GET GET = EQ
159+
check HEAD HEAD = EQ
160+
check POST POST = EQ
161+
check PUT PUT = EQ
162+
check DELETE DELETE = EQ
163+
check TRACE TRACE = EQ
164+
check OPTIONS OPTIONS = EQ
165+
check CONNECT CONNECT = EQ
166+
check PATCH PATCH = EQ
167+
check (Method x1) (Method y1) = compare x1 y1
168+
check x y = compare (tag x) (tag y)
169+
170+
tag :: Method -> Int
171+
tag (GET{}) = 0
172+
tag (HEAD{}) = 1
173+
tag (POST{}) = 2
174+
tag (PUT{}) = 3
175+
tag (DELETE{}) = 4
176+
tag (TRACE{}) = 5
177+
tag (OPTIONS{}) = 6
178+
tag (CONNECT{}) = 7
179+
tag (PATCH{}) = 8
180+
tag (Method{}) = 9
181+
182+
-- | Equate the special case constructors with their corresponding
183+
-- @Method name@ variant.
184+
{-# INLINE normalizeMethod #-}
185+
normalizeMethod :: Method -> Method
186+
normalizeMethod m@(Method name) = case name of
187+
"GET" -> GET
188+
"HEAD" -> HEAD
189+
"POST" -> POST
190+
"PUT" -> PUT
191+
"DELETE" -> DELETE
192+
"TRACE" -> TRACE
193+
"OPTIONS" -> OPTIONS
194+
"CONNECT" -> CONNECT
195+
"PATCH" -> PATCH
196+
_ -> m
197+
normalizeMethod m = m
198+
161199

162200
------------------------------------------------------------------------------
163201
type HttpVersion = (Int,Int)

0 commit comments

Comments
 (0)