rename Format to StorageFormat
This commit is contained in:
parent
dd2f293094
commit
7291977e6f
@ -169,13 +169,13 @@ data Journal = Journal {
|
|||||||
type JournalUpdate = ErrorT String IO (Journal -> Journal)
|
type JournalUpdate = ErrorT String IO (Journal -> Journal)
|
||||||
|
|
||||||
-- | 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@.
|
||||||
type Format = String
|
type StorageFormat = String
|
||||||
|
|
||||||
-- | A hledger journal reader is a triple of format name, format-detecting
|
-- | A hledger journal reader is a triple of format name, format-detecting
|
||||||
-- predicate, and a parser to Journal.
|
-- predicate, and a parser to Journal.
|
||||||
data Reader = Reader {
|
data Reader = Reader {
|
||||||
-- name of the format this reader handles
|
-- name of the format this reader handles
|
||||||
rFormat :: Format
|
rFormat :: StorageFormat
|
||||||
-- quickly check if this reader can probably handle the given file path and file content
|
-- quickly check if this reader can probably handle the given file path and file content
|
||||||
,rDetector :: FilePath -> String -> Bool
|
,rDetector :: FilePath -> String -> Bool
|
||||||
-- parse the given string, using the given parse rules file if any, returning a journal or error aware of the given file path
|
-- parse the given string, using the given parse rules file if any, returning a journal or error aware of the given file path
|
||||||
|
|||||||
@ -114,7 +114,7 @@ tests_readJournal' = [
|
|||||||
-- - otherwise, try them all.
|
-- - otherwise, try them all.
|
||||||
--
|
--
|
||||||
-- A CSV conversion rules file may also be specified for use by the CSV reader.
|
-- A CSV conversion rules file may also be specified for use by the CSV reader.
|
||||||
readJournal :: Maybe Format -> Maybe FilePath -> Maybe FilePath -> String -> IO (Either String Journal)
|
readJournal :: Maybe StorageFormat -> Maybe FilePath -> Maybe FilePath -> String -> IO (Either String Journal)
|
||||||
readJournal format rulesfile path s =
|
readJournal format rulesfile path s =
|
||||||
-- trace (show (format, rulesfile, path)) $
|
-- trace (show (format, rulesfile, path)) $
|
||||||
tryReaders $ readersFor (format, path, s)
|
tryReaders $ readersFor (format, path, s)
|
||||||
@ -134,19 +134,19 @@ readJournal format rulesfile path s =
|
|||||||
path' = fromMaybe "(string)" path
|
path' = fromMaybe "(string)" path
|
||||||
|
|
||||||
-- | Which readers are worth trying for this (possibly unspecified) format, filepath, and data ?
|
-- | Which readers are worth trying for this (possibly unspecified) format, filepath, and data ?
|
||||||
readersFor :: (Maybe Format, Maybe FilePath, String) -> [Reader]
|
readersFor :: (Maybe StorageFormat, Maybe FilePath, String) -> [Reader]
|
||||||
readersFor (format,path,s) =
|
readersFor (format,path,s) =
|
||||||
case format of
|
case format of
|
||||||
Just f -> case readerForFormat f of Just r -> [r]
|
Just f -> case readerForStorageFormat f of Just r -> [r]
|
||||||
Nothing -> []
|
Nothing -> []
|
||||||
Nothing -> case path of Nothing -> readers
|
Nothing -> case path of Nothing -> readers
|
||||||
Just "-" -> readers
|
Just "-" -> readers
|
||||||
Just p -> case readersForPathAndData (p,s) of [] -> readers
|
Just p -> case readersForPathAndData (p,s) of [] -> readers
|
||||||
rs -> rs
|
rs -> rs
|
||||||
|
|
||||||
-- | Find the (first) reader which can handle the given format, if any.
|
-- | Find the (first) reader which can handle the given format, if any.
|
||||||
readerForFormat :: Format -> Maybe Reader
|
readerForStorageFormat :: StorageFormat -> Maybe Reader
|
||||||
readerForFormat s | null rs = Nothing
|
readerForStorageFormat s | null rs = Nothing
|
||||||
| otherwise = Just $ head rs
|
| otherwise = Just $ head rs
|
||||||
where
|
where
|
||||||
rs = filter ((s==).rFormat) readers :: [Reader]
|
rs = filter ((s==).rFormat) readers :: [Reader]
|
||||||
@ -159,7 +159,7 @@ readersForPathAndData (f,s) = filter (\r -> (rDetector r) f s) readers
|
|||||||
-- an error message, using the specified data format or trying all known
|
-- an error message, using the specified data format or trying all known
|
||||||
-- formats. A CSV conversion rules file may be specified for better
|
-- formats. A CSV conversion rules file may be specified for better
|
||||||
-- conversion of that format.
|
-- conversion of that format.
|
||||||
readJournalFile :: Maybe Format -> Maybe FilePath -> FilePath -> IO (Either String Journal)
|
readJournalFile :: Maybe StorageFormat -> Maybe FilePath -> FilePath -> IO (Either String Journal)
|
||||||
readJournalFile format rulesfile "-" = do
|
readJournalFile format rulesfile "-" = do
|
||||||
hSetNewlineMode stdin universalNewlineMode
|
hSetNewlineMode stdin universalNewlineMode
|
||||||
getContents >>= readJournal format rulesfile (Just "(stdin)")
|
getContents >>= readJournal format rulesfile (Just "(stdin)")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user