2022-07-01 23:15:44 -04:00
|
|
|
{-# LANGUAGE DeriveFunctor #-}
|
2022-07-04 12:30:13 -04:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2022-07-01 23:15:44 -04:00
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TupleSections #-}
|
2021-11-21 22:47:43 -05:00
|
|
|
|
2021-11-07 13:35:08 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Functions for handling dependencies
|
|
|
|
|
2022-07-09 17:44:14 -04:00
|
|
|
module Data.Internal.Dependency
|
2022-06-26 19:05:25 -04:00
|
|
|
-- feature types
|
|
|
|
( Feature
|
2022-06-21 00:56:42 -04:00
|
|
|
, Always(..)
|
2022-06-28 23:27:55 -04:00
|
|
|
, Always_(..)
|
2022-07-02 00:09:16 -04:00
|
|
|
, FallbackRoot(..)
|
|
|
|
, FallbackStack(..)
|
2022-06-28 23:27:55 -04:00
|
|
|
, Sometimes(..)
|
|
|
|
, Sometimes_
|
2022-06-26 19:05:25 -04:00
|
|
|
, AlwaysX
|
|
|
|
, AlwaysIO
|
2022-06-21 00:56:42 -04:00
|
|
|
, SometimesX
|
|
|
|
, SometimesIO
|
2022-06-26 19:05:25 -04:00
|
|
|
, PostPass(..)
|
|
|
|
, Subfeature(..)
|
2022-06-26 19:27:04 -04:00
|
|
|
, SubfeatureRoot
|
2022-07-07 01:05:17 -04:00
|
|
|
, Msg(..)
|
2022-07-07 18:40:42 -04:00
|
|
|
, LogLevel(..)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-08 18:57:12 -04:00
|
|
|
-- configuration
|
|
|
|
, XParams(..)
|
|
|
|
, XPFeatures(..)
|
2022-07-08 20:01:35 -04:00
|
|
|
, XPQuery
|
2022-07-08 18:57:12 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- dependency tree types
|
|
|
|
, Root(..)
|
|
|
|
, Tree(..)
|
|
|
|
, Tree_(..)
|
2022-07-02 17:09:21 -04:00
|
|
|
, IOTree
|
|
|
|
, IOTree_
|
|
|
|
, DBusTree
|
|
|
|
, DBusTree_
|
2022-07-09 17:08:10 -04:00
|
|
|
, SafeClient(..)
|
2022-06-26 19:05:25 -04:00
|
|
|
, IODependency(..)
|
|
|
|
, IODependency_(..)
|
|
|
|
, SystemDependency(..)
|
|
|
|
, DBusDependency_(..)
|
|
|
|
, DBusMember(..)
|
|
|
|
, UnitType(..)
|
|
|
|
, Result
|
2022-07-09 01:02:37 -04:00
|
|
|
, Fulfillment(..)
|
2022-07-09 14:59:42 -04:00
|
|
|
, ArchPkg(..)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
-- dumping
|
|
|
|
, dumpFeature
|
|
|
|
, dumpAlways
|
|
|
|
, dumpSometimes
|
|
|
|
, jsonArray
|
|
|
|
, JSONQuotable(..)
|
|
|
|
, JSONUnquotable(..)
|
|
|
|
, JSONMixed(..)
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- testing
|
2022-07-04 12:30:13 -04:00
|
|
|
, FIO
|
|
|
|
, withCache
|
2022-06-22 01:28:46 -04:00
|
|
|
, evalFeature
|
2022-06-21 00:56:42 -04:00
|
|
|
, executeSometimes
|
|
|
|
, executeAlways
|
|
|
|
, evalAlways
|
|
|
|
, evalSometimes
|
2022-07-06 18:54:10 -04:00
|
|
|
, fontTreeAlt
|
|
|
|
, fontTree
|
|
|
|
, fontTree_
|
|
|
|
, fontAlways
|
|
|
|
, fontSometimes
|
2022-07-07 19:20:21 -04:00
|
|
|
, readEthernet
|
|
|
|
, readWireless
|
2022-07-08 00:21:05 -04:00
|
|
|
, socketExists
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- lifting
|
|
|
|
, ioSometimes
|
|
|
|
, ioAlways
|
2022-06-21 00:56:42 -04:00
|
|
|
|
|
|
|
-- feature construction
|
2022-06-26 20:07:25 -04:00
|
|
|
, always1
|
2022-06-21 00:56:42 -04:00
|
|
|
, sometimes1
|
|
|
|
, sometimesIO
|
2022-07-02 17:09:21 -04:00
|
|
|
, sometimesIO_
|
2022-06-21 00:56:42 -04:00
|
|
|
, sometimesDBus
|
|
|
|
, sometimesExe
|
|
|
|
, sometimesExeArgs
|
|
|
|
, sometimesEndpoint
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- dependency construction
|
|
|
|
, sysExe
|
|
|
|
, localExe
|
|
|
|
, sysdSystem
|
|
|
|
, sysdUser
|
2022-06-21 00:56:42 -04:00
|
|
|
, listToAnds
|
2022-07-06 18:54:10 -04:00
|
|
|
, toAnd_
|
|
|
|
, toFallback
|
2021-11-07 18:41:25 -05:00
|
|
|
, pathR
|
|
|
|
, pathRW
|
2022-06-21 00:56:42 -04:00
|
|
|
, pathW
|
2022-07-04 00:36:41 -04:00
|
|
|
, voidResult
|
|
|
|
, voidRead
|
2022-07-08 18:17:41 -04:00
|
|
|
, process
|
2022-07-03 18:23:32 -04:00
|
|
|
|
|
|
|
-- misc
|
|
|
|
, shellTest
|
2021-11-07 13:35:08 -05:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class
|
2021-11-22 23:02:23 -05:00
|
|
|
import Control.Monad.Identity
|
2022-07-07 18:40:42 -04:00
|
|
|
import Control.Monad.Reader
|
2022-07-04 12:30:13 -04:00
|
|
|
import Control.Monad.State
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-07-07 18:40:42 -04:00
|
|
|
import Data.Aeson hiding (Error, Result)
|
|
|
|
import Data.Aeson.Key
|
2022-06-21 00:56:42 -04:00
|
|
|
import Data.Bifunctor
|
2022-07-07 19:20:21 -04:00
|
|
|
import Data.Either
|
2022-07-04 12:30:13 -04:00
|
|
|
import qualified Data.HashMap.Strict as H
|
|
|
|
import Data.Hashable
|
2022-07-09 17:44:14 -04:00
|
|
|
import Data.Internal.DBus
|
2022-06-28 21:24:21 -04:00
|
|
|
import Data.List
|
2022-06-17 00:37:12 -04:00
|
|
|
import Data.Maybe
|
2022-07-07 18:40:42 -04:00
|
|
|
import Data.Yaml
|
2021-11-07 20:16:53 -05:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
import GHC.Generics (Generic)
|
2022-07-08 02:11:42 -04:00
|
|
|
import GHC.IO.Exception (ioe_description)
|
2022-07-04 12:30:13 -04:00
|
|
|
|
2022-07-06 00:16:47 -04:00
|
|
|
import DBus hiding (typeOf)
|
2021-11-27 01:02:22 -05:00
|
|
|
import qualified DBus.Introspection as I
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-07-07 18:40:42 -04:00
|
|
|
import System.Directory
|
2022-06-26 19:05:25 -04:00
|
|
|
import System.Environment
|
2021-11-07 13:35:08 -05:00
|
|
|
import System.Exit
|
2022-07-07 18:40:42 -04:00
|
|
|
import System.FilePath
|
2022-07-07 19:20:21 -04:00
|
|
|
import System.IO.Error
|
2022-07-07 18:40:42 -04:00
|
|
|
import System.Posix.Files
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-27 01:02:22 -05:00
|
|
|
import XMonad.Core (X, io)
|
2021-11-07 18:41:25 -05:00
|
|
|
import XMonad.Internal.IO
|
2021-11-07 13:35:08 -05:00
|
|
|
import XMonad.Internal.Process
|
|
|
|
import XMonad.Internal.Shell
|
2022-07-06 18:54:10 -04:00
|
|
|
import XMonad.Internal.Theme
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Feature Evaluation
|
|
|
|
--
|
|
|
|
-- Here we attempt to build and return the monadic actions encoded by each
|
|
|
|
-- feature.
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
-- | Run feature evaluation(s) with the cache
|
|
|
|
-- Currently there is no easy way to not use this (oh well)
|
|
|
|
withCache :: FIO a -> IO a
|
2022-07-07 18:40:42 -04:00
|
|
|
withCache x = do
|
|
|
|
p <- getParams
|
|
|
|
evalStateT (runReaderT x p) emptyCache
|
2022-07-04 12:30:13 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Execute an Always immediately
|
2022-07-04 12:30:13 -04:00
|
|
|
executeAlways :: Always (IO a) -> FIO a
|
|
|
|
executeAlways = io <=< evalAlways
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Execute a Sometimes immediately (or do nothing if failure)
|
2022-07-04 12:30:13 -04:00
|
|
|
executeSometimes :: Sometimes (IO a) -> FIO (Maybe a)
|
|
|
|
executeSometimes a = maybe (return Nothing) (io . fmap Just) =<< evalSometimes a
|
2022-06-22 01:28:46 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Possibly return the action of an Always/Sometimes
|
2022-07-04 12:30:13 -04:00
|
|
|
evalFeature :: Feature a -> FIO (Maybe a)
|
2022-06-26 19:05:25 -04:00
|
|
|
evalFeature (Right a) = Just <$> evalAlways a
|
|
|
|
evalFeature (Left s) = evalSometimes s
|
2022-06-22 01:28:46 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Possibly return the action of a Sometimes
|
2022-07-04 12:30:13 -04:00
|
|
|
evalSometimes :: Sometimes a -> FIO (Maybe a)
|
|
|
|
evalSometimes x = either goFail goPass =<< evalSometimesMsg x
|
2022-06-26 19:05:25 -04:00
|
|
|
where
|
2022-07-07 01:05:17 -04:00
|
|
|
goPass (a, ws) = putErrors ws >> return (Just a)
|
2022-06-26 19:05:25 -04:00
|
|
|
goFail es = putErrors es >> return Nothing
|
2022-07-07 19:20:21 -04:00
|
|
|
putErrors = mapM_ printMsg
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Return the action of an Always
|
2022-07-04 12:30:13 -04:00
|
|
|
evalAlways :: Always a -> FIO a
|
2022-06-26 19:05:25 -04:00
|
|
|
evalAlways a = do
|
2022-07-07 01:05:17 -04:00
|
|
|
(x, ws) <- evalAlwaysMsg a
|
2022-07-07 19:20:21 -04:00
|
|
|
mapM_ printMsg ws
|
2022-06-26 19:05:25 -04:00
|
|
|
return x
|
2022-06-16 18:50:24 -04:00
|
|
|
|
2022-07-07 19:20:21 -04:00
|
|
|
printMsg :: FMsg -> FIO ()
|
2022-07-07 01:05:17 -04:00
|
|
|
printMsg (FMsg fn n (Msg ll m)) = do
|
2022-07-07 19:20:21 -04:00
|
|
|
xl <- asks xpLogLevel
|
|
|
|
p <- io getProgName
|
2022-07-08 20:01:35 -04:00
|
|
|
io $ when (ll <= xl) $ putStrLn $ unwords $ s p
|
|
|
|
where
|
|
|
|
s p = [ bracket p
|
|
|
|
, bracket $ show ll
|
|
|
|
, bracket fn
|
|
|
|
]
|
|
|
|
++ maybe [] ((:[]) . bracket) n
|
|
|
|
++ [m]
|
2022-07-07 01:05:17 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Feature status
|
2021-11-21 22:47:43 -05:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
-- | Dump the status of a Feature
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpFeature :: Feature a -> FIO JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
dumpFeature = either dumpSometimes dumpAlways
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Dump the status of an Always to stdout
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpAlways :: Always a -> FIO JSONUnquotable
|
2022-06-28 23:27:55 -04:00
|
|
|
dumpAlways (Always n x) = go [] x
|
2022-06-28 21:24:21 -04:00
|
|
|
where
|
|
|
|
go failed (Option o os) = do
|
|
|
|
(s, r) <- dumpSubfeatureRoot o
|
|
|
|
if r
|
2022-06-28 23:27:55 -04:00
|
|
|
then return $ jsonAlways (Q n) (Just s) failed $ untested [] os
|
2022-06-28 21:24:21 -04:00
|
|
|
else go (s:failed) os
|
2022-06-28 23:27:55 -04:00
|
|
|
go failed (Always_ _) = return $ jsonAlways (Q n) (Just (UQ "true")) failed []
|
|
|
|
untested acc (Always_ _) = acc
|
2022-06-28 21:24:21 -04:00
|
|
|
untested acc (Option o os) = untested (dataSubfeatureRoot o:acc) os
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Dump the status of a Sometimes to stdout
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpSometimes :: Sometimes a -> FIO JSONUnquotable
|
2022-07-08 18:57:12 -04:00
|
|
|
dumpSometimes (Sometimes n _ a) = go [] a
|
2022-06-28 21:24:21 -04:00
|
|
|
where
|
2022-06-28 23:27:55 -04:00
|
|
|
go failed [] = return $ jsonSometimes (Q n) Nothing failed []
|
2022-06-28 21:24:21 -04:00
|
|
|
go failed (x:xs) = do
|
|
|
|
(s, r) <- dumpSubfeatureRoot x
|
|
|
|
if r
|
2022-06-28 23:27:55 -04:00
|
|
|
then return $ jsonSometimes (Q n) (Just s) failed $ fmap dataSubfeatureRoot xs
|
2022-06-28 21:24:21 -04:00
|
|
|
else go (s:failed) xs
|
2021-11-11 00:11:15 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Wrapper types
|
2021-11-20 01:15:04 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
type AlwaysX = Always (X ())
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
type AlwaysIO = Always (IO ())
|
2021-11-21 18:18:09 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
type SometimesX = Sometimes (X ())
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
type SometimesIO = Sometimes (IO ())
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
type Feature a = Either (Sometimes a) (Always a)
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Feature declaration
|
|
|
|
|
|
|
|
-- | Feature that is guaranteed to work
|
|
|
|
-- This is composed of sub-features that are tested in order, and if all fail
|
|
|
|
-- the fallback is a monadic action (eg a plain haskell function)
|
2022-06-28 23:27:55 -04:00
|
|
|
data Always a = Always String (Always_ a)
|
|
|
|
|
|
|
|
-- | Feature that is guaranteed to work (inner data)
|
2022-07-02 17:09:21 -04:00
|
|
|
data Always_ a = Option (SubfeatureRoot a) (Always_ a)
|
|
|
|
| Always_ (FallbackRoot a)
|
2022-07-02 00:09:16 -04:00
|
|
|
|
|
|
|
-- | Root of a fallback action for an always
|
|
|
|
-- This may either be a lone action or a function that depends on the results
|
|
|
|
-- from other Always features.
|
2022-07-02 17:09:21 -04:00
|
|
|
data FallbackRoot a = FallbackAlone a
|
|
|
|
| forall p. FallbackTree (p -> a) (FallbackStack p)
|
2022-07-02 00:09:16 -04:00
|
|
|
|
|
|
|
-- | Always features that are used as a payload for a fallback action
|
|
|
|
data FallbackStack p = FallbackBottom (Always p)
|
|
|
|
| forall x y. FallbackStack (x -> y -> p) (Always x) (FallbackStack y)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
-- | Feature that might not be present
|
|
|
|
-- This is like an Always except it doesn't fall back on a guaranteed monadic
|
|
|
|
-- action
|
2022-07-08 20:01:35 -04:00
|
|
|
data Sometimes a = Sometimes String XPQuery (Sometimes_ a)
|
2022-06-28 23:27:55 -04:00
|
|
|
|
|
|
|
-- | Feature that might not be present (inner data)
|
|
|
|
type Sometimes_ a = [SubfeatureRoot a]
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
-- | Individually tested sub-feature data for Always/sometimes
|
|
|
|
-- The polymorphism allows representing tested and untested states. Includes
|
|
|
|
-- the 'action' itself to be tested and any auxilary data for describing the
|
|
|
|
-- sub-feature.
|
|
|
|
data Subfeature f = Subfeature
|
2022-07-08 19:02:49 -04:00
|
|
|
{ sfData :: f
|
|
|
|
, sfName :: String
|
2022-06-21 00:56:42 -04:00
|
|
|
}
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Loglevel at which feature testing should be reported
|
|
|
|
-- This is currently not used for anything important
|
2022-07-09 19:55:25 -04:00
|
|
|
data LogLevel = Silent | Error | Warn | Debug | Info
|
|
|
|
deriving (Eq, Show, Ord, Generic)
|
2022-07-07 18:40:42 -04:00
|
|
|
|
|
|
|
type SubfeatureRoot a = Subfeature (Root a)
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | An action and its dependencies
|
|
|
|
-- May be a plain old monad or be DBus-dependent, in which case a client is
|
|
|
|
-- needed
|
2022-07-02 17:09:21 -04:00
|
|
|
data Root a = forall p. IORoot (p -> a) (IOTree p)
|
|
|
|
| IORoot_ a IOTree_
|
2022-07-09 17:08:10 -04:00
|
|
|
| forall c p. SafeClient c => DBusRoot (p -> c -> a) (DBusTree c p) (Maybe c)
|
|
|
|
| forall c. SafeClient c => DBusRoot_ (c -> a) (DBusTree_ c) (Maybe c)
|
|
|
|
|
2022-07-02 17:09:21 -04:00
|
|
|
-- | The dependency tree with rule to merge results when needed
|
2022-06-26 19:05:25 -04:00
|
|
|
data Tree d d_ p =
|
2022-07-01 23:15:44 -04:00
|
|
|
forall x y. And12 (x -> y -> p) (Tree d d_ x) (Tree d d_ y)
|
2022-06-26 20:48:26 -04:00
|
|
|
| And1 (Tree d d_ p) (Tree_ d_)
|
|
|
|
| And2 (Tree_ d_) (Tree d d_ p)
|
|
|
|
| Or (Tree d d_ p) (Tree d d_ p)
|
2022-06-26 19:05:25 -04:00
|
|
|
| Only (d p)
|
|
|
|
|
|
|
|
-- | A dependency tree without functions to merge results
|
2022-07-02 17:09:21 -04:00
|
|
|
data Tree_ d = And_ (Tree_ d) (Tree_ d) | Or_ (Tree_ d) (Tree_ d) | Only_ d
|
|
|
|
|
|
|
|
-- | Shorthand tree types for lazy typers
|
|
|
|
type IOTree p = Tree IODependency IODependency_ p
|
2022-07-09 17:08:10 -04:00
|
|
|
type DBusTree c p = Tree IODependency (DBusDependency_ c) p
|
2022-07-02 17:09:21 -04:00
|
|
|
type IOTree_ = Tree_ IODependency_
|
2022-07-09 17:08:10 -04:00
|
|
|
type DBusTree_ c = Tree_ (DBusDependency_ c)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-03 18:23:32 -04:00
|
|
|
-- | A dependency that only requires IO to evaluate (with payload)
|
|
|
|
data IODependency p =
|
2022-07-06 18:54:10 -04:00
|
|
|
-- a cachable IO action that yields a payload
|
2022-07-09 01:02:37 -04:00
|
|
|
IORead String [Fulfillment] (FIO (Result p))
|
2022-07-03 18:23:32 -04:00
|
|
|
-- always yields a payload
|
|
|
|
| IOConst p
|
|
|
|
-- an always that yields a payload
|
2022-06-26 19:05:25 -04:00
|
|
|
| forall a. IOAlways (Always a) (a -> p)
|
2022-07-03 18:23:32 -04:00
|
|
|
-- a sometimes that yields a payload
|
2022-06-26 19:05:25 -04:00
|
|
|
| forall a. IOSometimes (Sometimes a) (a -> p)
|
|
|
|
|
|
|
|
-- | A dependency pertaining to the DBus
|
2022-07-09 17:08:10 -04:00
|
|
|
data DBusDependency_ c = Bus [Fulfillment] BusName
|
2022-07-09 01:02:37 -04:00
|
|
|
| Endpoint [Fulfillment] BusName ObjectPath InterfaceName DBusMember
|
2022-06-26 19:05:25 -04:00
|
|
|
| DBusIO IODependency_
|
2022-07-06 00:16:47 -04:00
|
|
|
deriving (Eq, Generic)
|
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
instance Hashable (DBusDependency_ c) where
|
2022-07-09 01:02:37 -04:00
|
|
|
hashWithSalt s (Bus f b) = s `hashWithSalt` f
|
|
|
|
`hashWithSalt` formatBusName b
|
|
|
|
hashWithSalt s (Endpoint f b o i m) = s `hashWithSalt` f
|
|
|
|
`hashWithSalt` formatBusName b
|
|
|
|
`hashWithSalt` formatObjectPath o
|
|
|
|
`hashWithSalt` formatInterfaceName i
|
|
|
|
`hashWithSalt` m
|
|
|
|
hashWithSalt s (DBusIO i) = hashWithSalt s i
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | A dependency that only requires IO to evaluate (no payload)
|
2022-07-09 01:02:37 -04:00
|
|
|
data IODependency_ = IOSystem_ [Fulfillment] SystemDependency
|
|
|
|
| IOTest_ String [Fulfillment] (IO (Maybe Msg))
|
2022-06-26 19:27:04 -04:00
|
|
|
| forall a. IOSometimes_ (Sometimes a)
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-07-06 00:16:47 -04:00
|
|
|
instance Eq IODependency_ where
|
2022-07-09 01:02:37 -04:00
|
|
|
(==) (IOSystem_ f0 s0) (IOSystem_ f1 s1) = f0 == f1 && s0 == s1
|
|
|
|
(==) (IOTest_ {}) (IOTest_ {}) = False
|
|
|
|
(==) (IOSometimes_ _) (IOSometimes_ _) = False
|
|
|
|
(==) _ _ = False
|
2022-07-06 00:16:47 -04:00
|
|
|
|
|
|
|
instance Hashable IODependency_ where
|
2022-07-09 01:02:37 -04:00
|
|
|
hashWithSalt s (IOSystem_ f y) = s `hashWithSalt` f
|
|
|
|
`hashWithSalt` y
|
|
|
|
hashWithSalt s (IOTest_ n f _) = s `hashWithSalt` n
|
|
|
|
`hashWithSalt` f
|
2022-07-08 18:57:12 -04:00
|
|
|
hashWithSalt s (IOSometimes_ (Sometimes n _ _)) = hashWithSalt s n
|
2022-07-04 12:30:13 -04:00
|
|
|
|
2022-07-03 18:23:32 -04:00
|
|
|
-- | A system component to an IODependency
|
|
|
|
-- This name is dumb, but most constructors should be obvious
|
|
|
|
data SystemDependency =
|
|
|
|
Executable Bool FilePath
|
2021-11-21 10:26:28 -05:00
|
|
|
| AccessiblePath FilePath Bool Bool
|
|
|
|
| Systemd UnitType String
|
2022-07-08 18:17:41 -04:00
|
|
|
| Process String
|
2022-07-04 12:30:13 -04:00
|
|
|
deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance Hashable SystemDependency
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | The type of a systemd service
|
2022-07-04 12:30:13 -04:00
|
|
|
data UnitType = SystemUnit | UserUnit deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance Hashable UnitType
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Wrapper type to describe and endpoint
|
2021-11-22 23:46:51 -05:00
|
|
|
data DBusMember = Method_ MemberName
|
|
|
|
| Signal_ MemberName
|
|
|
|
| Property_ String
|
2022-07-06 00:16:47 -04:00
|
|
|
deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance Hashable DBusMember where
|
|
|
|
hashWithSalt s (Method_ m) = hashWithSalt s $ formatMemberName m
|
|
|
|
hashWithSalt s (Signal_ m) = hashWithSalt s $ formatMemberName m
|
|
|
|
hashWithSalt s (Property_ p) = hashWithSalt s p
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
-- TODO there is a third type of package: not in aur or official
|
|
|
|
-- | A means to fulfill a dependency
|
|
|
|
-- For now this is just the name of an Arch Linux package (AUR or official)
|
2022-07-09 14:59:42 -04:00
|
|
|
data Fulfillment = Package ArchPkg String deriving (Eq, Show)
|
2022-07-09 01:02:37 -04:00
|
|
|
|
|
|
|
instance Hashable Fulfillment where
|
|
|
|
hashWithSalt s (Package a n) = s `hashWithSalt` a `hashWithSalt` n
|
|
|
|
|
2022-07-09 14:59:42 -04:00
|
|
|
data ArchPkg = Official | AUR | Custom deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance Hashable ArchPkg
|
|
|
|
|
2021-11-22 23:46:51 -05:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Tested dependency tree
|
2021-11-21 10:26:28 -05:00
|
|
|
--
|
2022-06-26 19:05:25 -04:00
|
|
|
-- The main reason I need this is so I have a "result" I can convert to JSON
|
|
|
|
-- and dump on the CLI (unless there is a way to make Aeson work inside an IO)
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
-- | A message with criteria for when to show it
|
|
|
|
data Msg = Msg LogLevel String
|
|
|
|
|
|
|
|
-- | A message annotated with subfeature and feature name
|
2022-07-08 18:57:12 -04:00
|
|
|
data FMsg = FMsg String (Maybe String) Msg
|
2022-07-07 01:05:17 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Tested Always feature
|
2022-06-28 23:27:55 -04:00
|
|
|
data PostAlways a = Primary (SubfeaturePass a) [SubfeatureFail] (Always_ a)
|
2022-06-26 19:05:25 -04:00
|
|
|
| Fallback a [SubfeatureFail]
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Tested Sometimes feature
|
|
|
|
data PostSometimes a = PostSometimes
|
|
|
|
{ psSuccess :: Maybe (SubfeaturePass a)
|
|
|
|
, psFailed :: [SubfeatureFail]
|
|
|
|
}
|
2022-06-22 01:28:46 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Passing subfeature
|
|
|
|
type SubfeaturePass a = Subfeature (PostPass a)
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Failed subfeature
|
|
|
|
type SubfeatureFail = Subfeature PostFail
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | An action that passed
|
2022-07-07 01:05:17 -04:00
|
|
|
data PostPass a = PostPass a [Msg] deriving (Functor)
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
addMsgs :: PostPass a -> [Msg] -> PostPass a
|
2022-06-26 19:05:25 -04:00
|
|
|
addMsgs (PostPass a ms) ms' = PostPass a $ ms ++ ms'
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | An action that failed
|
2022-07-07 01:05:17 -04:00
|
|
|
data PostFail = PostFail [Msg] | PostMissing Msg
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Evaluation cache
|
|
|
|
--
|
|
|
|
-- Setting up trees like this usually entails having some repeated dependencies.
|
|
|
|
-- Testing the same dependency multiple times is stupid, so cache the results.
|
|
|
|
-- Note this is basically memorization, except that the results are IO-dependent
|
|
|
|
-- and this may technically change with each invocation. The assumption here is
|
|
|
|
-- that each repeated test without caching would be run in such close succession
|
|
|
|
-- that the results will always be the same.
|
|
|
|
|
2022-07-09 19:55:25 -04:00
|
|
|
-- TODO cache DBus calls
|
2022-07-06 00:16:47 -04:00
|
|
|
emptyCache :: Cache
|
2022-07-09 17:08:10 -04:00
|
|
|
emptyCache = Cache H.empty H.empty
|
2022-07-06 00:16:47 -04:00
|
|
|
|
|
|
|
memoizeIO_ :: (IODependency_ -> FIO Result_) -> IODependency_ -> FIO Result_
|
|
|
|
memoizeIO_ f d = do
|
|
|
|
m <- gets cIO_
|
|
|
|
case H.lookup d m of
|
2022-07-09 19:55:25 -04:00
|
|
|
(Just r) -> return $ info "retrieving from to cache" r
|
2022-07-06 00:16:47 -04:00
|
|
|
Nothing -> do
|
2022-07-09 19:55:25 -04:00
|
|
|
r <- info "adding to cache" <$> f d
|
2022-07-06 00:16:47 -04:00
|
|
|
modify (\s -> s { cIO_ = H.insert d r (cIO_ s) })
|
|
|
|
return r
|
2022-07-09 19:55:25 -04:00
|
|
|
where
|
|
|
|
info m = fmap (++ [Msg Info m])
|
2022-07-04 12:30:13 -04:00
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
memoizeFont :: (String -> IO (Result FontBuilder)) -> String -> FIO (Result FontBuilder)
|
|
|
|
memoizeFont f d = do
|
|
|
|
m <- gets cFont
|
|
|
|
case H.lookup d m of
|
2022-07-09 19:55:25 -04:00
|
|
|
(Just r) -> return $ info "retrieving from cache" r
|
2022-07-06 18:54:10 -04:00
|
|
|
Nothing -> do
|
2022-07-09 19:55:25 -04:00
|
|
|
r <- io $ info "adding to cache" <$> f d
|
2022-07-06 18:54:10 -04:00
|
|
|
modify (\s -> s { cFont = H.insert d r (cFont s) })
|
|
|
|
return r
|
2022-07-09 19:55:25 -04:00
|
|
|
where
|
|
|
|
info m = fmap (`addMsgs` [Msg Info m])
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-07 18:40:42 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Configuration
|
|
|
|
|
|
|
|
type FIO a = ReaderT XParams (StateT Cache IO) a
|
|
|
|
|
|
|
|
data XParams = XParams
|
|
|
|
{ xpLogLevel :: LogLevel
|
2022-07-08 18:57:12 -04:00
|
|
|
, xpFeatures :: XPFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
instance FromJSON XParams where
|
|
|
|
parseJSON = withObject "parameters" $ \o -> XParams
|
|
|
|
<$> o .: fromString "loglevel"
|
|
|
|
<*> o .: fromString "features"
|
|
|
|
|
|
|
|
instance FromJSON LogLevel
|
|
|
|
|
|
|
|
data XPFeatures = XPFeatures
|
|
|
|
{ xpfOptimus :: Bool
|
|
|
|
, xpfVirtualBox :: Bool
|
|
|
|
, xpfXSANE :: Bool
|
2022-08-01 10:43:52 -04:00
|
|
|
, xpfEthernet :: Bool
|
2022-07-08 18:57:12 -04:00
|
|
|
, xpfWireless :: Bool
|
|
|
|
, xpfVPN :: Bool
|
|
|
|
, xpfBluetooth :: Bool
|
|
|
|
, xpfIntelBacklight :: Bool
|
|
|
|
, xpfClevoBacklight :: Bool
|
2022-07-08 20:01:35 -04:00
|
|
|
, xpfBattery :: Bool
|
2022-07-09 01:07:41 -04:00
|
|
|
, xpfF5VPN :: Bool
|
2022-07-07 18:40:42 -04:00
|
|
|
}
|
|
|
|
|
2022-07-08 18:57:12 -04:00
|
|
|
instance FromJSON XPFeatures where
|
|
|
|
parseJSON = withObject "features" $ \o -> XPFeatures
|
|
|
|
<$> o .:+ "optimus"
|
|
|
|
<*> o .:+ "virtualbox"
|
|
|
|
<*> o .:+ "xsane"
|
2022-08-01 10:43:52 -04:00
|
|
|
<*> o .:+ "ethernet"
|
2022-07-08 18:57:12 -04:00
|
|
|
<*> o .:+ "wireless"
|
|
|
|
<*> o .:+ "vpn"
|
|
|
|
<*> o .:+ "bluetooth"
|
|
|
|
<*> o .:+ "intel_backlight"
|
|
|
|
<*> o .:+ "clevo_backlight"
|
2022-07-08 20:01:35 -04:00
|
|
|
<*> o .:+ "battery"
|
2022-07-09 01:07:41 -04:00
|
|
|
<*> o .:+ "f5vpn"
|
2022-07-08 18:57:12 -04:00
|
|
|
|
2022-07-07 18:40:42 -04:00
|
|
|
defParams :: XParams
|
2022-07-08 18:57:12 -04:00
|
|
|
defParams = XParams
|
|
|
|
{ xpLogLevel = Error
|
|
|
|
, xpFeatures = defXPFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
defXPFeatures :: XPFeatures
|
|
|
|
defXPFeatures = XPFeatures
|
|
|
|
{ xpfOptimus = False
|
|
|
|
, xpfVirtualBox = False
|
|
|
|
, xpfXSANE = False
|
2022-08-01 10:43:52 -04:00
|
|
|
, xpfEthernet = False
|
2022-07-08 18:57:12 -04:00
|
|
|
, xpfWireless = False
|
2022-07-08 20:01:35 -04:00
|
|
|
-- TODO this might be broken down into different flags (expressvpn, etc)
|
2022-07-08 18:57:12 -04:00
|
|
|
, xpfVPN = False
|
|
|
|
, xpfBluetooth = False
|
|
|
|
, xpfIntelBacklight = False
|
|
|
|
, xpfClevoBacklight = False
|
2022-07-08 20:01:35 -04:00
|
|
|
, xpfBattery = False
|
2022-07-09 01:07:41 -04:00
|
|
|
, xpfF5VPN = False
|
2022-07-08 18:57:12 -04:00
|
|
|
}
|
2022-07-07 18:40:42 -04:00
|
|
|
|
2022-07-08 20:01:35 -04:00
|
|
|
type XPQuery = XPFeatures -> Bool
|
|
|
|
|
2022-07-07 18:40:42 -04:00
|
|
|
data Cache = Cache
|
|
|
|
{ --cIO :: forall p. Memoizable p => H.HashMap (IODependency p) (Result p)
|
2022-07-09 17:08:10 -04:00
|
|
|
cIO_ :: H.HashMap IODependency_ Result_
|
|
|
|
-- , cDBus_ :: forall c. H.HashMap (DBusDependency_ c) Result_
|
|
|
|
, cFont :: H.HashMap String (Result FontBuilder)
|
2022-07-07 18:40:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getParams :: IO XParams
|
|
|
|
getParams = do
|
|
|
|
p <- getParamFile
|
|
|
|
maybe (return defParams) decodeYaml p
|
|
|
|
where
|
|
|
|
decodeYaml p = either (\e -> print e >> return defParams) return
|
|
|
|
=<< decodeFileEither p
|
|
|
|
|
|
|
|
getParamFile :: IO (Maybe FilePath)
|
|
|
|
getParamFile = do
|
|
|
|
e <- lookupEnv "XDG_CONFIG_HOME"
|
|
|
|
parent <- case e of
|
|
|
|
Nothing -> fallback
|
|
|
|
Just path
|
|
|
|
| isRelative path -> fallback
|
|
|
|
| otherwise -> return path
|
|
|
|
let full = parent </> "xmonad.yml"
|
|
|
|
(\x -> if x then Just full else Nothing) <$> fileExist full
|
|
|
|
where
|
|
|
|
fallback = (</> ".config") <$> getHomeDirectory
|
|
|
|
|
2022-07-08 18:57:12 -04:00
|
|
|
(.:+) :: Object -> String -> Parser Bool
|
|
|
|
(.:+) o n = o .:? fromString n .!= False
|
2022-07-07 18:40:42 -04:00
|
|
|
|
2022-07-08 18:57:12 -04:00
|
|
|
infix .:+
|
2022-07-07 18:40:42 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-26 19:05:25 -04:00
|
|
|
-- | Testing pipeline
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
evalSometimesMsg :: Sometimes a -> FIO (Either [FMsg] (a, [FMsg]))
|
2022-07-08 18:57:12 -04:00
|
|
|
evalSometimesMsg (Sometimes n u xs) = do
|
|
|
|
r <- asks (u . xpFeatures)
|
2022-07-08 20:01:35 -04:00
|
|
|
if not r then return $ Left [dis n] else do
|
2022-07-08 18:57:12 -04:00
|
|
|
PostSometimes { psSuccess = s, psFailed = fs } <- testSometimes xs
|
|
|
|
let fs' = failedMsgs n fs
|
|
|
|
return $ case s of
|
|
|
|
(Just p) -> Right $ second (++ fs') $ passActMsg n p
|
|
|
|
_ -> Left fs'
|
2022-07-08 20:01:35 -04:00
|
|
|
where
|
|
|
|
dis name = FMsg name Nothing (Msg Debug "feature disabled")
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
evalAlwaysMsg :: Always a -> FIO (a, [FMsg])
|
2022-07-04 12:30:13 -04:00
|
|
|
evalAlwaysMsg (Always n x) = do
|
2022-06-26 19:05:25 -04:00
|
|
|
r <- testAlways x
|
2022-07-07 01:05:17 -04:00
|
|
|
return $ case r of
|
|
|
|
(Primary p fs _) -> second (++ failedMsgs n fs) $ passActMsg n p
|
|
|
|
(Fallback act fs) -> (act, failedMsgs n fs)
|
|
|
|
|
|
|
|
passActMsg :: String -> SubfeaturePass a -> (a, [FMsg])
|
2022-07-08 18:57:12 -04:00
|
|
|
passActMsg fn Subfeature { sfData = PostPass a ws, sfName = n } = (a, fmap (FMsg fn (Just n)) ws)
|
2022-07-07 01:05:17 -04:00
|
|
|
|
|
|
|
failedMsgs :: String -> [SubfeatureFail] -> [FMsg]
|
|
|
|
failedMsgs n = concatMap (failedMsg n)
|
|
|
|
|
|
|
|
failedMsg :: String -> SubfeatureFail -> [FMsg]
|
|
|
|
failedMsg fn Subfeature { sfData = d, sfName = n } = case d of
|
|
|
|
(PostFail es) -> f es
|
|
|
|
(PostMissing e) -> f [e]
|
|
|
|
where
|
2022-07-08 18:57:12 -04:00
|
|
|
f = fmap (FMsg fn (Just n))
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testAlways :: Always_ a -> FIO (PostAlways a)
|
2022-06-21 00:56:42 -04:00
|
|
|
testAlways = go []
|
|
|
|
where
|
|
|
|
go failed (Option fd next) = do
|
|
|
|
r <- testSubfeature fd
|
|
|
|
case r of
|
2022-06-26 19:05:25 -04:00
|
|
|
(Left l) -> go (l:failed) next
|
|
|
|
(Right pass) -> return $ Primary pass failed next
|
2022-07-02 00:09:16 -04:00
|
|
|
go failed (Always_ ar) = (`Fallback` failed) <$> evalFallbackRoot ar
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
evalFallbackRoot :: FallbackRoot a -> FIO a
|
2022-07-02 00:09:16 -04:00
|
|
|
evalFallbackRoot (FallbackAlone a) = return a
|
|
|
|
evalFallbackRoot (FallbackTree a s) = a <$> evalFallbackStack s
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
evalFallbackStack :: FallbackStack p -> FIO p
|
2022-07-02 00:09:16 -04:00
|
|
|
evalFallbackStack (FallbackBottom a) = evalAlways a
|
|
|
|
evalFallbackStack (FallbackStack f a as) = do
|
|
|
|
ps <- evalFallbackStack as
|
|
|
|
p <- evalAlways a
|
|
|
|
return $ f p ps
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testSometimes :: Sometimes_ a -> FIO (PostSometimes a)
|
2022-06-26 19:05:25 -04:00
|
|
|
testSometimes = go (PostSometimes Nothing [])
|
2022-06-21 00:56:42 -04:00
|
|
|
where
|
|
|
|
go ts [] = return ts
|
|
|
|
go ts (x:xs) = do
|
2022-06-26 19:05:25 -04:00
|
|
|
sf <- testSubfeature x
|
|
|
|
case sf of
|
|
|
|
(Left l) -> go (ts { psFailed = l:psFailed ts }) xs
|
|
|
|
(Right pass) -> return $ ts { psSuccess = Just pass }
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testSubfeature :: SubfeatureRoot a -> FIO (Either SubfeatureFail (SubfeaturePass a))
|
2022-06-26 19:05:25 -04:00
|
|
|
testSubfeature sf@Subfeature{ sfData = t } = do
|
|
|
|
t' <- testRoot t
|
|
|
|
-- monomorphism restriction :(
|
|
|
|
return $ bimap (\n -> sf { sfData = n }) (\n -> sf { sfData = n }) t'
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testRoot :: Root a -> FIO (Either PostFail (PostPass a))
|
2022-06-26 19:05:25 -04:00
|
|
|
testRoot r = do
|
|
|
|
case r of
|
|
|
|
(IORoot a t) -> go a testIODependency_ testIODependency t
|
|
|
|
(IORoot_ a t) -> go_ a testIODependency_ t
|
|
|
|
(DBusRoot a t (Just cl)) -> go (`a` cl) (testDBusDependency_ cl) testIODependency t
|
|
|
|
(DBusRoot_ a t (Just cl)) -> go_ (a cl) (testDBusDependency_ cl) t
|
2022-07-07 01:05:17 -04:00
|
|
|
_ -> return $ Left $ PostMissing
|
|
|
|
$ Msg Error "client not available"
|
2022-06-21 00:56:42 -04:00
|
|
|
where
|
2022-07-01 23:15:44 -04:00
|
|
|
-- rank N polymorphism is apparently undecidable...gross
|
2022-07-04 12:30:13 -04:00
|
|
|
go a f_ (f :: forall q. d q -> FIO (Result q)) t =
|
2022-07-01 23:15:44 -04:00
|
|
|
bimap PostFail (fmap a) <$> testTree f_ f t
|
2022-06-26 19:05:25 -04:00
|
|
|
go_ a f_ t = bimap PostFail (PostPass a) <$> testTree_ f_ t
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Payloaded dependency testing
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
type Result p = Either [Msg] (PostPass p)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testTree :: forall d d_ p. (d_ -> FIO Result_) -> (forall q. d q -> FIO (Result q))
|
2022-07-07 01:05:17 -04:00
|
|
|
-> Tree d d_ p -> FIO (Either [Msg] (PostPass p))
|
2022-06-26 19:05:25 -04:00
|
|
|
testTree test_ test = go
|
2022-06-21 00:56:42 -04:00
|
|
|
where
|
2022-07-07 01:05:17 -04:00
|
|
|
go :: forall q. Tree d d_ q -> FIO (Either [Msg] (PostPass q))
|
2022-06-26 20:48:26 -04:00
|
|
|
go (And12 f a b) = do
|
2022-06-26 19:05:25 -04:00
|
|
|
ra <- go a
|
2022-06-26 20:48:26 -04:00
|
|
|
liftRight (\pa -> (and2nd f pa =<<) <$> go b) ra
|
|
|
|
go (And1 a b) = do
|
|
|
|
ra <- go a
|
|
|
|
liftRight (\p -> fmap (addMsgs p) <$> testTree_ test_ b) ra
|
|
|
|
go (And2 a b) = do
|
2022-06-26 19:05:25 -04:00
|
|
|
ra <- testTree_ test_ a
|
2022-06-26 20:48:26 -04:00
|
|
|
liftRight (\wa -> fmap (`addMsgs` wa) <$> go b) ra
|
|
|
|
go (Or a b) = do
|
2022-06-26 19:05:25 -04:00
|
|
|
ra <- go a
|
2022-06-26 20:48:26 -04:00
|
|
|
either (\ea -> fmap (`addMsgs` ea) <$> go b) (return . Right) ra
|
2022-06-26 19:05:25 -04:00
|
|
|
go (Only a) = test a
|
|
|
|
and2nd f (PostPass pa wa) (PostPass pb wb) = Right $ PostPass (f pa pb) $ wa ++ wb
|
2022-06-26 20:48:26 -04:00
|
|
|
liftRight = either (return . Left)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testIODependency :: IODependency p -> FIO (Result p)
|
2022-07-09 01:02:37 -04:00
|
|
|
testIODependency (IORead _ _ t) = t
|
2022-07-03 18:23:32 -04:00
|
|
|
testIODependency (IOConst c) = return $ Right $ PostPass c []
|
2022-06-28 21:24:21 -04:00
|
|
|
-- TODO this is a bit odd because this is a dependency that will always
|
|
|
|
-- succeed, which kinda makes this pointless. The only reason I would want this
|
|
|
|
-- is if I want to have a built-in logic to "choose" a payload to use in
|
|
|
|
-- building a higher-level feature
|
2022-07-07 01:05:17 -04:00
|
|
|
testIODependency (IOAlways a f) = Right . uncurry PostPass
|
|
|
|
-- TODO this is wetter than Taco Bell shit
|
|
|
|
. bimap f (fmap stripMsg) <$> evalAlwaysMsg a
|
|
|
|
testIODependency (IOSometimes x f) =
|
|
|
|
bimap (fmap stripMsg) (uncurry PostPass . bimap f (fmap stripMsg))
|
|
|
|
<$> evalSometimesMsg x
|
|
|
|
|
|
|
|
stripMsg :: FMsg -> Msg
|
|
|
|
stripMsg (FMsg _ _ m) = m
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Standalone dependency testing
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
type Result_ = Either [Msg] [Msg]
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
testTree_ :: (d -> FIO Result_) -> Tree_ d -> FIO Result_
|
2022-06-26 19:05:25 -04:00
|
|
|
testTree_ test = go
|
|
|
|
where
|
|
|
|
go (And_ a b) = either (return . Left) (`test2nd` b) =<< go a
|
|
|
|
go (Or_ a b) = either (`test2nd` b) (return . Right) =<< go a
|
|
|
|
go (Only_ a) = test a
|
|
|
|
test2nd ws = fmap ((Right . (ws ++)) =<<) . go
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
testIODependency_ :: IODependency_ -> FIO Result_
|
2022-07-06 00:16:47 -04:00
|
|
|
testIODependency_ = memoizeIO_ testIODependency'_
|
|
|
|
|
|
|
|
testIODependency'_ :: IODependency_ -> FIO Result_
|
2022-07-09 01:02:37 -04:00
|
|
|
testIODependency'_ (IOSystem_ _ s) = io $ readResult_ <$> testSysDependency s
|
|
|
|
testIODependency'_ (IOTest_ _ _ t) = io $ readResult_ <$> t
|
2022-07-07 01:05:17 -04:00
|
|
|
testIODependency'_ (IOSometimes_ x) = bimap (fmap stripMsg) (fmap stripMsg . snd)
|
|
|
|
<$> evalSometimesMsg x
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | System Dependency Testing
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
testSysDependency :: SystemDependency -> IO (Maybe Msg)
|
2022-06-26 19:05:25 -04:00
|
|
|
testSysDependency (Executable sys bin) = maybe (Just msg) (const Nothing)
|
|
|
|
<$> findExecutable bin
|
|
|
|
where
|
2022-07-07 01:05:17 -04:00
|
|
|
msg = Msg Error $ unwords [e, "executable", singleQuote bin, "not found"]
|
2022-06-26 19:05:25 -04:00
|
|
|
e = if sys then "system" else "local"
|
2022-07-02 17:09:21 -04:00
|
|
|
testSysDependency (Systemd t n) = shellTest cmd msg
|
2021-11-07 13:35:08 -05:00
|
|
|
where
|
2022-07-02 17:09:21 -04:00
|
|
|
msg = unwords ["systemd", unitType t, "unit", singleQuote n, "not found"]
|
2022-06-21 00:56:42 -04:00
|
|
|
cmd = fmtCmd "systemctl" $ ["--user" | t == UserUnit] ++ ["status", n]
|
2022-07-08 18:17:41 -04:00
|
|
|
testSysDependency (Process n) = shellTest (fmtCmd "pidof" [n])
|
|
|
|
$ "Process " ++ singleQuote n ++ " not found"
|
2022-06-26 19:05:25 -04:00
|
|
|
testSysDependency (AccessiblePath p r w) = permMsg <$> getPermissionsSafe p
|
2021-11-07 18:41:25 -05:00
|
|
|
where
|
2022-06-21 00:56:42 -04:00
|
|
|
testPerm False _ _ = Nothing
|
|
|
|
testPerm True f res = Just $ f res
|
2022-07-07 01:05:17 -04:00
|
|
|
mkErr = Just . Msg Error
|
|
|
|
permMsg NotFoundError = mkErr "file not found"
|
|
|
|
permMsg PermError = mkErr "could not get permissions"
|
2022-06-21 00:56:42 -04:00
|
|
|
permMsg (PermResult res) =
|
|
|
|
case (testPerm r readable res, testPerm w writable res) of
|
2022-07-07 01:05:17 -04:00
|
|
|
(Just False, Just False) -> mkErr "file not readable or writable"
|
|
|
|
(Just False, _) -> mkErr "file not readable"
|
|
|
|
(_, Just False) -> mkErr "file not writable"
|
2022-06-26 19:05:25 -04:00
|
|
|
_ -> Nothing
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
shellTest :: String -> String -> IO (Maybe Msg)
|
2022-07-02 17:09:21 -04:00
|
|
|
shellTest cmd msg = do
|
|
|
|
(rc, _, _) <- readCreateProcessWithExitCode' (shell cmd) ""
|
|
|
|
return $ case rc of
|
|
|
|
ExitSuccess -> Nothing
|
2022-07-07 01:05:17 -04:00
|
|
|
_ -> Just $ Msg Error msg
|
2022-07-02 17:09:21 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
unitType :: UnitType -> String
|
|
|
|
unitType SystemUnit = "system"
|
|
|
|
unitType UserUnit = "user"
|
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-07-08 00:21:05 -04:00
|
|
|
-- | Font testers
|
2022-07-06 18:54:10 -04:00
|
|
|
--
|
|
|
|
-- Make a special case for these since we end up testing the font alot, and it
|
|
|
|
-- would be nice if I can cache them.
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontAlways :: String -> String -> [Fulfillment] -> Always FontBuilder
|
|
|
|
fontAlways n fam ful = always1 n (fontFeatureName fam) root fallbackFont
|
2022-07-06 18:54:10 -04:00
|
|
|
where
|
2022-07-09 01:02:37 -04:00
|
|
|
root = IORoot id $ fontTree fam ful
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontSometimes :: String -> String -> [Fulfillment]-> Sometimes FontBuilder
|
|
|
|
fontSometimes n fam ful = sometimes1 n (fontFeatureName fam) root
|
2022-07-06 18:54:10 -04:00
|
|
|
where
|
2022-07-09 01:02:37 -04:00
|
|
|
root = IORoot id $ fontTree fam ful
|
2022-07-06 18:54:10 -04:00
|
|
|
|
|
|
|
fontFeatureName :: String -> String
|
|
|
|
fontFeatureName n = unwords ["Font family for", singleQuote n]
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontTreeAlt :: String -> [Fulfillment] -> Tree IODependency d_ FontBuilder
|
|
|
|
fontTreeAlt fam ful = Or (fontTree fam ful) $ Only $ IOConst fallbackFont
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontTree :: String -> [Fulfillment] -> Tree IODependency d_ FontBuilder
|
|
|
|
fontTree n = Only . fontDependency n
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontTree_ :: String -> [Fulfillment] -> IOTree_
|
|
|
|
fontTree_ n = Only_ . fontDependency_ n
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontDependency :: String -> [Fulfillment] -> IODependency FontBuilder
|
|
|
|
fontDependency fam ful = IORead (fontTestName fam) ful $ testFont fam
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
fontDependency_ :: String -> [Fulfillment] -> IODependency_
|
|
|
|
fontDependency_ fam ful = IOTest_ (fontTestName fam) ful $ voidRead <$> testFont' fam
|
2022-07-06 18:54:10 -04:00
|
|
|
|
|
|
|
fontTestName :: String -> String
|
|
|
|
fontTestName fam = unwords ["test if font", singleQuote fam, "exists"]
|
|
|
|
|
|
|
|
testFont :: String -> FIO (Result FontBuilder)
|
|
|
|
testFont = memoizeFont testFont'
|
|
|
|
|
|
|
|
testFont' :: String -> IO (Result FontBuilder)
|
2022-07-07 01:05:17 -04:00
|
|
|
testFont' fam = maybe pass (Left . (:[])) <$> shellTest cmd msg
|
2022-07-06 18:54:10 -04:00
|
|
|
where
|
|
|
|
msg = unwords ["font family", qFam, "not found"]
|
|
|
|
cmd = fmtCmd "fc-list" ["-q", qFam]
|
|
|
|
qFam = singleQuote fam
|
2022-07-07 01:05:17 -04:00
|
|
|
pass = Right $ PostPass (buildFont $ Just fam) []
|
2022-07-06 18:54:10 -04:00
|
|
|
|
2022-07-07 19:20:21 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-07-08 00:21:05 -04:00
|
|
|
-- | Network Testers
|
2022-07-07 19:20:21 -04:00
|
|
|
--
|
|
|
|
-- ASSUME that the system uses systemd in which case ethernet interfaces always
|
|
|
|
-- start with "en" and wireless interfaces always start with "wl"
|
|
|
|
|
|
|
|
readEthernet :: IODependency String
|
|
|
|
readEthernet = readInterface "get ethernet interface" isEthernet
|
|
|
|
|
|
|
|
readWireless :: IODependency String
|
|
|
|
readWireless = readInterface "get wireless interface" isWireless
|
|
|
|
|
|
|
|
isWireless :: String -> Bool
|
|
|
|
isWireless ('w':'l':_) = True
|
|
|
|
isWireless _ = False
|
|
|
|
|
|
|
|
isEthernet :: String -> Bool
|
|
|
|
isEthernet ('e':'n':_) = True
|
|
|
|
isEthernet _ = False
|
|
|
|
|
|
|
|
listInterfaces :: IO [String]
|
|
|
|
listInterfaces = fromRight [] <$> tryIOError (listDirectory sysfsNet)
|
|
|
|
|
|
|
|
sysfsNet :: FilePath
|
|
|
|
sysfsNet = "/sys/class/net"
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
-- ASSUME there are no (non-base) packages required to make these interfaces
|
|
|
|
-- work (all at the kernel level)
|
2022-07-07 19:20:21 -04:00
|
|
|
readInterface :: String -> (String -> Bool) -> IODependency String
|
2022-07-09 01:02:37 -04:00
|
|
|
readInterface n f = IORead n [] go
|
2022-07-07 19:20:21 -04:00
|
|
|
where
|
|
|
|
go = io $ do
|
|
|
|
ns <- filter f <$> listInterfaces
|
|
|
|
case ns of
|
|
|
|
[] -> return $ Left [Msg Error "no interfaces found"]
|
|
|
|
(x:xs) -> do
|
|
|
|
return $ Right $ PostPass x
|
|
|
|
$ fmap (Msg Warn . ("ignoring extra interface: " ++)) xs
|
|
|
|
|
2022-07-08 00:21:05 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Misc testers
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
socketExists :: String -> [Fulfillment] -> IO FilePath -> IODependency_
|
|
|
|
socketExists n ful = IOTest_ ("test if " ++ n ++ " socket exists") ful
|
|
|
|
. socketExists'
|
2022-07-08 00:21:05 -04:00
|
|
|
|
|
|
|
socketExists' :: IO FilePath -> IO (Maybe Msg)
|
|
|
|
socketExists' getPath = do
|
|
|
|
p <- getPath
|
2022-07-08 02:11:42 -04:00
|
|
|
r <- tryIOError $ getFileStatus p
|
|
|
|
return $ case r of
|
|
|
|
Left e -> toErr $ ioe_description e
|
|
|
|
Right s -> if isSocket s then Nothing else toErr $ p ++ " is not a socket"
|
2022-07-08 00:21:05 -04:00
|
|
|
where
|
|
|
|
toErr = Just . Msg Error
|
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus Dependency Testing
|
|
|
|
|
|
|
|
introspectInterface :: InterfaceName
|
|
|
|
introspectInterface = interfaceName_ "org.freedesktop.DBus.Introspectable"
|
|
|
|
|
|
|
|
introspectMethod :: MemberName
|
|
|
|
introspectMethod = memberName_ "Introspect"
|
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
testDBusDependency_ :: SafeClient c => c -> DBusDependency_ c -> FIO Result_
|
|
|
|
-- testDBusDependency_ cl = memoizeDBus_ (testDBusDependency'_ cl)
|
|
|
|
testDBusDependency_ = testDBusDependency'_
|
2022-07-06 00:16:47 -04:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
testDBusDependency'_ :: SafeClient c => c -> DBusDependency_ c -> FIO Result_
|
2022-07-09 01:02:37 -04:00
|
|
|
testDBusDependency'_ cl (Bus _ bus) = io $ do
|
2022-07-09 17:44:14 -04:00
|
|
|
ret <- callMethod cl queryBus queryPath queryIface queryMem
|
2021-11-20 19:35:24 -05:00
|
|
|
return $ case ret of
|
2022-07-07 01:05:17 -04:00
|
|
|
Left e -> Left [Msg Error e]
|
2021-11-20 19:35:24 -05:00
|
|
|
Right b -> let ns = bodyGetNames b in
|
2022-06-26 19:05:25 -04:00
|
|
|
if bus' `elem` ns then Right []
|
2022-07-07 01:05:17 -04:00
|
|
|
else Left [
|
|
|
|
Msg Error $ unwords ["name", singleQuote bus', "not found on dbus"]
|
|
|
|
]
|
2021-11-07 20:16:53 -05:00
|
|
|
where
|
2021-11-20 19:35:24 -05:00
|
|
|
bus' = formatBusName bus
|
|
|
|
queryBus = busName_ "org.freedesktop.DBus"
|
|
|
|
queryIface = interfaceName_ "org.freedesktop.DBus"
|
|
|
|
queryPath = objectPath_ "/"
|
|
|
|
queryMem = memberName_ "ListNames"
|
|
|
|
bodyGetNames [v] = fromMaybe [] $ fromVariant v :: [String]
|
|
|
|
bodyGetNames _ = []
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
testDBusDependency'_ cl (Endpoint _ busname objpath iface mem) = io $ do
|
2022-07-09 17:44:14 -04:00
|
|
|
ret <- callMethod cl busname objpath introspectInterface introspectMethod
|
2021-11-20 19:35:24 -05:00
|
|
|
return $ case ret of
|
2022-07-07 01:05:17 -04:00
|
|
|
Left e -> Left [Msg Error e]
|
2021-11-20 19:35:24 -05:00
|
|
|
Right body -> procBody body
|
|
|
|
where
|
|
|
|
procBody body = let res = findMem =<< I.parseXML objpath =<< fromVariant
|
|
|
|
=<< listToMaybe body in
|
|
|
|
case res of
|
2022-06-26 19:05:25 -04:00
|
|
|
Just True -> Right []
|
2022-07-07 01:05:17 -04:00
|
|
|
_ -> Left [Msg Error $ fmtMsg' mem]
|
2021-11-20 19:35:24 -05:00
|
|
|
findMem = fmap (matchMem mem)
|
|
|
|
. find (\i -> I.interfaceName i == iface)
|
|
|
|
. I.objectInterfaces
|
|
|
|
matchMem (Method_ n) = elemMember n I.methodName I.interfaceMethods
|
|
|
|
matchMem (Signal_ n) = elemMember n I.signalName I.interfaceSignals
|
|
|
|
matchMem (Property_ n) = elemMember n I.propertyName I.interfaceProperties
|
|
|
|
elemMember n fname fmember = elem n . fmap fname . fmember
|
|
|
|
fmtMem (Method_ n) = "method " ++ singleQuote (formatMemberName n)
|
|
|
|
fmtMem (Signal_ n) = "signal " ++ singleQuote (formatMemberName n)
|
|
|
|
fmtMem (Property_ n) = "property " ++ singleQuote n
|
|
|
|
fmtMsg' m = unwords
|
|
|
|
[ "could not find"
|
|
|
|
, fmtMem m
|
|
|
|
, "on interface"
|
|
|
|
, singleQuote $ formatInterfaceName iface
|
|
|
|
, "on bus"
|
2021-11-21 22:47:43 -05:00
|
|
|
, formatBusName busname
|
2021-11-20 19:35:24 -05:00
|
|
|
]
|
2021-11-26 23:35:03 -05:00
|
|
|
|
2022-07-06 00:16:47 -04:00
|
|
|
testDBusDependency'_ _ (DBusIO i) = testIODependency_ i
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-28 21:24:21 -04:00
|
|
|
-- | IO Lifting functions
|
|
|
|
|
|
|
|
ioSometimes :: MonadIO m => Sometimes (IO a) -> Sometimes (m a)
|
2022-07-08 18:57:12 -04:00
|
|
|
ioSometimes (Sometimes n t xs) = Sometimes n t $ fmap ioSubfeature xs
|
2022-06-28 21:24:21 -04:00
|
|
|
|
|
|
|
ioAlways :: MonadIO m => Always (IO a) -> Always (m a)
|
2022-06-28 23:27:55 -04:00
|
|
|
ioAlways (Always n x) = Always n $ ioAlways' x
|
|
|
|
|
|
|
|
ioAlways' :: MonadIO m => Always_ (IO a) -> Always_ (m a)
|
2022-07-02 00:09:16 -04:00
|
|
|
ioAlways' (Always_ ar) = Always_ $ ioFallbackRoot ar
|
2022-06-28 23:27:55 -04:00
|
|
|
ioAlways' (Option sf a) = Option (ioSubfeature sf) $ ioAlways' a
|
2022-06-28 21:24:21 -04:00
|
|
|
|
2022-07-02 00:09:16 -04:00
|
|
|
ioFallbackRoot :: MonadIO m => FallbackRoot (IO a) -> FallbackRoot (m a)
|
|
|
|
ioFallbackRoot (FallbackAlone a) = FallbackAlone $ io a
|
|
|
|
ioFallbackRoot (FallbackTree a s) = FallbackTree (io . a) s
|
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
ioSubfeature :: MonadIO m => SubfeatureRoot (IO a) -> SubfeatureRoot (m a)
|
|
|
|
ioSubfeature sf = sf { sfData = ioRoot $ sfData sf }
|
|
|
|
|
|
|
|
ioRoot :: MonadIO m => Root (IO a) -> Root (m a)
|
|
|
|
ioRoot (IORoot a t) = IORoot (io . a) t
|
|
|
|
ioRoot (IORoot_ a t) = IORoot_ (io a) t
|
|
|
|
ioRoot (DBusRoot a t cl) = DBusRoot (\p c -> io $ a p c) t cl
|
|
|
|
ioRoot (DBusRoot_ a t cl) = DBusRoot_ (io . a) t cl
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Feature constructors
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-08 20:01:35 -04:00
|
|
|
sometimes1_ :: XPQuery -> String -> String -> Root a -> Sometimes a
|
2022-07-08 19:02:49 -04:00
|
|
|
sometimes1_ x fn n t = Sometimes fn x
|
|
|
|
[Subfeature{ sfData = t, sfName = n }]
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-08 19:02:49 -04:00
|
|
|
always1_ :: String -> String -> Root a -> a -> Always a
|
|
|
|
always1_ fn n t x = Always fn
|
|
|
|
$ Option (Subfeature{ sfData = t, sfName = n }) (Always_ $ FallbackAlone x)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 23:27:55 -04:00
|
|
|
sometimes1 :: String -> String -> Root a -> Sometimes a
|
2022-07-08 19:02:49 -04:00
|
|
|
sometimes1 = sometimes1_ (const True)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 23:27:55 -04:00
|
|
|
always1 :: String -> String -> Root a -> a -> Always a
|
2022-07-08 19:02:49 -04:00
|
|
|
always1 = always1_
|
2022-06-26 20:07:25 -04:00
|
|
|
|
2022-07-02 17:09:21 -04:00
|
|
|
sometimesIO_ :: String -> String -> IOTree_ -> a -> Sometimes a
|
|
|
|
sometimesIO_ fn n t x = sometimes1 fn n $ IORoot_ x t
|
|
|
|
|
|
|
|
sometimesIO :: String -> String -> IOTree p -> (p -> a) -> Sometimes a
|
|
|
|
sometimesIO fn n t x = sometimes1 fn n $ IORoot x t
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
sometimesExe :: MonadIO m => String -> String -> [Fulfillment] -> Bool
|
|
|
|
-> FilePath -> Sometimes (m ())
|
|
|
|
sometimesExe fn n ful sys path = sometimesExeArgs fn n ful sys path []
|
2022-06-28 21:24:21 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
sometimesExeArgs :: MonadIO m => String -> String -> [Fulfillment] -> Bool
|
|
|
|
-> FilePath -> [String] -> Sometimes (m ())
|
|
|
|
sometimesExeArgs fn n ful sys path args =
|
|
|
|
sometimesIO_ fn n (Only_ (IOSystem_ ful $ Executable sys path)) $ spawnCmd path args
|
2022-06-28 21:24:21 -04:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
sometimesDBus :: SafeClient c => Maybe c -> String -> String
|
|
|
|
-> Tree_ (DBusDependency_ c) -> (c -> a) -> Sometimes a
|
2022-06-28 23:27:55 -04:00
|
|
|
sometimesDBus c fn n t x = sometimes1 fn n $ DBusRoot_ x t c
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
sometimesEndpoint :: (SafeClient c, MonadIO m) => String -> String
|
|
|
|
-> [Fulfillment] -> BusName -> ObjectPath -> InterfaceName -> MemberName
|
|
|
|
-> Maybe c -> Sometimes (m ())
|
2022-07-09 01:02:37 -04:00
|
|
|
sometimesEndpoint fn name ful busname path iface mem cl =
|
|
|
|
sometimesDBus cl fn name deps cmd
|
2022-06-28 21:24:21 -04:00
|
|
|
where
|
2022-07-09 01:02:37 -04:00
|
|
|
deps = Only_ $ Endpoint ful busname path iface $ Method_ mem
|
2022-07-09 17:44:14 -04:00
|
|
|
cmd c = io $ void $ callMethod c busname path iface mem
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Dependency Tree Constructors
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
listToAnds :: d -> [d] -> Tree_ d
|
|
|
|
listToAnds i = foldr (And_ . Only_) (Only_ i)
|
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
toAnd_ :: d -> d -> Tree_ d
|
|
|
|
toAnd_ a b = And_ (Only_ a) (Only_ b)
|
|
|
|
|
|
|
|
toFallback :: IODependency p -> p -> Tree IODependency d_ p
|
|
|
|
toFallback a = Or (Only a) . Only . IOConst
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-04 00:36:41 -04:00
|
|
|
voidResult :: Result p -> Result_
|
|
|
|
voidResult (Left es) = Left es
|
|
|
|
voidResult (Right (PostPass _ ws)) = Right ws
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
voidRead :: Result p -> Maybe Msg
|
|
|
|
voidRead (Left []) = Just $ Msg Error "unspecified error"
|
2022-07-04 00:36:41 -04:00
|
|
|
voidRead (Left (e:_)) = Just e
|
|
|
|
voidRead (Right _) = Nothing
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
readResult_ :: Maybe Msg -> Result_
|
2022-07-04 12:30:13 -04:00
|
|
|
readResult_ (Just w) = Left [w]
|
|
|
|
readResult_ _ = Right []
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-28 21:24:21 -04:00
|
|
|
-- | IO Dependency Constructors
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
exe :: Bool -> [Fulfillment] -> String -> IODependency_
|
|
|
|
exe b ful = IOSystem_ ful . Executable b
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
sysExe :: [Fulfillment] -> String -> IODependency_
|
2022-06-26 19:05:25 -04:00
|
|
|
sysExe = exe True
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
localExe :: [Fulfillment] -> String -> IODependency_
|
2022-06-26 19:05:25 -04:00
|
|
|
localExe = exe False
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
path' :: Bool -> Bool -> String -> [Fulfillment] -> IODependency_
|
|
|
|
path' r w n ful = IOSystem_ ful $ AccessiblePath n r w
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
pathR :: String -> [Fulfillment] -> IODependency_
|
|
|
|
pathR = path' True False
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
pathW :: String -> [Fulfillment] -> IODependency_
|
|
|
|
pathW = path' False True
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
pathRW :: String -> [Fulfillment] -> IODependency_
|
|
|
|
pathRW = path' True True
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
sysd :: UnitType -> [Fulfillment] -> String -> IODependency_
|
|
|
|
sysd u ful = IOSystem_ ful . Systemd u
|
|
|
|
|
|
|
|
sysdUser :: [Fulfillment] -> String -> IODependency_
|
2022-06-26 19:05:25 -04:00
|
|
|
sysdUser = sysd UserUnit
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
sysdSystem :: [Fulfillment] -> String -> IODependency_
|
2022-06-26 19:05:25 -04:00
|
|
|
sysdSystem = sysd SystemUnit
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
process :: [Fulfillment] -> String -> IODependency_
|
|
|
|
process ful = IOSystem_ ful . Process
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-28 21:24:21 -04:00
|
|
|
-- | Printing
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpSubfeatureRoot :: SubfeatureRoot a -> FIO (JSONUnquotable, Bool)
|
2022-06-28 21:24:21 -04:00
|
|
|
dumpSubfeatureRoot Subfeature { sfData = r, sfName = n } =
|
|
|
|
first (jsonSubfeature $ Q n) <$> dumpRoot r
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpRoot :: Root a -> FIO (JSONUnquotable, Bool)
|
2022-06-28 21:24:21 -04:00
|
|
|
dumpRoot (IORoot _ t) = first jsonIORoot <$>
|
|
|
|
dumpTree testIODependency testIODependency_ dataIODependency dataIODependency_ t
|
|
|
|
dumpRoot (IORoot_ _ t) = first jsonIORoot <$>
|
|
|
|
dumpTree_ testIODependency_ dataIODependency_ t
|
|
|
|
dumpRoot (DBusRoot _ t (Just cl)) = first jsonDBusRoot <$>
|
|
|
|
dumpTree testIODependency (testDBusDependency_ cl) dataIODependency dataDBusDependency t
|
|
|
|
dumpRoot (DBusRoot_ _ t (Just cl)) = first jsonDBusRoot <$>
|
|
|
|
dumpTree_ (testDBusDependency_ cl) dataDBusDependency t
|
|
|
|
-- TODO somehow return a message here that these failed
|
|
|
|
dumpRoot (DBusRoot _ t Nothing) =
|
|
|
|
return (jsonDBusRoot $ dataTree dataIODependency dataDBusDependency t, False)
|
|
|
|
dumpRoot (DBusRoot_ _ t Nothing) =
|
|
|
|
return (jsonDBusRoot $ dataTree_ dataDBusDependency t, False)
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpTree :: forall d d_ p. (forall q. d q -> FIO (Result q))
|
|
|
|
-> (d_ -> FIO Result_) -> (forall q. d q -> DependencyData)
|
|
|
|
-> (d_ -> DependencyData) -> Tree d d_ p -> FIO (JSONUnquotable, Bool)
|
2022-06-28 21:24:21 -04:00
|
|
|
dumpTree test test_ dd dd_ = go
|
|
|
|
where
|
2022-07-04 12:30:13 -04:00
|
|
|
go :: forall q. Tree d d_ q -> FIO (JSONUnquotable, Bool)
|
2022-06-28 21:24:21 -04:00
|
|
|
go (And12 _ a b) = doAnd go go data' a b
|
|
|
|
go (And1 a b) = doAnd go dump_' (dataTree_ dd_) a b
|
|
|
|
go (And2 a b) = doAnd dump_' go data' a b
|
|
|
|
go (Or a b) = do
|
|
|
|
(sa, ra) <- go a
|
|
|
|
let j = jsonOr sa
|
|
|
|
if ra then return (j $ data' b, ra) else first j <$> go b
|
|
|
|
go (Only d) = do
|
|
|
|
r <- fromResult <$> test d
|
|
|
|
let (x, y) = dd d
|
|
|
|
return (jsonLeaf (Just r) x y, fst r)
|
2022-07-01 23:15:44 -04:00
|
|
|
data' :: forall q. Tree d d_ q -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
data' = dataTree dd dd_
|
2022-07-01 23:15:44 -04:00
|
|
|
dump_' = dumpTree_ test_ dd_
|
2022-06-28 21:24:21 -04:00
|
|
|
doAnd fa fb fb_ a b = do
|
|
|
|
(sa, ra) <- fa a
|
|
|
|
let j = jsonAnd sa
|
|
|
|
if ra then first j <$> fb b else return (j $ fb_ b, ra)
|
|
|
|
|
2022-07-04 12:30:13 -04:00
|
|
|
dumpTree_ :: (d_ -> FIO Result_) -> (d_ -> DependencyData) -> Tree_ d_
|
|
|
|
-> FIO (JSONUnquotable, Bool)
|
2022-06-28 21:24:21 -04:00
|
|
|
dumpTree_ test_ dd_ = go
|
|
|
|
where
|
|
|
|
go (And_ a b) = do
|
|
|
|
(sa, ra) <- go a
|
|
|
|
let j = jsonAnd sa
|
|
|
|
if ra then first j <$> go b else return (j $ dataTree_ dd_ b, ra)
|
|
|
|
go (Or_ a b) = do
|
|
|
|
(sa, ra) <- go a
|
|
|
|
let j = jsonAnd sa
|
|
|
|
if ra then return (j $ dataTree_ dd_ b, ra) else first j <$> go b
|
|
|
|
go (Only_ d) = do
|
|
|
|
r <- fromResult_ <$> test_ d
|
|
|
|
let (x, y) = dd_ d
|
|
|
|
return (jsonLeaf (Just r) x y, fst r)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Dependency data for JSON
|
2022-06-26 19:05:25 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
type DependencyData = (JSONQuotable, [(String, JSONMixed)])
|
|
|
|
|
|
|
|
dataSubfeatureRoot :: SubfeatureRoot a -> JSONUnquotable
|
|
|
|
dataSubfeatureRoot Subfeature { sfData = r, sfName = n } =
|
|
|
|
jsonSubfeature (Q n) $ dataRoot r
|
|
|
|
|
|
|
|
dataRoot :: Root a -> JSONUnquotable
|
|
|
|
dataRoot (IORoot _ t) = dataTree dataIODependency dataIODependency_ t
|
|
|
|
dataRoot (IORoot_ _ t) = dataTree_ dataIODependency_ t
|
|
|
|
dataRoot (DBusRoot _ t _) = dataTree dataIODependency dataDBusDependency t
|
|
|
|
dataRoot (DBusRoot_ _ t _) = dataTree_ dataDBusDependency t
|
|
|
|
|
2022-07-01 23:15:44 -04:00
|
|
|
dataTree :: forall d d_ p. (forall q. d q -> DependencyData)
|
|
|
|
-> (d_ -> DependencyData) -> Tree d d_ p -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
dataTree f f_ = go
|
2022-06-26 19:05:25 -04:00
|
|
|
where
|
2022-07-01 23:15:44 -04:00
|
|
|
go :: forall q. Tree d d_ q -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
go (And12 _ a b) = jsonAnd (go a) (go b)
|
|
|
|
go (And1 a b) = jsonAnd (go a) (dataTree_ f_ b)
|
|
|
|
go (And2 a b) = jsonAnd (dataTree_ f_ a) (go b)
|
|
|
|
go (Or a b) = jsonOr (go a) (go b)
|
|
|
|
go (Only d) = uncurry jsonLeafUntested $ f d
|
|
|
|
|
|
|
|
dataTree_ :: (d_ -> DependencyData) -> Tree_ d_ -> JSONUnquotable
|
|
|
|
dataTree_ f_ = go
|
|
|
|
where
|
|
|
|
go (And_ a b) = jsonAnd (go a) (go b)
|
|
|
|
go (Or_ a b) = jsonOr (go a) (go b)
|
|
|
|
go (Only_ d) = uncurry jsonLeafUntested $ f_ d
|
|
|
|
|
|
|
|
dataIODependency :: IODependency p -> DependencyData
|
2022-07-02 17:09:21 -04:00
|
|
|
dataIODependency d = first Q $ case d of
|
2022-07-09 01:02:37 -04:00
|
|
|
(IORead n f _) -> ("ioread", [ ("desc", JSON_Q $ Q n)
|
2022-07-09 23:45:56 -04:00
|
|
|
, ("fulfillment", JSON_UQ
|
2022-07-09 01:02:37 -04:00
|
|
|
$ dataFulfillments f)
|
|
|
|
])
|
2022-07-08 18:57:12 -04:00
|
|
|
(IOConst _) -> ("const", [])
|
|
|
|
-- TODO what if this isn't required?
|
2022-07-09 01:02:37 -04:00
|
|
|
(IOSometimes (Sometimes n _ _) _) -> ("sometimes", [ ("name", JSON_Q $ Q n)])
|
2022-07-08 18:57:12 -04:00
|
|
|
(IOAlways (Always n _) _) -> ("always", [("name", JSON_Q $ Q n)])
|
2022-06-28 21:24:21 -04:00
|
|
|
|
|
|
|
dataIODependency_ :: IODependency_ -> DependencyData
|
|
|
|
dataIODependency_ d = case d of
|
2022-07-09 01:02:37 -04:00
|
|
|
(IOSystem_ f s) -> dataSysDependency f s
|
|
|
|
(IOSometimes_ _) -> (Q "sometimes", [])
|
|
|
|
(IOTest_ desc f _) -> (Q "iotest", [ ("desc", JSON_Q $ Q desc)
|
2022-07-09 23:45:56 -04:00
|
|
|
, ("fulfillment", JSON_UQ $ dataFulfillments f)
|
2022-07-09 01:02:37 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
dataSysDependency :: [Fulfillment] -> SystemDependency -> DependencyData
|
|
|
|
dataSysDependency f d = first Q $
|
2022-06-28 21:24:21 -04:00
|
|
|
case d of
|
2022-07-02 17:09:21 -04:00
|
|
|
(Executable sys path) -> ("executable", [ ("system", JSON_UQ $ jsonBool sys)
|
2022-06-28 21:24:21 -04:00
|
|
|
, ("path", JSON_Q $ Q path)
|
2022-07-09 01:02:37 -04:00
|
|
|
, f'
|
2022-06-28 21:24:21 -04:00
|
|
|
])
|
2022-07-02 17:09:21 -04:00
|
|
|
(AccessiblePath p r w) -> ("path", [ ("path", JSON_Q $ Q p)
|
|
|
|
, ("readable", JSON_UQ $ jsonBool r)
|
|
|
|
, ("writable", JSON_UQ $ jsonBool w)
|
2022-07-09 01:02:37 -04:00
|
|
|
, f'
|
2022-07-02 17:09:21 -04:00
|
|
|
])
|
|
|
|
(Systemd t n) -> ("systemd", [ ("unittype", JSON_Q $ Q $ unitType t)
|
2022-07-09 01:02:37 -04:00
|
|
|
, ("unit", JSON_Q $ Q n)
|
|
|
|
, f'
|
|
|
|
])
|
|
|
|
(Process n) -> ("process", [("name", JSON_Q $ Q n), f'])
|
|
|
|
where
|
2022-07-09 23:45:56 -04:00
|
|
|
f' = ("fulfillment", JSON_UQ $ dataFulfillments f)
|
2022-07-09 01:02:37 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
dataDBusDependency :: DBusDependency_ c -> DependencyData
|
2022-06-28 21:24:21 -04:00
|
|
|
dataDBusDependency d =
|
|
|
|
case d of
|
|
|
|
(DBusIO i) -> dataIODependency_ i
|
2022-07-09 01:02:37 -04:00
|
|
|
(Bus f b) -> (Q "bus", [ ("busname", JSON_Q $ Q $ formatBusName b)
|
2022-07-09 23:45:56 -04:00
|
|
|
, ("fulfillment", JSON_UQ $ dataFulfillments f)
|
2022-07-09 01:02:37 -04:00
|
|
|
])
|
|
|
|
(Endpoint f b o i m) -> let (mt, mn) = memberData m
|
2022-06-28 21:24:21 -04:00
|
|
|
in (Q "endpoint", [ ("busname", JSON_Q $ Q $ formatBusName b)
|
|
|
|
, ("objectpath", JSON_Q $ Q $ formatObjectPath o)
|
|
|
|
, ("interface", JSON_Q $ Q $ formatInterfaceName i)
|
|
|
|
, ("membertype", JSON_Q $ Q mt)
|
|
|
|
, ("membername", JSON_Q $ Q mn)
|
2022-07-09 23:45:56 -04:00
|
|
|
, ("fulfillment", JSON_UQ $ dataFulfillments f)
|
2022-06-28 21:24:21 -04:00
|
|
|
])
|
|
|
|
where
|
|
|
|
memberData (Method_ n) = ("method", formatMemberName n)
|
|
|
|
memberData (Signal_ n) = ("signal", formatMemberName n)
|
|
|
|
memberData (Property_ n) = ("property", n)
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
dataFulfillments :: [Fulfillment] -> JSONUnquotable
|
|
|
|
dataFulfillments = jsonArray . fmap (JSON_UQ . dataFulfillment)
|
|
|
|
|
|
|
|
dataFulfillment :: Fulfillment -> JSONUnquotable
|
|
|
|
dataFulfillment (Package a n) = jsonObject [ ("type", JSON_Q $ Q "package")
|
2022-07-09 14:59:42 -04:00
|
|
|
, ("type", JSON_Q $ Q $ show a)
|
2022-07-09 01:02:37 -04:00
|
|
|
, ("name", JSON_Q $ Q n)
|
|
|
|
]
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
fromMsg :: Msg -> JSONUnquotable
|
|
|
|
fromMsg (Msg e s) = jsonObject [ ("level", JSON_Q $ Q $ show e)
|
|
|
|
, ("msg", JSON_Q $ Q s)
|
|
|
|
]
|
|
|
|
|
|
|
|
fromResult :: Result a -> (Bool, [JSONUnquotable])
|
|
|
|
fromResult = second (fmap fromMsg) . either (False,) (\(PostPass _ ws) -> (True, ws))
|
2022-06-28 21:24:21 -04:00
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
fromResult_ :: Result_ -> (Bool, [JSONUnquotable])
|
|
|
|
fromResult_ = second (fmap fromMsg) . either (False,) (True,)
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-28 21:24:21 -04:00
|
|
|
-- | JSON formatting
|
2022-06-26 19:05:25 -04:00
|
|
|
--
|
2022-06-28 21:24:21 -04:00
|
|
|
-- I could use Aeson...but I don't feel like it (too many intermediate types)
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
newtype JSONQuotable = Q String
|
|
|
|
|
|
|
|
newtype JSONUnquotable = UQ String
|
|
|
|
|
|
|
|
data JSONMixed = JSON_UQ JSONUnquotable | JSON_Q JSONQuotable
|
|
|
|
|
2022-06-28 23:27:55 -04:00
|
|
|
jsonAlways :: JSONQuotable -> Maybe JSONUnquotable -> [JSONUnquotable]
|
|
|
|
-> [JSONUnquotable] -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
jsonAlways = jsonFeature True
|
|
|
|
|
2022-06-28 23:27:55 -04:00
|
|
|
jsonSometimes :: JSONQuotable -> Maybe JSONUnquotable -> [JSONUnquotable]
|
|
|
|
-> [JSONUnquotable] -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
jsonSometimes = jsonFeature False
|
|
|
|
|
2022-06-28 23:27:55 -04:00
|
|
|
jsonFeature :: Bool -> JSONQuotable -> Maybe JSONUnquotable -> [JSONUnquotable]
|
2022-06-28 21:24:21 -04:00
|
|
|
-> [JSONUnquotable] -> JSONUnquotable
|
2022-06-28 23:27:55 -04:00
|
|
|
jsonFeature isalways name success failed untested = jsonObject
|
2022-06-28 21:24:21 -04:00
|
|
|
[ ("type", JSON_Q $ Q $ if isalways then "always" else "sometimes")
|
2022-06-28 23:27:55 -04:00
|
|
|
, ("name", JSON_Q name)
|
2022-06-28 21:24:21 -04:00
|
|
|
, ("success", JSON_UQ $ fromMaybe (UQ "null") success)
|
|
|
|
, ("failed", JSON_UQ $ jsonArray $ fmap JSON_UQ failed)
|
|
|
|
, ("untested", JSON_UQ $ jsonArray $ fmap JSON_UQ untested)
|
|
|
|
]
|
|
|
|
|
|
|
|
jsonSubfeature :: JSONQuotable -> JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonSubfeature n r = jsonObject
|
|
|
|
[ ("name", JSON_Q n)
|
|
|
|
, ("root", JSON_UQ r)
|
|
|
|
]
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
jsonIORoot :: JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonIORoot = jsonRoot True
|
2022-06-22 01:28:46 -04:00
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
jsonDBusRoot :: JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonDBusRoot = jsonRoot False
|
|
|
|
|
|
|
|
jsonRoot :: Bool -> JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonRoot isIO tree = jsonObject
|
|
|
|
[ ("type", JSON_Q $ Q $ if isIO then "io" else "dbus")
|
|
|
|
, ("tree", JSON_UQ tree)
|
|
|
|
]
|
|
|
|
|
|
|
|
jsonLeafUntested :: JSONQuotable -> [(String, JSONMixed)] -> JSONUnquotable
|
|
|
|
jsonLeafUntested = jsonLeaf Nothing
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
jsonLeaf :: Maybe (Bool, [JSONUnquotable]) -> JSONQuotable -> [(String, JSONMixed)]
|
2022-06-28 21:24:21 -04:00
|
|
|
-> JSONUnquotable
|
|
|
|
jsonLeaf status deptype depdata = jsonObject
|
|
|
|
[ ("type", JSON_Q deptype)
|
|
|
|
, ("status", jsonMaybe (JSON_UQ . uncurry jsonStatus) status)
|
|
|
|
, ("data", JSON_UQ $ jsonObject depdata)
|
|
|
|
]
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
jsonStatus :: Bool -> [JSONUnquotable] -> JSONUnquotable
|
2022-06-28 21:24:21 -04:00
|
|
|
jsonStatus present messages = jsonObject
|
|
|
|
[ ("present", JSON_UQ $ jsonBool present)
|
2022-07-07 01:05:17 -04:00
|
|
|
, ("messages", JSON_UQ $ jsonArray $ fmap JSON_UQ messages)
|
2022-06-28 21:24:21 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
jsonAnd :: JSONUnquotable -> JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonAnd = jsonBranch True
|
|
|
|
|
|
|
|
jsonOr :: JSONUnquotable -> JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonOr = jsonBranch False
|
|
|
|
|
|
|
|
jsonBranch :: Bool -> JSONUnquotable -> JSONUnquotable -> JSONUnquotable
|
|
|
|
jsonBranch isAnd l r = jsonObject
|
|
|
|
[ ("test", JSON_Q $ Q $ if isAnd then "and" else "or")
|
|
|
|
, ("left", JSON_UQ l)
|
|
|
|
, ("right",JSON_UQ r)
|
|
|
|
]
|
|
|
|
|
|
|
|
jsonMaybe :: (a -> JSONMixed) -> Maybe a -> JSONMixed
|
|
|
|
jsonMaybe = maybe (JSON_UQ $ UQ "null")
|
|
|
|
|
|
|
|
jsonBool :: Bool -> JSONUnquotable
|
|
|
|
jsonBool True = UQ "true"
|
|
|
|
jsonBool False = UQ "false"
|
|
|
|
|
|
|
|
jsonArray :: [JSONMixed] -> JSONUnquotable
|
|
|
|
jsonArray = UQ . bracket . intercalate "," . fmap quoteMaybe
|
|
|
|
|
|
|
|
jsonObject :: [(String, JSONMixed)] -> JSONUnquotable
|
|
|
|
jsonObject = UQ . curly . intercalate ","
|
|
|
|
. fmap (\(k, v) -> doubleQuote k ++ ":" ++ quoteMaybe v)
|
|
|
|
|
|
|
|
quoteMaybe :: JSONMixed -> String
|
|
|
|
quoteMaybe (JSON_Q (Q s)) = doubleQuote s
|
|
|
|
quoteMaybe (JSON_UQ (UQ s)) = s
|
2022-06-26 19:05:25 -04:00
|
|
|
|
|
|
|
bracket :: String -> String
|
|
|
|
bracket s = "[" ++ s ++ "]"
|
|
|
|
|
2022-06-28 21:24:21 -04:00
|
|
|
curly :: String -> String
|
|
|
|
curly s = "{" ++ s ++ "}"
|