Debian Codegraph @

src/Network/HTTP/Media/Quality.hs

raw
1 {-# LANGUAGE DeriveFunctor #-}
2
3 -- | Defines the quality value data type.
4 module Network.HTTP.Media.Quality
5 ( Quality (..),
6 quality,
7 QualityOrder,
8 qualityOrder,
9 isAcceptable,
10 maxQuality,
11 minQuality,
12 mostSpecific,
13 showQ,
14 readQ,
15 )
16 where
17
18 import Data.ByteString (ByteString)
19 import qualified Data.ByteString.Char8 as BS
20 import Data.ByteString.UTF8 (toString)
21 import Data.Char (isDigit)
22 import Data.List (dropWhileEnd)
23 import Data.Maybe (fromMaybe)
24 import Data.Monoid ((<>))
25 import Data.Word (Word16, Word32)
26 import Network.HTTP.Media.Accept (Accept, moreSpecificThan)
27 import Network.HTTP.Media.RenderHeader (RenderHeader (..))
28 import Prelude hiding ((<>))
29
30 -- | Attaches a quality value to data.
31 data Quality a = Quality
32 { qualityData :: a,
33 qualityValue :: Word16
34 }
35 deriving (Eq, Functor, Ord)
36
37 instance (RenderHeader a) => Show (Quality a) where
38 show = BS.unpack . renderHeader
39
40 instance (RenderHeader h) => RenderHeader (Quality h) where
41 renderHeader (Quality a q) = renderHeader a <> ";q=" <> showQ q
42
43 -- | Manually construct a quality value.
44 quality :: a -> ByteString -> Quality a
45 quality x q =
46 Quality x $
47 flip fromMaybe (readQ q) $
48 error ("Invalid quality value " ++ toString q)
49
50 -- | An opaque ordered representation of quality values without attached data.
51 newtype QualityOrder = QualityOrder Word16
52 deriving (Eq, Ord)
53
54 -- | Whether the quality value is greater than zero; otherwise the value
55 -- should never be accepted, even when no other options are available.
56 isAcceptable :: Quality a -> Bool
57 isAcceptable (Quality _ 0) = False
58 isAcceptable (Quality _ _) = True
59
60 -- | Remove the attached data from a quality value, retaining only the
61 -- priority of the quality parameter.
62 qualityOrder :: Quality a -> QualityOrder
63 qualityOrder = QualityOrder . qualityValue
64
65 -- | Attaches the quality value '1'.
66 maxQuality :: a -> Quality a
67 maxQuality = flip Quality 1000
68
69 -- | Attaches the quality value '0'.
70 minQuality :: a -> Quality a
71 minQuality = flip Quality 0
72
73 -- | Combines quality values by specificity. Selects the more specific of the
74 -- two arguments, but if they are the same returns the data of the left
75 -- argument with the two quality values of both arguments combined.
76 mostSpecific :: (Accept a) => Quality a -> Quality a -> Quality a
77 mostSpecific (Quality a q) (Quality b r)
78 | a `moreSpecificThan` b = Quality a q
79 | b `moreSpecificThan` a = Quality b r
80 | otherwise = Quality a q'
81 where
82 q' = fromIntegral (fromIntegral q * fromIntegral r `div` 1000 :: Word32)
83
84 -- | Converts the integral value into its standard quality representation.
85 showQ :: Word16 -> ByteString
86 showQ 1000 = "1"
87 showQ 0 = "0"
88 showQ q = "0." <> BS.replicate (3 - length s) '0' <> b
89 where
90 s = show q
91 b = BS.pack (dropWhileEnd (== '0') s)
92
93 -- | Reads the standard quality representation into an integral value.
94 readQ :: ByteString -> Maybe Word16
95 readQ bs
96 | BS.null bs = Nothing
97 | h == '1' = read1 t
98 | h == '0' = read0 t
99 | otherwise = Nothing
100 where
101 h = BS.head bs
102 t = BS.tail bs
103
104 read1 :: ByteString -> Maybe Word16
105 read1 bs
106 | BS.null bs || h == '.' && BS.length t < 4 && BS.all (== '0') t =
107 Just 1000
108 | otherwise = Nothing
109 where
110 h = BS.head bs
111 t = BS.tail bs
112
113 read0 :: ByteString -> Maybe Word16
114 read0 bs
115 | BS.null bs = Just 0
116 | h == '.' && BS.length t < 4 && BS.all isDigit t =
117 Just (toWord (t <> BS.replicate (3 - BS.length t) '0'))
118 | otherwise = Nothing
119 where
120 h = BS.head bs
121 t = BS.tail bs
122 toWord = read . BS.unpack