-
Notifications
You must be signed in to change notification settings - Fork 10
/
simplifile.gleam
895 lines (813 loc) · 24.4 KB
/
simplifile.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
import filepath
import gleam/bit_array
import gleam/int
import gleam/list
import gleam/result
import gleam/set.{type Set}
@target(erlang)
import gleam/string
/// This type represents all of the reasons for why a file system operation could fail.
///
/// Most of these reasons are POSIX errors, which come from the operating system
/// and start with E. Others have been added to represent other issues that may
/// arise specific to this library.
///
pub type FileError {
/// Permission denied.
Eacces
/// Resource temporarily unavailable.
Eagain
/// Bad file number
Ebadf
/// Bad message.
Ebadmsg
/// File busy.
Ebusy
/// Resource deadlock avoided.
Edeadlk
/// On most architectures, same as `Edeadlk`. On some architectures, it
/// means "File locking deadlock error."
Edeadlock
/// Disk quota exceeded.
Edquot
/// File already exists.
Eexist
/// Bad address in system call argument.
Efault
/// File too large.
Efbig
/// Inappropriate file type or format. Usually caused by trying to set the
/// "sticky bit" on a regular file (not a directory).
Eftype
/// Interrupted system call.
Eintr
/// Invalid argument.
Einval
/// I/O error.
Eio
/// Illegal operation on a directory.
Eisdir
/// Too many levels of symbolic links.
Eloop
/// Too many open files.
Emfile
/// Too many links.
Emlink
/// Multihop attempted.
Emultihop
/// Filename too long
Enametoolong
/// File table overflow
Enfile
/// No buffer space available.
Enobufs
/// No such device.
Enodev
/// No locks available.
Enolck
/// Link has been severed.
Enolink
/// No such file or directory.
Enoent
/// Not enough memory.
Enomem
/// No space left on device.
Enospc
/// No STREAM resources.
Enosr
/// Not a STREAM.
Enostr
/// Function not implemented.
Enosys
/// Block device required.
Enotblk
/// Not a directory.
Enotdir
/// Operation not supported.
Enotsup
/// No such device or address.
Enxio
/// Operation not supported on socket.
Eopnotsupp
/// Value too large to be stored in data type.
Eoverflow
/// Not owner.
Eperm
/// Broken pipe.
Epipe
/// Result too large.
Erange
/// Read-only file system.
Erofs
/// Invalid seek.
Espipe
/// No such process.
Esrch
/// Stale remote file handle.
Estale
/// Text file busy.
Etxtbsy
/// Cross-domain link.
Exdev
/// File was requested to be read as UTF-8, but is not UTF-8 encoded.
NotUtf8
/// Any error not accounted for by this type
Unknown
}
/// Convert an error into a human-readable description
/// ## Example
/// ```gleam
/// let assert "Input/output error" = describe_error(Eio)
/// ```
///
pub fn describe_error(error: FileError) -> String {
case error {
Eperm -> "Operation not permitted"
Enoent -> "No such file or directory"
Esrch -> "No such process"
Eintr -> "Interrupted system call"
Eio -> "Input/output error"
Enxio -> "Device not configured"
Ebadf -> "Bad file descriptor"
Edeadlk -> "Resource deadlock avoided"
Edeadlock -> "Resource deadlock avoided"
Enomem -> "Cannot allocate memory"
Eacces -> "Permission denied"
Efault -> "Bad address"
Enotblk -> "Block device required"
Ebusy -> "Resource busy"
Eexist -> "File exists"
Exdev -> "Cross-device link"
Enodev -> "Operation not supported by device"
Enotdir -> "Not a directory"
Eisdir -> "Is a directory"
Einval -> "Invalid argument"
Enfile -> "Too many open files in system"
Emfile -> "Too many open files"
Etxtbsy -> "Text file busy"
Efbig -> "File too large"
Enospc -> "No space left on device"
Espipe -> "Illegal seek"
Erofs -> "Read-only file system"
Emlink -> "Too many links"
Epipe -> "Broken pipe"
Erange -> "Result too large"
Eagain -> "Resource temporarily unavailable"
Enotsup -> "Operation not supported"
Enobufs -> "No buffer space available"
Eloop -> "Too many levels of symbolic links"
Enametoolong -> "File name too long"
Edquot -> "Disc quota exceeded"
Estale -> "Stale NFS file handle"
Enolck -> "No locks available"
Enosys -> "Function not implemented"
Eftype -> "Inappropriate file type or format"
Eoverflow -> "Value too large to be stored in data type"
Ebadmsg -> "Bad message"
Emultihop -> "Multihop attempted"
Enolink -> "Link has been severed"
Enosr -> "No STREAM resources"
Enostr -> "Not a STREAM"
Eopnotsupp -> "Operation not supported on socket"
NotUtf8 -> "File not UTF-8 encoded"
Unknown -> "Unknown error"
}
}
/// Represents the intersection of information available
/// from erlang's `file:read_file_info` and node's `fs.stat`
pub type FileInfo {
FileInfo(
/// File size in bytes.
size: Int,
/// File mode that indicates the file type and its permissions.
/// For example, in Unix and Linux, a mode value of 33188 indicates
/// a regular file and the permissions associated with it
/// (read and write for the owner, and read-only for others, in
/// this case).
mode: Int,
/// Number of hard links that exist for the file.
nlinks: Int,
/// Inode number, which is a unique identifier for the file in the filesystem.
inode: Int,
/// User ID of the file's owner.
user_id: Int,
/// Group ID of the file's group.
group_id: Int,
/// Device ID of the file's major device.
/// TODO: We can actually get a major device and minor device from both
/// node and erlang. The `fs.stat` in node returns a `dev` and `rdev`,
/// so we can use some bitwise operations to get the minor out of `rdev`.
/// Someone who's not me should totally make a PR for that.
dev: Int,
/// The last access time in seconds since the UNIX epoch (00:00:00 UTC on 1 January 1970).
atime_seconds: Int,
/// The last modification time in seconds since the UNIX epoch (00:00:00 UTC on 1 January 1970).
mtime_seconds: Int,
/// The last change time in seconds since the UNIX epoch (00:00:00 UTC on 1 January 1970).
ctime_seconds: Int,
)
}
@target(erlang)
@external(erlang, "simplifile_erl", "file_info")
fn do_file_info(a: String) -> Result(FileInfo, FileError)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "fileInfo")
fn do_file_info(a: String) -> Result(FileInfo, String)
/// Get information about a file at a given path
pub fn file_info(a: String) -> Result(FileInfo, FileError) {
do_file_info(a)
|> cast_error
}
/// Read a files contents as a string
/// ## Example
/// ```gleam
/// let assert Ok(records) = read(from: "./users.csv")
/// ```
///
pub fn read(from filepath: String) -> Result(String, FileError) {
do_read(filepath)
|> cast_error
}
/// Write a string to a file at the given path
/// ## Example
/// ```gleam
/// let assert Ok(Nil) = write(to: "./hello_world.txt", contents: "Hello, World!")
/// ```
///
pub fn write(
to filepath: String,
contents contents: String,
) -> Result(Nil, FileError) {
do_write(contents, to: filepath)
|> cast_error
}
/// Delete a file or directory at a given path. Performs a recursive
/// delete on a directory.
/// Throws an error if the path does not exist.
/// ## Example
/// ```gleam
/// let assert Ok(Nil) = delete(file_at: "./delete_me.txt")
/// ```
///
pub fn delete(file_or_dir_at path: String) -> Result(Nil, FileError) {
do_delete(path)
|> cast_error
}
/// Delete all files/directories specified in a list of paths.
/// Recursively deletes provided directories.
/// Does not return an error if one or more of the provided paths
/// do not exist.
///
pub fn delete_all(paths paths: List(String)) -> Result(Nil, FileError) {
case paths {
[] -> Ok(Nil)
[path, ..rest] -> {
case delete(path) {
Ok(Nil) | Error(Enoent) -> delete_all(rest)
e -> e
}
}
}
}
/// Append a string to the contents of a file at the given path
/// ## Example
/// ```gleam
/// let assert Ok(Nil) = append(to: "./needs_more_text.txt", contents: "more text")
/// ```
///
pub fn append(
to filepath: String,
contents contents: String,
) -> Result(Nil, FileError) {
do_append(contents, to: filepath)
|> cast_error
}
/// Read a files contents as a bitstring
/// ## Example
/// ```gleam
/// let assert Ok(records) = read_bits(from: "./users.csv")
/// ```
///
pub fn read_bits(from filepath: String) -> Result(BitArray, FileError) {
do_read_bits(filepath)
|> cast_error
}
/// Write a bitstring to a file at the given path
/// ## Example
/// ```gleam
/// let assert Ok(Nil) = write_bits(to: "./hello_world.txt", bits: <<"Hello, World!":utf8>>)
/// ```
///
pub fn write_bits(
to filepath: String,
bits bits: BitArray,
) -> Result(Nil, FileError) {
do_write_bits(bits, filepath)
|> cast_error
}
/// Append a bitstring to the contents of a file at the given path
/// ## Example
/// ```gleam
/// let assert Ok(Nil) = append_bits(to: "./needs_more_text.txt", bits: <<"more text":utf8>>)
/// ```
///
pub fn append_bits(
to filepath: String,
bits bits: BitArray,
) -> Result(Nil, FileError) {
do_append_bits(bits, filepath)
|> cast_error
}
/// Checks if the provided filepath is a directory
/// ## Example
/// ```gleam
/// let assert True = is_directory("./test")
/// ```
///
@deprecated("Use `verify_is_directory` instead")
pub fn is_directory(filepath: String) -> Bool {
do_is_directory(filepath)
}
/// Checks if the provided filepath exists and is a directory.
/// Returns an error if it lacks permissions to read the directory.
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_directory("./test")
/// ```
pub fn verify_is_directory(filepath: String) -> Result(Bool, FileError) {
do_verify_is_directory(filepath)
|> cast_error
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "isValidDirectory")
fn do_verify_is_directory(filepath: String) -> Result(Bool, String)
@target(erlang)
@external(erlang, "simplifile_erl", "is_valid_directory")
fn do_verify_is_directory(filepath: String) -> Result(Bool, FileError)
/// Create a directory at the provided filepath. Returns an error if
/// the directory already exists.
///
/// ## Example
/// ```gleam
/// create_directory("./test")
/// ```
pub fn create_directory(filepath: String) -> Result(Nil, FileError) {
do_make_directory(filepath)
|> cast_error
}
/// Create a symbolic link called symlink pointing to target.
///
/// ## Example
/// ```gleam
/// create_symlink("../target", "./symlink")
/// ```
pub fn create_symlink(
to target: String,
from symlink: String,
) -> Result(Nil, FileError) {
do_make_symlink(target, symlink)
|> cast_error
}
/// Lists the contents of a directory.
/// The list contains directory and file names, and is not recursive.
///
/// ## Example
/// ```gleam
/// let assert Ok(files_and_folders) = read_directory(at: "./Folder1")
/// ```
///
pub fn read_directory(at path: String) -> Result(List(String), FileError) {
do_read_directory(path)
|> cast_error
}
/// Returns `True` if there is a file at the given path, false otherwise.
///
@deprecated("Use `verify_is_file` instead")
pub fn is_file(filepath: String) -> Bool {
do_is_file(filepath)
}
/// Checks if the file at the provided filepath exists and is a file.
/// Returns an Error if it lacks permissions to read the file.
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_file("./test.txt")
/// ```
///
pub fn verify_is_file(filepath: String) -> Result(Bool, FileError) {
do_verify_is_file(filepath)
|> cast_error
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "isValidFile")
fn do_verify_is_file(filepath: String) -> Result(Bool, String)
@target(erlang)
@external(erlang, "simplifile_erl", "is_valid_file")
fn do_verify_is_file(filepath: String) -> Result(Bool, FileError)
/// Checks if the file at the provided filepath exists and is a symbolic link.
/// Returns an Error if it lacks permissions to read the file.
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_symlink("./symlink")
/// ```
///
pub fn verify_is_symlink(filepath: String) -> Result(Bool, FileError) {
do_verify_is_symlink(filepath)
|> cast_error
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "isValidSymlink")
fn do_verify_is_symlink(filepath: String) -> Result(Bool, String)
@target(erlang)
@external(erlang, "simplifile_erl", "is_valid_symlink")
fn do_verify_is_symlink(filepath: String) -> Result(Bool, FileError)
/// Creates an empty file at the given filepath. Returns an `Error(Eexist)`
/// if the file already exists.
///
pub fn create_file(at filepath: String) -> Result(Nil, FileError) {
case
filepath
|> verify_is_file,
filepath
|> verify_is_directory
{
Ok(True), _ | _, Ok(True) -> Error(Eexist)
_, _ -> write_bits(<<>>, to: filepath)
}
}
/// Recursively creates necessary directories for a given directory
/// path. Note that if you pass a path that "looks like" a file, i.e.
/// `./a/b.txt`, a folder named `b.txt` will be created, so be sure
/// to pass only the path to the required directory.
pub fn create_directory_all(dirpath: String) -> Result(Nil, FileError) {
let is_abs = filepath.is_absolute(dirpath)
let path =
dirpath
|> filepath.split
|> list.fold("", filepath.join)
let path = case is_abs {
True -> "/" <> path
False -> path
}
do_create_dir_all(path <> "/")
|> cast_error
}
/// Copy a file at a given path to another path.
/// Note: destination should include the filename, not just the directory
pub fn copy_file(at src: String, to dest: String) -> Result(Nil, FileError) {
do_copy_file(src, dest)
|> result.replace(Nil)
|> cast_error
}
/// Rename a file at a given path to another path.
/// Note: destination should include the filename, not just the directory
pub fn rename_file(at src: String, to dest: String) -> Result(Nil, FileError) {
do_rename_file(src, dest)
|> cast_error
}
/// Copy a directory recursively
pub fn copy_directory(at src: String, to dest: String) -> Result(Nil, FileError) {
// Erlang does not provide a built in `copy_dir` function,
// and Deno doesn't support Node's `fs.cpSync`, so we'll just roll
// our own for now.
use _ <- result.try(create_directory_all(dest))
do_copy_directory(src, dest)
}
fn do_copy_directory(src: String, dest: String) -> Result(Nil, FileError) {
// Iterate over the segments of the file
use segments <- result.try(read_directory(src))
segments
|> list.each(fn(segment) {
let src_path = filepath.join(src, segment)
let dest_path = filepath.join(dest, segment)
case verify_is_file(src_path), verify_is_directory(src_path) {
Ok(True), Ok(False) -> {
// For a file, create the file in the new directory
use content <- result.try(read_bits(src_path))
content
|> write_bits(to: dest_path)
}
Ok(False), Ok(True) -> {
// Create the target directory and recurse
use _ <- result.try(create_directory(dest_path))
do_copy_directory(src_path, dest_path)
}
Error(e), _ | _, Error(e) -> {
Error(e)
}
Ok(False), Ok(False) | Ok(True), Ok(True) -> {
// We're really not sure how that one happened.
Error(Unknown)
}
}
})
Ok(Nil)
}
/// Copy a directory recursively and then delete the old one.
pub fn rename_directory(
at src: String,
to dest: String,
) -> Result(Nil, FileError) {
use _ <- result.try(copy_directory(src, dest))
delete(src)
}
/// Clear the contents of a directory, deleting all files and directories within
/// but leaving the top level directory in place.
pub fn clear_directory(at path: String) -> Result(Nil, FileError) {
use paths <- result.try(read_directory(path))
paths
|> list.map(filepath.join(path, _))
|> delete_all
}
/// Returns a list of filepaths for every file in the directory, including nested
/// files.
///
pub fn get_files(in directory: String) -> Result(List(String), FileError) {
use contents <- result.try(read_directory(directory))
let paths =
contents
|> list.map(filepath.join(directory, _))
let files = list.filter(paths, fn(path) { verify_is_file(path) == Ok(True) })
case list.filter(paths, fn(path) { verify_is_directory(path) == Ok(True) }) {
[] -> Ok(files)
directories -> {
use nested_files <- result.try(list.try_map(directories, get_files))
Ok(list.append(files, list.flatten(nested_files)))
}
}
}
/// Represents a file permission
pub type Permission {
Read
Write
Execute
}
fn permission_to_integer(permission: Permission) -> Int {
case permission {
Read -> 0o4
Write -> 0o2
Execute -> 0o1
}
}
/// Represents a set of file permissions for a given file
pub type FilePermissions {
FilePermissions(
user: Set(Permission),
group: Set(Permission),
other: Set(Permission),
)
}
pub fn file_permissions_to_octal(permissions: FilePermissions) -> Int {
let make_permission_digit = fn(permissions: Set(Permission)) {
permissions
|> set.to_list
|> list.map(permission_to_integer)
|> int.sum
}
make_permission_digit(permissions.user)
* 64
+ make_permission_digit(permissions.group)
* 8
+ make_permission_digit(permissions.other)
}
/// Sets the permissions for a given file
///
/// # Example
/// ```gleam
/// let all = set.from_list([Read, Write, Execute])
/// let all = FilePermissions(user: all, group: all, other: all)
/// let assert Ok(Nil) = set_permissions("./script.sh", all)
/// ```
pub fn set_permissions(
for_file_at filepath: String,
to permissions: FilePermissions,
) -> Result(Nil, FileError) {
set_permissions_octal(filepath, file_permissions_to_octal(permissions))
}
/// Sets the permissions for a given file using an octal representation
///
/// # Example
/// ```gleam
/// set_permissions_octal("./script.sh", 0o777)
/// ```
pub fn set_permissions_octal(
for_file_at filepath: String,
to permissions: Int,
) -> Result(Nil, FileError) {
do_set_permissions(filepath, permissions)
|> cast_error
}
/// Returns the current working directory
///
pub fn current_directory() -> Result(String, FileError) {
do_current_directory()
|> cast_error
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "currentDirectory")
fn do_current_directory() -> Result(String, String)
@target(erlang)
fn do_current_directory() -> Result(String, FileError) {
do_do_current_directory()
|> result.map(string.from_utf_codepoints)
}
@target(erlang)
@external(erlang, "file", "get_cwd")
fn do_do_current_directory() -> Result(List(UtfCodepoint), FileError)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "setPermissions")
fn do_set_permissions(
file_at: String,
permissions_octal: Int,
) -> Result(Nil, String)
@target(erlang)
@external(erlang, "simplifile_erl", "set_permissions")
fn do_set_permissions(
file_at: String,
permissions_octal: Int,
) -> Result(Nil, FileError)
@target(javascript)
fn do_read(from filepath: String) -> Result(String, String) {
case do_read_bits(filepath) {
Ok(bits) -> {
case bit_array.to_string(bits) {
Ok(str) -> Ok(str)
_ -> Error("NOTUTF8")
}
}
Error(e) -> Error(e)
}
}
@target(javascript)
fn do_write(content: String, to filepath: String) -> Result(Nil, String) {
content
|> bit_array.from_string
|> do_write_bits(to: filepath)
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "deleteFileOrDirRecursive")
fn do_delete(file_or_dir_at: String) -> Result(Nil, String)
@target(javascript)
fn do_append(content: String, to filepath: String) -> Result(Nil, String) {
content
|> bit_array.from_string
|> do_append_bits(to: filepath)
}
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "readBits")
fn do_read_bits(from: String) -> Result(BitArray, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "writeBits")
fn do_write_bits(content: BitArray, to filepath: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "appendBits")
fn do_append_bits(content: BitArray, to filepath: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "isDirectory")
fn do_is_directory(filepath: String) -> Bool
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "makeDirectory")
fn do_make_directory(filepath: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "makeSymlink")
fn do_make_symlink(target: String, symlink: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "createDirAll")
fn do_create_dir_all(dirpath: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "listContents")
fn do_read_directory(directory_path: String) -> Result(List(String), String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "copyFile")
fn do_copy_file(at: String, to: String) -> Result(Nil, String)
@target(javascript)
@external(javascript, "./simplifile_js.mjs", "renameFile")
fn do_rename_file(at: String, to: String) -> Result(Nil, String)
@target(javascript)
fn cast_error(input: Result(a, String)) -> Result(a, FileError) {
result.map_error(input, fn(e) {
case e {
"EACCES" -> Eacces
"EAGAIN" -> Eagain
"EBADF" -> Ebadf
"EBADMSG" -> Ebadmsg
"EBUSY" -> Ebusy
"EDEADLK" -> Edeadlk
"EDEADLOCK" -> Edeadlock
"EDQUOT" -> Edquot
"EEXIST" -> Eexist
"EFAULT" -> Efault
"EFBIG" -> Efbig
"EFTYPE" -> Eftype
"EINTR" -> Eintr
"EINVAL" -> Einval
"EIO" -> Eio
"EISDIR" -> Eisdir
"ELOOP" -> Eloop
"EMFILE" -> Emfile
"EMLINK" -> Emlink
"EMULTIHOP" -> Emultihop
"ENAMETOOLONG" -> Enametoolong
"ENFILE" -> Enfile
"ENOBUFS" -> Enobufs
"ENODEV" -> Enodev
"ENOLCK" -> Enolck
"ENOLINK" -> Enolink
"ENOENT" -> Enoent
"ENOMEM" -> Enomem
"ENOSPC" -> Enospc
"ENOSR" -> Enosr
"ENOSTR" -> Enostr
"ENOSYS" -> Enosys
"ENOBLK" -> Enotblk
"ENODIR" -> Enotdir
"ENOTSUP" -> Enotsup
"ENXIO" -> Enxio
"EOPNOTSUPP" -> Eopnotsupp
"EOVERFLOW" -> Eoverflow
"EPERM" -> Eperm
"EPIPE" -> Epipe
"ERANGE" -> Erange
"EROFS" -> Erofs
"ESPIPE" -> Espipe
"ESRCH" -> Esrch
"ESTALE" -> Estale
"ETXTBSY" -> Etxtbsy
"EXDEV" -> Exdev
"NOTUTF8" -> NotUtf8
_ -> Unknown
}
})
}
@target(erlang)
@external(erlang, "simplifile_erl", "append_file")
fn do_append_bits(
content: BitArray,
to filepath: String,
) -> Result(Nil, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "write_file")
fn do_write_bits(
content: BitArray,
to filepath: String,
) -> Result(Nil, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "read_file")
fn do_read_bits(from: String) -> Result(BitArray, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "recursive_delete")
fn do_delete(file_or_dir_at: String) -> Result(Nil, FileError)
@target(erlang)
fn do_append(content: String, to filepath: String) -> Result(Nil, FileError) {
content
|> bit_array.from_string
|> do_append_bits(filepath)
}
@target(erlang)
fn do_write(content: String, to filepath: String) -> Result(Nil, FileError) {
content
|> bit_array.from_string
|> do_write_bits(filepath)
}
@target(erlang)
fn do_read(from filepath: String) -> Result(String, FileError) {
case do_read_bits(filepath) {
Ok(bits) -> {
case bit_array.to_string(bits) {
Ok(str) -> Ok(str)
_ -> Error(NotUtf8)
}
}
Error(e) -> Error(e)
}
}
@target(erlang)
fn cast_error(input: Result(a, FileError)) -> Result(a, FileError) {
input
}
@target(erlang)
@external(erlang, "filelib", "is_dir")
fn do_is_directory(path: String) -> Bool
@target(erlang)
@external(erlang, "simplifile_erl", "make_directory")
fn do_make_directory(directory: String) -> Result(Nil, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "make_symlink")
fn do_make_symlink(target: String, symlink: String) -> Result(Nil, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "list_directory")
fn do_read_directory(directory: String) -> Result(List(String), FileError)
@external(erlang, "simplifile_erl", "is_file")
@external(javascript, "./simplifile_js.mjs", "isFile")
fn do_is_file(filepath: String) -> Bool
@target(erlang)
@external(erlang, "simplifile_erl", "create_dir_all")
fn do_create_dir_all(dirpath: String) -> Result(Nil, FileError)
@target(erlang)
@external(erlang, "file", "copy")
fn do_copy_file(src: String, dest: String) -> Result(Int, FileError)
@target(erlang)
@external(erlang, "simplifile_erl", "rename_file")
fn do_rename_file(src: String, dest: String) -> Result(Nil, FileError)