Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolverson committed Nov 29, 2019
0 parents commit f949140
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ebin/
output/
.psc-package/
.psc-ide-port
*.dump
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Nicholas Wolverson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: ps erl all test

all: ps erl

ps:
psc-package sources | xargs purs compile 'src/**/*.purs' 'test/**/*.purs'

test: ps erl
erl -pa ebin -noshell -eval '(test_main@ps:main())()' -eval 'init:stop()'

erl:
mkdir -p ebin
erlc -o ebin/ output/*/*.erl
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# purescript-erl-atom

Create Erlang atoms from Strings for use in accessing native libraries. ADTs compile down to tuples including atoms, but in many cases APIs require e.g. returning a specific atom, which we would otherwise have to do via FFI.
12 changes: 12 additions & 0 deletions psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "purescript-erl-file",
"set": "erl-0.13.2-20190808",
"source": "https://github.com/purerl/package-sets.git",
"depends": [
"assert",
"debug",
"erl-atom",
"erl-binary",
"prelude"
]
}
14 changes: 14 additions & 0 deletions src/Erl/File.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-module(erl_file@foreign).
-export([writeFile_/2, readFile_/1, deleteFile_/1]).

writeFile_(Filename, Data) -> fun () ->
file:write_file(Filename, Data)
end.

readFile_(Filename) -> fun () ->
file:read_file(Filename)
end.

deleteFile_(Filename) -> fun () ->
file:delete(Filename)
end.
32 changes: 32 additions & 0 deletions src/Erl/File.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Erl.File
( readFile
, writeFile
, deleteFile
, module StandardResult
) where

import Prelude

import Data.Either (Either)
import Effect (Effect)
import Erl.Data.Binary (Binary)
import Erl.Data.Binary.IOData (IOData)
import Erl.StandardResult (Reason, ReasonResult, standardResultToEither)
import Erl.StandardResult (Reason(..)) as StandardResult

foreign import writeFile_ :: String -> IOData -> Effect (ReasonResult Unit)

writeFile :: String -> IOData -> Effect (Either Reason Unit)
writeFile f x = standardResultToEither <$> writeFile_ f x


foreign import readFile_ :: String -> Effect (ReasonResult Binary)

readFile :: String -> Effect (Either Reason Binary)
readFile f = standardResultToEither <$> readFile_ f


foreign import deleteFile_ :: String -> Effect (ReasonResult Unit)

deleteFile :: String -> Effect (Either Reason Unit)
deleteFile f = standardResultToEither <$> deleteFile_ f
23 changes: 23 additions & 0 deletions src/Erl/FileLib.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-module(erl_fileLib@foreign).
-export([ensureDir_/1, fileSize/1, isDir/1, isFile/1, isRegular/1]).

ensureDir_(Name) -> fun () ->
filelib:ensure_dir(Name)
end.

fileSize(Name) -> fun () ->
filelib:file_size(Name)
end.

isDir(Name) -> fun () ->
filelib:is_dir(Name)
end.

isFile(Name) -> fun() ->
filelib:is_file(Name)
end.

isRegular(Name) -> fun() ->
filelib:is_regular(Name)
end.

25 changes: 25 additions & 0 deletions src/Erl/FileLib.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Erl.FileLib
( ensureDir
, module StandardResult
)
where

import Prelude

import Data.Either (Either)
import Effect (Effect)
import Erl.StandardResult (Reason, ReasonResult, standardResultToEither)
import Erl.StandardResult (Reason(..)) as StandardResult

foreign import ensureDir_ :: String -> Effect (ReasonResult Unit)

ensureDir :: String -> Effect (Either Reason Unit)
ensureDir f = standardResultToEither <$> ensureDir_ f

foreign import fileSize :: String -> Effect Int

foreign import isDir :: String -> Effect Boolean

foreign import isFile :: String -> Effect Boolean

foreign import isRegular :: String -> Effect Boolean
12 changes: 12 additions & 0 deletions src/Erl/StandardResult.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-module(erl_standardResult@foreign).
-export([standardResultToEither_/3]).

standardResultToEither_(Left, Right, Result) ->
case Result of
error -> Left(unit);
{error, X} -> Left(X);
Z when is_tuple(Z), element(1, Z) =:= error -> Left(erlang:delete_element(1, Z));
ok -> Right(unit);
{ok, X} -> Right(X);
Z when is_tuple(Z), element(1, Z) =:= ok -> Right(erlang:delete_element(1, Z))
end.
23 changes: 23 additions & 0 deletions src/Erl/StandardResult.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Erl.StandardResult
( Reason(..)
, StandardResult
, ReasonResult
, standardResultToEither
) where

import Prelude

import Data.Either (Either(..))
import Erl.Atom (Atom)

newtype Reason = Reason Atom
derive newtype instance eqReason :: Eq Reason
derive newtype instance showReason :: Show Reason

foreign import data StandardResult :: Type -> Type -> Type
type ReasonResult a = StandardResult Reason a

foreign import standardResultToEither_ :: forall a b. (a -> Either a b) -> (b -> Either a b) -> StandardResult a b -> Either a b

standardResultToEither :: forall a. StandardResult Reason a -> Either Reason a
standardResultToEither = standardResultToEither_ Left Right
41 changes: 41 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Test.Main where

import Debug.Trace
import Prelude

import Data.Either (Either(..))
import Effect (Effect)
import Effect.Class.Console (log)
import Erl.Atom (atom)
import Erl.Data.Binary (Binary)
import Erl.Data.Binary.IOData (IOData)
import Erl.Data.Binary.IOData as IOData
import Erl.Data.Binary.UTF8 as UTF8String
import Erl.File (Reason(..), deleteFile, readFile, writeFile)
import Test.Assert (assert, assert', assertEqual)

testBinary :: Binary
testBinary = UTF8String.toBinary $ "Testing testing 123\nhello\n"

testData :: IOData
testData = IOData.fromBinary $ testBinary

main :: Effect Unit
main = do
let tmpFile = "/tmp/erl-file-test-test-file.txt"

log "writeFile"
res <- writeFile tmpFile testData
assertEqual { actual: res, expected: Right unit }

log "readFile"
res1 <- readFile tmpFile
assert $ res1 == Right testBinary

res2 <- readFile "/tmp/junk/this-doesnt-exist"
assert $ res2 == (Left $ Reason $ atom "enoent")

log "deleteFile"
res3 <- deleteFile tmpFile
assert $ res3 == Right unit

0 comments on commit f949140

Please sign in to comment.