dev: PrefixedFilePath cleanups

And some helpers that weren't needed after all, but maybe in future
This commit is contained in:
Simon Michael 2025-07-14 11:31:36 -07:00
parent 42b9c8b0fb
commit 1046f652b1
2 changed files with 37 additions and 15 deletions

View File

@ -672,6 +672,8 @@ data SepFormat
| Ssv -- semicolon-separated | Ssv -- semicolon-separated
deriving (Eq, Ord) deriving (Eq, Ord)
-- XXX A little confusion, this is also used to name readers in splitReaderPrefix.
-- readers, input formats, and output formats overlap but are distinct concepts.
-- | The id of a data format understood by hledger, eg @journal@ or @csv@. -- | The id of a data format understood by hledger, eg @journal@ or @csv@.
-- The --output-format option selects one of these for output. -- The --output-format option selects one of these for output.
data StorageFormat data StorageFormat

View File

@ -167,27 +167,47 @@ findReader Nothing (Just path) =
(prefix,path') = splitReaderPrefix path (prefix,path') = splitReaderPrefix path
ext = map toLower $ drop 1 $ takeExtension path' ext = map toLower $ drop 1 $ takeExtension path'
-- | A file path optionally prefixed by a reader name and colon -- | A prefix used to specify a particular reader to be used for a file path,
-- (journal:, csv:, timedot:, etc.). -- overriding the file extension. It is a valid reader name followed by a colon.
-- Eg journal:, csv:, timeclock:, timedot:.
-- type ReaderPrefix = String
-- | A file path with an optional reader prefix.
type PrefixedFilePath = FilePath type PrefixedFilePath = FilePath
-- | If a filepath is prefixed by one of the reader names and a colon, -- | Separate a file path and its reader prefix, if any.
-- split that off. Eg "csv:-" -> (Just "csv", "-"). --
-- These reader prefixes can be used to force a specific reader, -- >>> splitReaderPrefix "csv:-"
-- overriding the file extension. -- (Just csv,"-")
splitReaderPrefix :: PrefixedFilePath -> (Maybe StorageFormat, FilePath) splitReaderPrefix :: PrefixedFilePath -> (Maybe StorageFormat, FilePath)
splitReaderPrefix f = splitReaderPrefix f =
let let
candidates = [(Just r, drop (length r + 1) f) | r <- readerNames ++ ["ssv","tsv"], (r++":") `isPrefixOf` f] candidates = [(Just r, drop (length r + 1) f) | r <- readerNames ++ ["ssv","tsv"], (r++":") `isPrefixOf` f]
(strPrefix, newF) = headDef (Nothing, f) candidates (strPrefix, newF) = headDef (Nothing, f) candidates
in case strPrefix of in case strPrefix of
Just "csv" -> (Just (Sep Csv), newF) Just "csv" -> (Just (Sep Csv), newF)
Just "tsv" -> (Just (Sep Tsv), newF) Just "tsv" -> (Just (Sep Tsv), newF)
Just "ssv" -> (Just (Sep Ssv), newF) Just "ssv" -> (Just (Sep Ssv), newF)
Just "journal" -> (Just Journal', newF) Just "journal" -> (Just Journal', newF)
Just "timeclock" -> (Just Timeclock, newF) Just "timeclock" -> (Just Timeclock, newF)
Just "timedot" -> (Just Timedot, newF) Just "timedot" -> (Just Timedot, newF)
_ -> (Nothing, f) _ -> (Nothing, f)
-- -- | Does this file path have a reader prefix ?
-- hasReaderPrefix :: PrefixedFilePath -> Bool
-- hasReaderPrefix = isJust . fst. splitReaderPrefix
-- -- | Add a reader prefix to a file path, unless it already has one.
-- -- The argument should be a valid reader name.
-- --
-- -- >>> addReaderPrefix "csv" "a.txt"
-- -- >>> "csv:a.txt"
-- -- >>> addReaderPrefix "csv" "timedot:a.txt"
-- -- >>> "timedot:a.txt"
-- addReaderPrefix :: ReaderPrefix -> FilePath -> PrefixedFilePath
-- addReaderPrefix readername f
-- | hasReaderPrefix f = f
-- | otherwise = readername <> ":" <> f
--- ** reader --- ** reader