Skip to content

Commit

Permalink
[temporary] More specific FileSupport type
Browse files Browse the repository at this point in the history
  • Loading branch information
aeqz committed Jan 13, 2023
1 parent 9ea450f commit bdda616
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
18 changes: 14 additions & 4 deletions src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ data ExclusionConfig' f = ExclusionConfig

makeLensesWith postfixFields ''ExclusionConfig'

-- | File extension, dot included.
type Extension = String

-- | Whether the file is a symlink.
type IsSymlink = Bool

-- | Way to parse a file.
type ScanAction = CanonicalPath -> IO (FileInfo, [ScanError 'Parse])

-- | All supported ways to parse a file.
type FileSupport = CanonicalPath -> IO $ Maybe ScanAction
type FileSupport = IsSymlink -> Extension -> Maybe ScanAction

data ScanResult = ScanResult
{ srScanErrors :: [ScanError 'Gather]
Expand Down Expand Up @@ -218,7 +224,7 @@ scanRepo scanMode rw formatsSupport config root = do
IncludeUntracked -> pure []

scannableNotProcessedFiles <- liftIO $
filterM (fmap isJust . formatsSupport . fst) notProcessedFiles
filterM (fmap isJust . fileScanner . fst) notProcessedFiles

whenJust (nonEmpty $ map fst scannableNotProcessedFiles) $ \files -> hPutStrLn @Text stderr
[int|A|
Expand All @@ -238,6 +244,10 @@ scanRepo scanMode rw formatsSupport config root = do
, riRoot = canonicalRoot
}
where
fileScanner :: CanonicalPath -> IO (Maybe ScanAction)
fileScanner canonicalFile = do
isSymlink <- pathIsSymbolicLink canonicalFile
pure $ formatsSupport isSymlink $ takeExtension canonicalFile

gatherScanErrs
:: CanonicalPath
Expand All @@ -255,8 +265,8 @@ scanRepo scanMode rw formatsSupport config root = do

processFile :: CanonicalPath -> IO (FileStatus, [ScanError 'Parse])
processFile canonicalFile = do
action <- formatsSupport canonicalFile
case action of
mScanner <- fileScanner canonicalFile
case mScanner of
Nothing -> pure (NotScannable, [])
Just scanner -> scanner canonicalFile <&> _1 %~ Scanned

Expand Down
10 changes: 4 additions & 6 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,7 @@ markdownScanner config canonicalFile =
<$> BSL.readFile (unCanonicalPath canonicalFile)

markdownSupport :: MarkdownConfig -> FileSupport
markdownSupport config path =
if takeExtension path /= ".md"
then pure Nothing
else ifM (pathIsSymbolicLink path)
(pure Nothing)
(pure (Just $ markdownScanner config))
markdownSupport config isSymlink extension = do
guard $ extension == ".md"
guard $ not isSymlink
pure $ markdownScanner config
7 changes: 3 additions & 4 deletions src/Xrefcheck/Scanners/Symlink.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ symlinkScanner path = do
pure (FileInfo [Reference {rName, rLink, rAnchor, rPos, rInfo, rIsSymlink}] [], [])

symlinkSupport :: FileSupport
symlinkSupport path =
ifM (pathIsSymbolicLink path)
(pure (Just symlinkScanner))
(pure Nothing)
symlinkSupport isSymlink _ = do
guard isSymlink
pure symlinkScanner

0 comments on commit bdda616

Please sign in to comment.