start converting from System.Directory to System.OsPath

This is the start of a long road, got the first few files to compile
after this large change.

Sponsored-by: mycroft
This commit is contained in:
Joey Hess 2025-01-23 10:22:06 -04:00
parent d46504e51e
commit 05bdce328d
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
8 changed files with 185 additions and 47 deletions

View file

@ -7,51 +7,68 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
module Utility.OsPath (
OsPath,
OsString,
literalOsPath,
toOsPath,
fromOsPath,
) where
import Utility.FileSystemEncoding
#ifdef WITH_OSPATH
import System.OsPath
import "os-string" System.OsString.Internal.Types
import qualified Data.ByteString.Short as S
#else
import qualified Data.ByteString as S
#endif
class OsPathConv t where
toOsPath :: t -> OsPath
fromOsPath :: OsPath -> t
instance OsPathConv FilePath where
toOsPath = toOsPath . toRawFilePath
fromOsPath = fromRawFilePath . fromOsPath
{- Used for string constants. -}
literalOsPath :: String -> OsPath
literalOsPath = toOsPath
#ifdef WITH_OSPATH
instance OsPathConv RawFilePath where
toOsPath = bytesToOsPath
fromOsPath = bytesFromOsPath
{- Unlike System.OsString.fromBytes, on Windows this does not ensure a
- valid USC-2LE encoding. The input ByteString must be in a valid encoding
- already or uses of the OsPath will fail. -}
toOsPath :: RawFilePath -> OsPath
bytesToOsPath :: RawFilePath -> OsPath
#if defined(mingw32_HOST_OS)
toOsPath = OsString . WindowsString . S.toShort
bytesToOsPath = OsString . WindowsString . S.toShort
#else
toOsPath = OsString . PosixString . S.toShort
bytesToOsPath = OsString . PosixString . S.toShort
#endif
fromOsPath :: OsPath -> RawFilePath
bytesFromOsPath :: OsPath -> RawFilePath
#if defined(mingw32_HOST_OS)
fromOsPath = S.fromShort . getWindowsString . getOsString
bytesFromOsPath = S.fromShort . getWindowsString . getOsString
#else
fromOsPath = S.fromShort . getPosixString . getOsString
bytesFromOsPath = S.fromShort . getPosixString . getOsString
#endif
#else
{- When not building with WITH_OSPATH, use FilePath. This allows
- using functions from legacy FilePath libraries interchangeably with
- newer OsPath libraries.
{- When not building with WITH_OSPATH, use RawFilePath.
-}
type OsPath = FilePath
type OsPath = RawFilePath
type OsString = String
type OsString = S.ByteString
toOsPath :: RawFilePath -> OsPath
toOsPath = fromRawFilePath
fromOsPath :: OsPath -> RawFilePath
fromOsPath = toRawFilePath
instance OsPathConv RawFilePath where
toOsPath = id
fromOsPath = id
#endif