From a5d0c25fc331ede1b87c31813ec8973098e4b2db Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sun, 24 Oct 2021 18:54:41 -0400 Subject: [PATCH] make gemQuery field of GemURL optional (with Maybe) --- src/Network/GemServ.hs | 4 ++-- src/Network/GemServ/Types.hs | 6 +++--- test/Network/GemServSpec.hs | 7 +++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Network/GemServ.hs b/src/Network/GemServ.hs index 0432e23..26b8979 100644 --- a/src/Network/GemServ.hs +++ b/src/Network/GemServ.hs @@ -53,8 +53,8 @@ encodeGemURL url = Nothing -> "" path = intercalate "/" $ map escapeString $ gemPath url query = case gemQuery url of - "" -> "" - q -> '?' : escapeString q + Nothing -> "" + Just q -> '?' : escapeString q -- | add required escape sequences to a string escapeString :: String -> String diff --git a/src/Network/GemServ/Types.hs b/src/Network/GemServ/Types.hs index 32dac17..75a2a74 100644 --- a/src/Network/GemServ/Types.hs +++ b/src/Network/GemServ/Types.hs @@ -39,8 +39,8 @@ data GemURL = GemURL -- ^ The port number (if supplied) , gemPath :: [String] -- ^ The decoded path segments - , gemQuery :: String - -- ^ The decoded request query + , gemQuery :: Maybe String + -- ^ The decoded request query (if supplied) } deriving (Eq, Show) -- | Builds a new 'GemURL' @@ -52,7 +52,7 @@ newGemURL host = GemURL { gemHost = host , gemPort = Nothing , gemPath = [] - , gemQuery = "" + , gemQuery = Nothing } --jl diff --git a/test/Network/GemServSpec.hs b/test/Network/GemServSpec.hs index a20ad60..16f126f 100644 --- a/test/Network/GemServSpec.hs +++ b/test/Network/GemServSpec.hs @@ -45,6 +45,7 @@ encodeGemURLSpec = describe "encodeGemURL" $ mapM_ , ( "with port", withPortURL, withPortExp ) , ( "with path", withPathURL, withPathExp ) , ( "with query", withQueryURL, withQueryExp ) + , ( "blank query", blankQueryURL, blankQueryExp ) , ( "with escape", withEscapeURL, withEscapeExp ) ] @@ -55,12 +56,14 @@ encodeGemURLSpec = describe "encodeGemURL" $ mapM_ withPortExp = "gemini://example.com:1965/" withPathURL = simpleURL { gemPath = ["foo", "bar"] } withPathExp = "gemini://example.com/foo/bar" - withQueryURL = simpleURL { gemQuery = "foo" } + withQueryURL = simpleURL { gemQuery = Just "foo" } withQueryExp = "gemini://example.com/?foo" + blankQueryURL = simpleURL { gemQuery = Just "" } + blankQueryExp = "gemini://example.com/?" withEscapeURL = simpleURL { gemPath = ["foo bar"] - , gemQuery = "baz quux" + , gemQuery = Just "baz quux" } withEscapeExp = "gemini://example.com/foo%20bar?baz%20quux"