aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/file.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/library/lux/world/file.lux22
1 files changed, 17 insertions, 5 deletions
diff --git a/stdlib/source/library/lux/world/file.lux b/stdlib/source/library/lux/world/file.lux
index 8e2adf93b..19dde66f4 100644
--- a/stdlib/source/library/lux/world/file.lux
+++ b/stdlib/source/library/lux/world/file.lux
@@ -7,6 +7,7 @@
["." monad (#+ Monad do)]]
[control
[pipe (#+ case>)]
+ ["." maybe ("#\." functor)]
["." try (#+ Try) ("#\." functor)]
["." exception (#+ exception:)]
["." io (#+ IO) ("#\." functor)]
@@ -17,7 +18,6 @@
[data
["." bit ("#\." equivalence)]
["." product]
- ["." maybe ("#\." functor)]
["." binary (#+ Binary)]
["." text ("#\." equivalence)
["%" format (#+ format)]]
@@ -36,9 +36,12 @@
["." duration]]]])
(type: .public Path
+ {#.doc (example "A path to a file or a directory in a file-system.")}
Text)
(`` (interface: .public (System !)
+ {#.doc (example "An interface to a file-system.")}
+
(: Text
separator)
@@ -90,12 +93,14 @@
(in [parent child])))))
(def: .public (parent fs path)
+ {#.doc (example "If a path represents a nested file/directory, extracts its parent directory.")}
(All [!] (-> (System !) Path (Maybe Path)))
(|> path
(..un_rooted fs)
(maybe\map product.left)))
(def: .public (name fs path)
+ {#.doc (example "The un-nested name of a file/directory.")}
(All [!] (-> (System !) Path Text))
(|> path
(..un_rooted fs)
@@ -137,6 +142,7 @@
)))
(def: .public (rooted fs parent child)
+ {#.doc (example "A nested path for a file/directory, given a root/parent path and a file/directory name within it.")}
(All [!] (-> (System !) Path Text Path))
(format parent (\ fs separator) child))
@@ -260,7 +266,7 @@
(do (try.with io.monad)
[.let [file (java/io/File::new path)]
size (java/io/File::length file)
- .let [data (binary.create (.nat size))]
+ .let [data (binary.empty (.nat size))]
stream (java/io/FileInputStream::new file)
bytes_read (java/io/InputStream::read data stream)
_ (java/lang/AutoCloseable::close stream)]
@@ -349,7 +355,7 @@
(def: (any_callback write!)
(-> (async.Resolver (Try Any)) ffi.Function)
(<| (ffi.closure [error])
- io.run
+ io.run!
write!
(if (ffi.null? error)
(#try.Success [])
@@ -358,7 +364,7 @@
(def: (value_callback write!)
(All [a] (-> (async.Resolver (Try a)) ffi.Function))
(<| (ffi.closure [error datum])
- io.run
+ io.run!
write!
(if (ffi.null? error)
(#try.Success (:assume datum))
@@ -963,7 +969,7 @@
... (def: (make_file path)
... (do {! (try.with io.monad)}
- ... [verdict (..touch [path (|> instant.now io.run instant.relative duration.millis (i./ +1,000))])]
+ ... [verdict (..touch [path (|> instant.now io.run! instant.relative duration.millis (i./ +1,000))])]
... (\ io.monad in
... (if verdict
... (#try.Success (..file path))
@@ -985,6 +991,7 @@
(as_is)))
(def: .public (exists? monad fs path)
+ {#.doc (example "Checks if either a file or a directory exists at the given path.")}
(All [!] (-> (Monad !) (System !) Path (! Bit)))
(do monad
[verdict (\ fs file? path)]
@@ -1170,6 +1177,8 @@
(recur sub_directory tail)))))))
(def: .public (mock separator)
+ {#.doc (example "A purely in-memory simulation of a file-system."
+ "Useful for testing.")}
(-> Text (System Async))
(let [store (stm.var ..empty_mock)]
(`` (implementation
@@ -1318,6 +1327,8 @@
(\ fs make_directory path))))
(def: .public (make_directories monad fs path)
+ {#.doc (example "Creates the directory specified by the given path."
+ "Also, creates every super-directory necessary to make the given path valid.")}
(All [!] (-> (Monad !) (System !) Path (! (Try Any))))
(let [rooted? (text.starts_with? (\ fs separator) path)
segments (text.split_all_with (\ fs separator) path)]
@@ -1350,6 +1361,7 @@
(in (#try.Failure error)))))))))
(def: .public (make_file monad fs content path)
+ {#.doc (example "Creates a new file with the given content if-and-only-if the file does not already exist.")}
(All [!] (-> (Monad !) (System !) Binary Path (! (Try Any))))
(do monad
[? (\ fs file? path)]