From 13188fe64dea4798ae2dea071ba96ab5f3eeec9f Mon Sep 17 00:00:00 2001 From: q9i <46249765+quantum9Innovation@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:39:35 -0700 Subject: [PATCH] refactor: create getters module group helper funcs for creating directory names and fetching network resources into a single module --- app/Getters.hs | 17 +++++++++++++++++ app/Main.hs | 26 +++++++------------------- suntheme.cabal | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 app/Getters.hs diff --git a/app/Getters.hs b/app/Getters.hs new file mode 100644 index 0000000..96087d9 --- /dev/null +++ b/app/Getters.hs @@ -0,0 +1,17 @@ +module Getters where + +import Const (prog) + +import System.FilePath (()) +import System.Directory (XdgDirectory(XdgCache, XdgConfig), getXdgDirectory) +import Control.Exception (SomeException, try) +import Network.HTTP.Request (Response, get) + +pathToCache :: String -> IO String +pathToCache str = ( str) <$> getXdgDirectory XdgCache prog + +pathToConfig :: String -> IO String +pathToConfig str = ( str) <$> getXdgDirectory XdgConfig prog + +fetch :: String -> IO (Either SomeException Response) +fetch = try . get diff --git a/app/Main.hs b/app/Main.hs index 3c43d4c..1a1f823 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -7,16 +7,17 @@ module Main where import Pure (buildCmd, readLines) import Time (activateOnSunrise, sunriseNow, sunsetNow) -import Const (darkModeScript, lightModeScript, prog, query) +import Const (darkModeScript, lightModeScript, query) import Sugar (continue, crash, destruct, exec, killall) -import Types (ResponseMsg(..), ResponseCode(..), genericErr, toResponseMsg) +import Types (ResponseCode(..), ResponseMsg(..), genericErr, toResponseMsg) +import Getters (fetch, pathToCache, pathToConfig) import Data.List.Extra ((!?)) import Data.ByteString.Char8 (unpack) -import System.FilePath ((), takeDirectory) -import System.Directory (XdgDirectory(XdgCache, XdgConfig), createDirectoryIfMissing, doesFileExist, getXdgDirectory) -import Control.Exception (SomeException, catch, try) -import Network.HTTP.Request (Response(responseBody, responseStatus), get) +import System.FilePath (takeDirectory) +import System.Directory (createDirectoryIfMissing, doesFileExist) +import Control.Exception (SomeException, catch) +import Network.HTTP.Request (Response(responseBody, responseStatus)) -- error handling -- refactor into modules @@ -25,19 +26,6 @@ import Network.HTTP.Request (Response(responseBody, responseStatus), get) -- introduce liquid types and checking -- whitepaper! -pathToCache :: String -> IO String -pathToCache str = do - dir <- getXdgDirectory XdgCache prog - return (dir str) - -pathToConfig :: String -> IO String -pathToConfig str = do - dir <- getXdgDirectory XdgConfig prog - return (dir str) - -fetch :: String -> IO (Either SomeException Response) -fetch = try . get - ping :: (Response -> IO ()) -> IO () -> IO () ping run err = do res <- fetch query diff --git a/suntheme.cabal b/suntheme.cabal index 0c421aa..9499d09 100644 --- a/suntheme.cabal +++ b/suntheme.cabal @@ -62,7 +62,7 @@ executable suntheme main-is: Main.hs -- Modules included in this executable, other than Main. - other-modules: Types, Const, Pure, Time, Sugar + other-modules: Types, Const, Pure, Time, Sugar, Getters -- LANGUAGE extensions used by modules in this package. -- other-extensions: