Skip to content

Commit f8aac32

Browse files
Copilottek
authored andcommitted
Add System.OsPath.Extra to buck-worker-types
1 parent f6adeed commit f8aac32

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

ghc-server/lib/GhcServer/Path.hs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
-- | Path utilities shared across GHC server modules.
2-
module GhcServer.Path where
2+
module GhcServer.Path (fromOsPath, toOsPath, outputDirName, tmpDirName, socketDirName, socketPath) where
33

4-
import Control.Exception (Exception, SomeException, throw)
5-
import System.OsPath (OsPath, decodeUtf, encodeUtf, (</>))
6-
7-
data PathDecodingException = PathDecodingException OsPath SomeException
8-
deriving stock (Show)
9-
10-
instance Exception PathDecodingException where
11-
12-
data PathEncodingException = PathEncodingException FilePath SomeException
13-
deriving stock (Show)
14-
15-
instance Exception PathEncodingException where
16-
17-
fromOsPath :: OsPath -> FilePath
18-
fromOsPath p = either (throw . PathDecodingException p) id (decodeUtf p)
19-
20-
toOsPath :: String -> OsPath
21-
toOsPath p = either (throw . PathEncodingException p) id (encodeUtf p)
4+
import System.OsPath (OsPath, (</>))
5+
import System.OsPath.Extra (fromOsPath, toOsPath)
226

237
-- | Directory names under the project root for server artifacts.
248
outputDirName, tmpDirName, socketDirName :: OsPath

types/buck-worker-types.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ library
5050
Types.State.Oneshot
5151
Types.State.Stats
5252
Types.Target
53+
System.OsPath.Extra
5354
build-depends:
5455
aeson,
5556
base ==4.*,

types/src/System/OsPath/Extra.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module System.OsPath.Extra where
2+
3+
import Control.Exception (Exception, SomeException, throw)
4+
import System.OsPath (OsPath, decodeUtf, encodeUtf)
5+
6+
data OsPathDecodingException = OsPathDecodingException OsPath SomeException
7+
deriving stock (Show)
8+
9+
instance Exception OsPathDecodingException where
10+
11+
data OsPathEncodingException = OsPathEncodingException String SomeException
12+
deriving stock (Show)
13+
14+
instance Exception OsPathEncodingException where
15+
16+
-- | Like 'decodeUtf' but throws an exception instead of returning an Either
17+
-- and the exception provides the filepath as context.
18+
fromOsPath :: OsPath -> String
19+
fromOsPath p = either (throw . OsPathDecodingException p) id (decodeUtf p)
20+
21+
-- | Like 'encodeUtf' but throws an exception instead of returning an Either
22+
-- and the exception provides the filepath as context.
23+
toOsPath :: String -> OsPath
24+
toOsPath p = either (throw . OsPathEncodingException p) id (encodeUtf p)

0 commit comments

Comments
 (0)