Skip to content

Commit

Permalink
Merge pull request #3836 from maxonfjvipon/bug/#3481/rhos-in-text
Browse files Browse the repository at this point in the history
bug(#3481): removed redundant rhos in whole `eo-runtime` + optimized some objects
  • Loading branch information
yegor256 authored Jan 20, 2025
2 parents f4aabe2 + d69b0d8 commit 4c1e844
Show file tree
Hide file tree
Showing 19 changed files with 534 additions and 499 deletions.
54 changes: 27 additions & 27 deletions eo-runtime/src/main/eo/org/eolang/fs/dir.eo
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@
# Directory in the file system.
# Apparently every directory is a file.
[file] > dir
$ > as-dir
$.file > @
true > is-directory

# Makes a directory together with all required
# parent directories and returns the created directory.
[] > made
if. > @
^.exists
^
seq *
mkdir
as-dir. > @
if.
^.exists
^
seq *
mkdir
^

# Makes a directory together with all required
# parent directories.
Expand All @@ -59,40 +61,38 @@
# Deletes directory and all files in it, recursively.
# Returns the deleted directory.
[] > deleted
(^.walk "**").at.^ > walked
walked.length > len!
if. > @
^.exists
seq *
rec-delete walked 0
as-dir. > @
if.
^.exists
seq *
rec-delete (walk "**").as-tuple
^
^
^

# Deletes files and directories in current directory recursively.
# Returns `true`.
#
# Attention! The object is for internal usage only, please
# don't use the object programmatically outside of `dir` object.
[tup index] > rec-delete
[tup] > rec-delete
if. > @
^.len.eq index
tup.length.eq 0
true
seq *
tup.tail.deleted.exists
^.rec-delete
tup.head
index.plus 1
tup.value.deleted
rec-delete tup.prev

# Creates an empty temporary file in the current directory.
[] > tmpfile
if. > @
^.exists
QQ.fs.file
string touch.as-bytes
error
sprintf
"Directory %s does not exist, can't create temporary file"
* ^.path
as-file. > @
if.
^.exists
QQ.fs.file
touch.as-bytes
error
sprintf
"Directory %s does not exist, can't create temporary file"
* file

# Creates an empty temporary file in the current directory and
# returns absolute path to it as `string`.
Expand All @@ -107,4 +107,4 @@
error > @
sprintf
"The file %s is a directory, can't open for I/O operations"
* ^.path
* file
121 changes: 62 additions & 59 deletions eo-runtime/src/main/eo/org/eolang/fs/file.eo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

# The file object in the filesystem.
[path] > file
$ > as-file
$.path > @
# Convert the `file` to the `path`.
(QQ.fs.path path).determined > as-path

# Returns `true` if current file is a directory, returns `false` otherwise.
[] > is-directory /org.eolang.bool
Expand All @@ -43,12 +46,13 @@
# If current file does not exist - create an empty file
# in filesystem and returns it.
[] > touched
if. > @
^.exists
^
seq *
touch
as-file. > @
if.
exists
^
seq *
touch
^

# Creates new empty file.
#
Expand All @@ -59,12 +63,13 @@
# If current file exists - deletes it and returns it.
# If current file does not exist - just returns it.
[] > deleted
if. > @
^.exists
seq *
delete
as-file. > @
if.
exists
seq *
delete
^
^
^

# Deletes the file and returns `true`.
#
Expand All @@ -77,8 +82,8 @@

# Move current file to `target`, making and returning a new `file` from it.
[target] > moved
QQ.fs.file > @
string move.as-bytes
as-file. > @
file move

# Tries to move file from `^.path` to `target`
# and returns path of moved file as `string`.
Expand All @@ -88,9 +93,6 @@
# don't use the object programmatically outside of `file` object.
[] > move /org.eolang.string

# Convert the `file` to the `path`.
(QQ.fs.path ^.path).determined > [] > as-path

# Opens the file.
#
# The first argument `mode` defines the operations that are allowed on the file
Expand Down Expand Up @@ -124,12 +126,12 @@
# closes the file stream and returns an original file object.
[mode scope] > open
mode > access!
access.eq "r" > read
access.eq "w" > write
access.eq "a" > append
access.eq "r+" > read-write
access.eq "w+" > write-read
access.eq "a+" > read-append
(access.eq "r").as-bool > read
(access.eq "w").as-bool > write
(access.eq "a").as-bool > append
(access.eq "r+").as-bool > read-write
(access.eq "w+").as-bool > write-read
(access.eq "a+").as-bool > read-append
as-bool. > can-read
or.
read.or read-write
Expand All @@ -144,31 +146,32 @@
read.or read-write
as-bool. > truncate
write.or write-read
if. > @
can-read.not.and can-write.not
error "Wrong access mod. Only next modes are available: 'r', 'w', 'a', 'r+', 'w+', 'a+'"
as-file. > @
if.
^.exists.not
if.
must-exists
error
sprintf
"File must exist for given access mod: '%s'"
* access
seq *
^.touched.touch
process-file
^
can-read.not.and can-write.not
error "Wrong access mod. Only next modes are available: 'r', 'w', 'a', 'r+', 'w+', 'a+'"
if.
truncate
seq *
^.deleted.delete
^.touched.touch
process-file
^
seq *
process-file
^
exists.not
if.
must-exists
error
sprintf
"File must exist for given access mod: '%s'"
* access
seq *
touched.touch
process-file
^
if.
truncate
seq *
deleted.delete
touched.touch
process-file
^
seq *
process-file
^

# Process current file in the provided scope.
#
Expand All @@ -191,33 +194,33 @@
# Returns new instance of `input-block` with `buffer` read from file, or
# returns `error` if access mode does not allow reading operations.
[size] > read
((input-block --).read size).self > @
((input-block --).read size).this > @

# File input block
#
# Attention! The object is for internal usage only, please
# don't use the object programmatically outside of `file` object.
[buffer] > input-block
$ > self
$ > this
buffer > @

# Read `size` amount of bytes from file input stream.
# Returns new instance of `input-block` with `buffer` read from file, or
# returns `error` if provided access mode does not allow reading operations.
[size] > read
^.^.read-bytes size > read-bytes!
self. > @
this. > @
if.
^.^.^.^.can-read.not
can-read.not
[] >>
$ > self
$ > this
error > @
sprintf
"Can't read from file with provided access mode '%s'"
* ^.^.^.^.^.access
* access
seq *
read-bytes
^.^.input-block read-bytes
input-block read-bytes

# Bytes read from file input stream
#
Expand All @@ -231,14 +234,14 @@
# Returns new instance of `output-block` ready to write again, or
# returns an error if provided access mode does not allow writing operations.
[buffer] > write
(output-block.write buffer).self > @
(output-block.write buffer).this > @

# File output block.
#
# Attention! The object is for internal usage only, please
# don't use the object programmatically outside of `file` object.
[] > output-block
$ > self
$ > this
true > @

# Write given `buffer` to file output stream.
Expand All @@ -247,18 +250,18 @@
# Returns new instance of `output-block` ready to write again, or
# returns an error if provided access mode does not allow writing operations.
[buffer] > write
self. > @
this. > @
if.
^.^.^.^.can-write.not
can-write.not
[] >>
$ > self
$ > this
error > @
sprintf
"Can't write to file with provided access mode '%s'"
* ^.^.^.^.^.access
* access
seq *
^.^.written-bytes buffer
^.^.output-block
written-bytes buffer
output-block

# Bytes written to file output stream.
#
Expand Down
Loading

0 comments on commit 4c1e844

Please sign in to comment.