-mimeTypeP ∷ Parser MIMEType
-mimeTypeP = try $
- do maj ← A.toCIAscii <$> token
- _ ← char '/'
- min ← A.toCIAscii <$> token
- params ← P.many paramP
- return $ MIMEType maj min params
- where
- paramP ∷ Parser (CIAscii, Ascii)
- paramP = try $
- do skipMany lws
- _ ← char ';'
- skipMany lws
- name ← A.toCIAscii <$> token
- _ ← char '='
- value ← token <|> quotedStr
- return (name, value)
+instance ConvertSuccess MIMEType AsciiBuilder where
+ {-# INLINEABLE convertSuccess #-}
+ convertSuccess (MIMEType {..})
+ = cs mtMedia ⊕
+ cs ("/" ∷ Ascii) ⊕
+ cs mtSub ⊕
+ cs mtParams
+
+deriveAttempts [ ([t| MIMEType |], [t| Ascii |])
+ , ([t| MIMEType |], [t| AsciiBuilder |])
+ ]
+
+-- |Parse 'MIMEType' from an 'Ascii'. For MIME type literals, consider
+-- using 'Network.HTTP.Lucu.MIMEType.TH.mimeType'.
+instance ConvertAttempt Ascii MIMEType where
+ {-# INLINEABLE convertAttempt #-}
+ convertAttempt str
+ = case parseOnly (finishOff mimeType) (cs str) of
+ Right t → return t
+ Left err → fail ("Unparsable MIME Type: " ⧺ cs str ⧺ ": " ⧺ err)
+
+-- |'Parser' for an 'MIMEType'.
+mimeType ∷ Parser MIMEType
+{-# INLINEABLE mimeType #-}
+mimeType = do media ← cs <$> token
+ _ ← char '/'
+ sub ← cs <$> token
+ params ← mimeParams
+ return $ MIMEType media sub params