-- | Small wrapper over @'System.FilePath.Glob.Pattern'@
module Restyler.Config.Glob
    ( Glob
    , match
    )
where

import Restyler.Prelude

import Data.Aeson
import System.FilePath.Glob hiding (match)
import qualified System.FilePath.Glob as Glob

newtype Glob = Glob { Glob -> Pattern
unGlob :: Pattern }
    deriving stock (Glob -> Glob -> Bool
(Glob -> Glob -> Bool) -> (Glob -> Glob -> Bool) -> Eq Glob
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Glob -> Glob -> Bool
$c/= :: Glob -> Glob -> Bool
== :: Glob -> Glob -> Bool
$c== :: Glob -> Glob -> Bool
Eq, (forall x. Glob -> Rep Glob x)
-> (forall x. Rep Glob x -> Glob) -> Generic Glob
forall x. Rep Glob x -> Glob
forall x. Glob -> Rep Glob x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Glob x -> Glob
$cfrom :: forall x. Glob -> Rep Glob x
Generic)
    deriving newtype Int -> Glob -> ShowS
[Glob] -> ShowS
Glob -> String
(Int -> Glob -> ShowS)
-> (Glob -> String) -> ([Glob] -> ShowS) -> Show Glob
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Glob] -> ShowS
$cshowList :: [Glob] -> ShowS
show :: Glob -> String
$cshow :: Glob -> String
showsPrec :: Int -> Glob -> ShowS
$cshowsPrec :: Int -> Glob -> ShowS
Show

instance FromJSON Glob where
    parseJSON :: Value -> Parser Glob
parseJSON = String -> (Text -> Parser Glob) -> Value -> Parser Glob
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText "Glob" ((Text -> Parser Glob) -> Value -> Parser Glob)
-> (Text -> Parser Glob) -> Value -> Parser Glob
forall a b. (a -> b) -> a -> b
$ Glob -> Parser Glob
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Glob -> Parser Glob) -> (Text -> Glob) -> Text -> Parser Glob
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pattern -> Glob
Glob (Pattern -> Glob) -> (Text -> Pattern) -> Text -> Glob
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Pattern
compile (String -> Pattern) -> (Text -> String) -> Text -> Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack

instance ToJSON Glob where
    toJSON :: Glob -> Value
toJSON = Text -> Value
String (Text -> Value) -> (Glob -> Text) -> Glob -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack (String -> Text) -> (Glob -> String) -> Glob -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pattern -> String
decompile (Pattern -> String) -> (Glob -> Pattern) -> Glob -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Glob -> Pattern
unGlob

match :: Glob -> FilePath -> Bool
match :: Glob -> String -> Bool
match (Glob p :: Pattern
p) = Pattern -> String -> Bool
Glob.match Pattern
p