2020-05-18 15:59:07 +03:00
|
|
|
module System.Directory
|
|
|
|
|
|
|
|
import public System.File
|
|
|
|
|
|
|
|
public export
|
|
|
|
DirPtr : Type
|
|
|
|
DirPtr = AnyPtr
|
|
|
|
|
|
|
|
support : String -> String
|
|
|
|
support fn = "C:" ++ fn ++ ", libidris2_support"
|
|
|
|
|
|
|
|
%foreign support "idris2_fileErrno"
|
2020-07-06 18:58:02 +03:00
|
|
|
"node:support:fileErrno,support_system_directory"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_fileErrno : PrimIO Int
|
|
|
|
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
returnError : HasIO io => io (Either FileError a)
|
2020-05-18 15:59:07 +03:00
|
|
|
returnError
|
|
|
|
= do err <- primIO prim_fileErrno
|
|
|
|
case err of
|
|
|
|
0 => pure $ Left FileReadError
|
|
|
|
1 => pure $ Left FileWriteError
|
|
|
|
2 => pure $ Left FileNotFound
|
|
|
|
3 => pure $ Left PermissionDenied
|
|
|
|
4 => pure $ Left FileExists
|
|
|
|
_ => pure $ Left (GenericFileError (err-5))
|
|
|
|
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
ok : HasIO io => a -> io (Either FileError a)
|
2020-05-18 15:59:07 +03:00
|
|
|
ok x = pure (Right x)
|
|
|
|
|
|
|
|
%foreign support "idris2_currentDirectory"
|
2020-06-18 01:29:54 +03:00
|
|
|
"node:lambda:()=>process.cwd()"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_currentDir : PrimIO (Ptr String)
|
|
|
|
|
|
|
|
%foreign support "idris2_changeDir"
|
2020-07-06 18:58:02 +03:00
|
|
|
"node:support:changeDir,support_system_directory"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_changeDir : String -> PrimIO Int
|
|
|
|
|
|
|
|
%foreign support "idris2_createDir"
|
2020-07-06 18:58:02 +03:00
|
|
|
"node:support:createDir,support_system_directory"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_createDir : String -> PrimIO Int
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
%foreign support "idris2_openDir"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_openDir : String -> PrimIO DirPtr
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
%foreign support "idris2_closeDir"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim_closeDir : DirPtr -> PrimIO ()
|
|
|
|
|
2020-05-21 15:32:35 +03:00
|
|
|
%foreign support "idris2_removeDir"
|
|
|
|
prim_removeDir : String -> PrimIO ()
|
2020-05-18 20:28:33 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
%foreign support "idris2_nextDirEntry"
|
|
|
|
prim_dirEntry : DirPtr -> PrimIO (Ptr String)
|
|
|
|
|
|
|
|
export
|
|
|
|
data Directory : Type where
|
|
|
|
MkDir : DirPtr -> Directory
|
|
|
|
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
createDir : HasIO io => String -> io (Either FileError ())
|
2020-05-18 15:59:07 +03:00
|
|
|
createDir dir
|
|
|
|
= do res <- primIO (prim_createDir dir)
|
|
|
|
if res == 0
|
|
|
|
then ok ()
|
|
|
|
else returnError
|
|
|
|
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
changeDir : HasIO io => String -> io Bool
|
2020-05-18 15:59:07 +03:00
|
|
|
changeDir dir
|
|
|
|
= do ok <- primIO (prim_changeDir dir)
|
|
|
|
pure (ok == 0)
|
|
|
|
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
currentDir : HasIO io => io (Maybe String)
|
2020-05-18 15:59:07 +03:00
|
|
|
currentDir
|
|
|
|
= do res <- primIO prim_currentDir
|
|
|
|
if prim__nullPtr res /= 0
|
|
|
|
then pure Nothing
|
|
|
|
else pure (Just (prim__getString res))
|
|
|
|
|
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
openDir : HasIO io => String -> io (Either FileError Directory)
|
2020-05-21 15:32:35 +03:00
|
|
|
openDir d
|
2020-05-18 15:59:07 +03:00
|
|
|
= do res <- primIO (prim_openDir d)
|
|
|
|
if prim__nullAnyPtr res /= 0
|
|
|
|
then returnError
|
|
|
|
else ok (MkDir res)
|
|
|
|
|
|
|
|
export
|
2020-06-21 03:18:43 +03:00
|
|
|
closeDir : HasIO io => Directory -> io ()
|
2020-05-21 15:32:35 +03:00
|
|
|
closeDir (MkDir d) = primIO (prim_closeDir d)
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-05-18 20:28:33 +03:00
|
|
|
export
|
2020-06-21 03:18:43 +03:00
|
|
|
removeDir : HasIO io => String -> io ()
|
2020-05-21 15:32:35 +03:00
|
|
|
removeDir dirName = primIO (prim_removeDir dirName)
|
2020-05-18 20:28:33 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
Back to HasIO, remove MonadIO
Following a fairly detailed discussion on slack, the feeling is
generally that it's better to have a single interface. While precision
is nice, it doesn't appear to buy us anything here. If that turns out to
be wrong, or limiting somehow, we can revisit it later. Also:
- it's easier for backend authors if the type of IO operations is
slightly less restrictive. For example, if it's in HasIO, that limits
alternative implementations, which might be awkward for some
alternative back ends.
- it's one less extra detail to learn. This is minor, but there needs to
be a clear advantage if there's more detail to learn.
- It is difficult to think of an underlying type that can't have a Monad
instance (I have personally never encountered one - if they turns out
to exist, again, we can revisit!)
2020-06-21 21:21:22 +03:00
|
|
|
dirEntry : HasIO io => Directory -> io (Either FileError String)
|
2020-05-18 15:59:07 +03:00
|
|
|
dirEntry (MkDir d)
|
|
|
|
= do res <- primIO (prim_dirEntry d)
|
|
|
|
if prim__nullPtr res /= 0
|
|
|
|
then returnError
|
|
|
|
else ok (prim__getString res)
|