diff options
author | Eduardo Julian | 2017-12-04 20:47:29 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-12-04 20:47:29 -0400 |
commit | f4c5f6ec0ad286f55a4c9e36677c321e99eefcbe (patch) | |
tree | 0ba7b268cc03eb67cf65b38d823b8eb8a10a71c9 | |
parent | 077e8286b1168909d702ae0c28a0d2941c956f15 (diff) |
- lux/world/file now works with Process, instead of Task.
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/world/file.lux | 80 | ||||
-rw-r--r-- | stdlib/test/test/lux/world/file.lux | 178 |
2 files changed, 133 insertions, 125 deletions
diff --git a/stdlib/source/lux/world/file.lux b/stdlib/source/lux/world/file.lux index 5fa4e1661..b33cf9540 100644 --- a/stdlib/source/lux/world/file.lux +++ b/stdlib/source/lux/world/file.lux @@ -2,14 +2,12 @@ lux (lux (control [monad #+ do] ["ex" exception #+ exception:]) - (concurrency ["P" promise] - ["T" task]) (data ["E" error] (coll [array])) (time ["i" instant] ["d" duration]) (world [blob #+ Blob]) - [io] + [io #+ Process] [host])) (exception: Could-Not-Read-All-Data) @@ -51,47 +49,47 @@ (do-template [<name> <flag>] [(def: #export (<name> data file) - (-> Blob File (T.Task Unit)) - (P.future (do (E.ErrorT io.Monad<IO>) - [stream (FileOutputStream::new [(java/io/File::new file) <flag>]) - _ (OutputStream::write [data] stream) - _ (OutputStream::flush [] stream)] - (AutoCloseable::close [] stream))))] + (-> Blob File (Process Unit)) + (do (E.ErrorT io.Monad<IO>) + [stream (FileOutputStream::new [(java/io/File::new file) <flag>]) + _ (OutputStream::write [data] stream) + _ (OutputStream::flush [] stream)] + (AutoCloseable::close [] stream)))] [append true] [write false] ) (def: #export (read file) - (-> File (T.Task Blob)) - (P.future (do (E.ErrorT io.Monad<IO>) - [#let [file' (java/io/File::new file)] - size (java/io/File::length [] file') - #let [data (blob.create (int-to-nat size))] - stream (FileInputStream::new [file']) - bytes-read (InputStream::read [data] stream) - _ (AutoCloseable::close [] stream)] - (if (i/= size bytes-read) - (wrap data) - (io.io (ex.throw Could-Not-Read-All-Data file)))))) + (-> File (Process Blob)) + (do (E.ErrorT io.Monad<IO>) + [#let [file' (java/io/File::new file)] + size (java/io/File::length [] file') + #let [data (blob.create (int-to-nat size))] + stream (FileInputStream::new [file']) + bytes-read (InputStream::read [data] stream) + _ (AutoCloseable::close [] stream)] + (if (i/= size bytes-read) + (wrap data) + (io.io (ex.throw Could-Not-Read-All-Data file))))) (def: #export (size file) - (-> File (T.Task Nat)) - (P.future (do (E.ErrorT io.Monad<IO>) - [size (java/io/File::length [] (java/io/File::new file))] - (wrap (int-to-nat size))))) + (-> File (Process Nat)) + (do (E.ErrorT io.Monad<IO>) + [size (java/io/File::length [] (java/io/File::new file))] + (wrap (int-to-nat size)))) (def: #export (files dir) - (-> File (T.Task (List File))) - (P.future (do (E.ErrorT io.Monad<IO>) - [files (java/io/File::listFiles [] (java/io/File::new dir))] - (monad.map @ (java/io/File::getAbsolutePath []) - (array.to-list files))))) + (-> File (Process (List File))) + (do (E.ErrorT io.Monad<IO>) + [files (java/io/File::listFiles [] (java/io/File::new dir))] + (monad.map @ (java/io/File::getAbsolutePath []) + (array.to-list files)))) (do-template [<name> <method>] [(def: #export (<name> file) - (-> File (T.Task Bool)) - (P.future (<method> [] (java/io/File::new file))))] + (-> File (Process Bool)) + (<method> [] (java/io/File::new file)))] [exists? java/io/File::exists] [make-dir java/io/File::mkdir] @@ -104,17 +102,17 @@ ) (def: #export (move target source) - (-> File File (T.Task Bool)) - (P.future (java/io/File::renameTo [(java/io/File::new target)] - (java/io/File::new source)))) + (-> File File (Process Bool)) + (java/io/File::renameTo [(java/io/File::new target)] + (java/io/File::new source))) (def: #export (get-last-modified file) - (-> File (T.Task i.Instant)) - (P.future (do (E.ErrorT io.Monad<IO>) - [millis (java/io/File::lastModified [] (java/io/File::new file))] - (wrap (|> millis d.from-millis i.absolute))))) + (-> File (Process i.Instant)) + (do (E.ErrorT io.Monad<IO>) + [millis (java/io/File::lastModified [] (java/io/File::new file))] + (wrap (|> millis d.from-millis i.absolute)))) (def: #export (set-last-modified time file) - (-> i.Instant File (T.Task Bool)) - (P.future (java/io/File::setLastModified [(|> time i.relative d.to-millis)] - (java/io/File::new file)))) + (-> i.Instant File (Process Bool)) + (java/io/File::setLastModified [(|> time i.relative d.to-millis)] + (java/io/File::new file))) diff --git a/stdlib/test/test/lux/world/file.lux b/stdlib/test/test/lux/world/file.lux index 1721c8cbb..388a52807 100644 --- a/stdlib/test/test/lux/world/file.lux +++ b/stdlib/test/test/lux/world/file.lux @@ -32,131 +32,141 @@ ($_ seq (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +0 code)))] - result (do T.Monad<Task> - [pre (@.exists? file) - _ (@.write dataL file) - post (@.exists? file) - deleted? (@.delete file) - remains? (@.exists? file)] - (wrap (and (not pre) post - deleted? (not remains?))))] + result (P.future + (do io.Monad<Process> + [pre (@.exists? file) + _ (@.write dataL file) + post (@.exists? file) + deleted? (@.delete file) + remains? (@.exists? file)] + (wrap (and (not pre) post + deleted? (not remains?)))))] (assert "Can create/delete files." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +1 code)))] - result (do T.Monad<Task> - [_ (@.write dataL file) - output (@.read file) - _ (@.delete file)] - (wrap (:: blob.Eq<Blob> = dataL output)))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file) + output (@.read file) + _ (@.delete file)] + (wrap (:: blob.Eq<Blob> = dataL output))))] (assert "Can write/read files." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +2 code)))] - result (do T.Monad<Task> - [_ (@.write dataL file) - read-size (@.size file) - _ (@.delete file)] - (wrap (n/= file-size read-size)))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file) + read-size (@.size file) + _ (@.delete file)] + (wrap (n/= file-size read-size))))] (assert "Can read file size." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +3 code)))] - result (do T.Monad<Task> - [_ (@.write dataL file) - _ (@.append dataR file) - output (@.read file) - read-size (@.size file) - _ (@.delete file)] - (wrap (and (n/= (n/* +2 file-size) read-size) - (:: blob.Eq<Blob> = dataL (E.assume (blob.slice +0 (n/dec file-size) output))) - (:: blob.Eq<Blob> = dataR (E.assume (blob.slice file-size (n/dec read-size) output))))))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file) + _ (@.append dataR file) + output (@.read file) + read-size (@.size file) + _ (@.delete file)] + (wrap (and (n/= (n/* +2 file-size) read-size) + (:: blob.Eq<Blob> = dataL (E.assume (blob.slice +0 (n/dec file-size) output))) + (:: blob.Eq<Blob> = dataR (E.assume (blob.slice file-size (n/dec read-size) output)))))))] (assert "Can append to files." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [dir (format "temp_dir_" (%n (n/+ +4 code)))] - result (do T.Monad<Task> - [pre (@.exists? dir) - _ (@.make-dir dir) - post (@.exists? dir) - deleted? (@.delete dir) - remains? (@.exists? dir)] - (wrap (and (not pre) post - deleted? (not remains?))))] + result (P.future + (do io.Monad<Process> + [pre (@.exists? dir) + _ (@.make-dir dir) + post (@.exists? dir) + deleted? (@.delete dir) + remains? (@.exists? dir)] + (wrap (and (not pre) post + deleted? (not remains?)))))] (assert "Can create/delete directories." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +5 code))) dir (format "temp_dir_" (%n (n/+ +5 code)))] - result (do T.Monad<Task> - [_ (@.write dataL file) - file-is-file (@.file? file) - file-is-directory (@.directory? file) - _ (@.delete file) - _ (@.make-dir dir) - directory-is-file (@.file? dir) - directory-is-directory (@.directory? dir) - _ (@.delete dir)] - (wrap (and file-is-file (not file-is-directory) - (not directory-is-file) directory-is-directory)))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file) + file-is-file (@.file? file) + file-is-directory (@.directory? file) + _ (@.delete file) + _ (@.make-dir dir) + directory-is-file (@.file? dir) + directory-is-directory (@.directory? dir) + _ (@.delete dir)] + (wrap (and file-is-file (not file-is-directory) + (not directory-is-file) directory-is-directory))))] (assert "Can differentiate files from directories." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +6 code))) dir (format "temp_dir_" (%n (n/+ +6 code)))] - result (do T.Monad<Task> - [_ (@.make-dir dir) - #let [file' (format dir "/" file)] - _ (@.write dataL file') - read-size (@.size file') - deleted-file (@.delete file') - deleted-dir (@.delete dir)] - (wrap (and (n/= file-size read-size) - deleted-file - deleted-dir)))] + result (P.future + (do io.Monad<Process> + [_ (@.make-dir dir) + #let [file' (format dir "/" file)] + _ (@.write dataL file') + read-size (@.size file') + deleted-file (@.delete file') + deleted-dir (@.delete dir)] + (wrap (and (n/= file-size read-size) + deleted-file + deleted-dir))))] (assert "Can create files inside of directories." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +7 code))) dir (format "temp_dir_" (%n (n/+ +7 code)))] - result (do T.Monad<Task> - [_ (@.make-dir dir) - #let [file' (format dir "/" file)] - _ (@.write dataL file') - children (@.files dir) - _ (@.delete file') - _ (@.delete dir)] - (wrap (case children - (^ (list child)) - (text.ends-with? file' child) + result (P.future + (do io.Monad<Process> + [_ (@.make-dir dir) + #let [file' (format dir "/" file)] + _ (@.write dataL file') + children (@.files dir) + _ (@.delete file') + _ (@.delete dir)] + (wrap (case children + (^ (list child)) + (text.ends-with? file' child) - _ - false)))] + _ + false))))] (assert "Can list files inside a directory." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file (format "temp_file_" (%n (n/+ +8 code)))] - result (do T.Monad<Task> - [_ (@.write dataL file) - was-modified? (@.set-last-modified last-modified file) - time-read (@.get-last-modified file) - _ (@.delete file)] - (wrap (and was-modified? - (:: i.Eq<Instant> = last-modified time-read))))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file) + was-modified? (@.set-last-modified last-modified file) + time-read (@.get-last-modified file) + _ (@.delete file)] + (wrap (and was-modified? + (:: i.Eq<Instant> = last-modified time-read)))))] (assert "Can change the time of last modification." (E.default false result)))) (wrap (do P.Monad<Promise> [#let [file0 (format "temp_file_" (%n (n/+ +9 code)) "0") file1 (format "temp_file_" (%n (n/+ +9 code)) "1")] - result (do T.Monad<Task> - [_ (@.write dataL file0) - pre (@.exists? file0) - moved? (@.move file1 file0) - post (@.exists? file0) - confirmed? (@.exists? file1) - deleted? (@.delete file1)] - (wrap (and pre moved? (not post) - confirmed? deleted?)))] + result (P.future + (do io.Monad<Process> + [_ (@.write dataL file0) + pre (@.exists? file0) + moved? (@.move file1 file0) + post (@.exists? file0) + confirmed? (@.exists? file1) + deleted? (@.delete file1)] + (wrap (and pre moved? (not post) + confirmed? deleted?))))] (assert "Can move a file from one path to another." (E.default false result)))) ))) |