module Rakka.Utils ( yesOrNo , parseYesOrNo , maybeA , deleteIfEmpty , chomp , guessMIMEType ) where import Control.Arrow import Control.Arrow.ArrowList import qualified Data.ByteString.Lazy as Lazy (ByteString) import qualified Data.ByteString.Lazy.Char8 as L8 hiding (ByteString) import Magic import Network.HTTP.Lucu import System.IO.Unsafe yesOrNo :: Bool -> String yesOrNo True = "yes" yesOrNo False = "no" parseYesOrNo :: ArrowChoice a => a String Bool parseYesOrNo = proc str -> do case str of "yes" -> returnA -< True "no" -> returnA -< False _ -> returnA -< error ("Expected yes or no: " ++ str) maybeA :: (ArrowList a, ArrowChoice a) => a b c -> a b (Maybe c) maybeA a = listA a >>> proc xs -> case xs of [] -> returnA -< Nothing (x:_) -> returnA -< Just x deleteIfEmpty :: (ArrowList a, ArrowChoice a) => a String String deleteIfEmpty = proc str -> do case str of "" -> none -< () _ -> returnA -< str chomp :: String -> String chomp = reverse . snd . break (/= '\n') . reverse guessMIMEType :: Lazy.ByteString -> MIMEType guessMIMEType = read . unsafePerformIO . magicString magic . L8.unpack where magic :: Magic magic = unsafePerformIO $ do m <- magicOpen [MagicMime] magicLoadDefault m return m