17 import qualified Codec.Binary.Url as Url
19 import Control.Arrow.ArrowList
20 import qualified Data.ByteString as BS
21 import qualified Data.ByteString.Unsafe as BS
22 import qualified Data.ByteString.Lazy as LS
24 import qualified Data.Text as T
25 import Data.Text.Encoding
27 import Network.HTTP.Lucu
29 import Prelude.Unicode
30 import System.IO.Unsafe
32 yesOrNo ∷ Bool → String
36 trueOrFalse ∷ Bool → String
37 trueOrFalse True = "true"
38 trueOrFalse False = "false"
40 parseYesOrNo ∷ ArrowChoice a ⇒ a String Bool
42 = proc str → do case str of
43 "yes" → returnA ⤙ True
44 "no" → returnA ⤙ False
45 _ → returnA ⤙ error ("Expected yes or no: " ⧺ str)
47 maybeA ∷ (ArrowList a, ArrowChoice a) ⇒ a b c → a b (Maybe c)
51 [] → returnA ⤙ Nothing
52 (x:_) → returnA ⤙ Just x
54 deleteIfEmpty ∷ (ArrowList a, ArrowChoice a) ⇒ a String String
56 = proc str → do case str of
60 chomp ∷ String → String
62 chomp = reverse . snd . break (≢ '\n') . reverse
64 guessMIMEType ∷ LS.ByteString → MIMEType
65 {-# INLINEABLE guessMIMEType #-}
68 ∘ flip BS.unsafeUseAsCStringLen (magicCString magic)
73 {-# NOINLINE magic #-}
74 magic = unsafePerformIO
75 $ do m ← magicOpen [MagicMime]
79 isSafeChar ∷ Char → Bool
80 {-# INLINEABLE isSafeChar #-}
83 | isReserved c = False
84 | isUnreserved c = True
87 mkQueryString ∷ [(T.Text, T.Text)] → String
88 {-# INLINEABLE mkQueryString #-}
89 mkQueryString = intercalate ";" ∘ map pairToStr
91 pairToStr ∷ (T.Text, T.Text) → String
92 {-# INLINE pairToStr #-}
94 = encode k ⧺ ('=':encode v)
96 encode ∷ T.Text → String
98 encode = Url.encode ∘ BS.unpack ∘ encodeUtf8